Skip to content

Commit 6d7f548

Browse files
committed
add as visual fame
1 parent 1ba25f2 commit 6d7f548

20 files changed

Lines changed: 898 additions & 172 deletions

.github/workflows/deploy-pages.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Deploy GitHub Pages
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
pages: write
11+
id-token: write
12+
13+
concurrency:
14+
group: "pages"
15+
cancel-in-progress: true
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
24+
- name: Setup Node
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version: 20
28+
cache: npm
29+
30+
- name: Install dependencies
31+
run: npm ci
32+
33+
- name: Run tests
34+
run: npm test
35+
36+
- name: Build static site
37+
run: npm run web:build
38+
39+
- name: Configure Pages
40+
uses: actions/configure-pages@v5
41+
42+
- name: Upload artifact
43+
uses: actions/upload-pages-artifact@v3
44+
with:
45+
path: ./dist
46+
47+
deploy:
48+
needs: build
49+
runs-on: ubuntu-latest
50+
environment:
51+
name: github-pages
52+
url: ${{ steps.deployment.outputs.page_url }}
53+
steps:
54+
- name: Deploy to GitHub Pages
55+
id: deployment
56+
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/node_modules
2+
/dist

README.md

Lines changed: 95 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,87 @@
11
# Toy Robot
22

3+
Toy Robot simulator with two modes:
4+
5+
- CLI simulator (`npm start`)
6+
- Browser game for GitHub Pages (`npm run web:start`)
7+
8+
Both modes use the same simulator core and command rules.
9+
310
## Instructions
411

5-
This is a program that simulates a Toy Robot moving on a table top. The table top is a grid of 5 units x 5 units. There are no obstructions on the table surface. The Toy Robot is free to roam around the surface of the table, but must be prevented from falling to destruction. A failed or invalid command should not stop the simulation.
12+
Toy Robot moves on a tabletop grid with no obstacles.
13+
14+
- Grid coordinates are `0..5` for both X and Y (6x6 board).
15+
- Origin `(0,0)` is the south-west corner.
16+
- Invalid/failed commands do not stop the simulation.
17+
18+
Commands:
619

7-
- `PLACE X,Y,F` will place the Toy Robot on the table at position X,Y and facing direction F.
8-
- Direction can be one of the cardinal points: `NORTH`, `EAST`, `SOUTH` or `WEST`.
9-
- `MOVE` will move the Toy Robot one unit forward in the direction it is currently facing.
10-
- `LEFT` will rotate the Toy Robot 90 degrees left (anti-clockwise/counter-clockwise).
11-
- `RIGHT` will rotate the Toy Robot 90 degrees right (clockwise).
12-
- `REPORT` will announce the X,Y,F of Toy Robot.
13-
- Every command should provide visual output that the command has either succeeded or failed (display fail messages).
20+
- `PLACE X,Y,F` places the robot at `X,Y` facing `F`.
21+
- `MOVE` moves one unit forward.
22+
- `LEFT` rotates 90 degrees anti-clockwise.
23+
- `RIGHT` rotates 90 degrees clockwise.
24+
- `REPORT` outputs `X,Y,F`.
25+
26+
Facing direction `F` must be one of:
27+
28+
- `NORTH`
29+
- `EAST`
30+
- `SOUTH`
31+
- `WEST`
1432

1533
## Assumptions
1634

17-
- Successful commands are upper case (lower case letters would result fail).
18-
- Allow empty space before/after commands (e.g. ` REPORT` or `PLACE 1,1,NORTH ` result success).
19-
- Do not allow empty space between position X,Y,F (e.g. `PLACE ,,NORTH` will result fail).
20-
- Origin (0,0) to be the SOUTH WEST most corner.
21-
- Inputs are from standard input.
22-
- Ignore any move that would cause the robot to fall and fail (remain the same state).
23-
- Discard all commands in the sequence until a valid PLACE command has been executed.
24-
- Current version installed
25-
- npm: 10.2.4
26-
- node: 20.11.0
27-
- git: 2.39.2 (Apple Git-143)
35+
- Commands are case-sensitive and must be uppercase.
36+
- Leading/trailing spaces are allowed (for example ` REPORT`).
37+
- `PLACE` parameters cannot contain empty values (for example `PLACE ,,NORTH` fails).
38+
- Any move that would cause a fall fails and keeps the same state.
39+
- Commands are ignored until the first valid `PLACE`.
2840

2941
## Constraints
3042

31-
The first valid command must be `PLACE`. After that, any sequence of commands may be issued in any order, including another PLACE command.
43+
The first valid command must be `PLACE`.
44+
After that, any sequence of commands is allowed, including another `PLACE`.
45+
46+
## Example Input/Output
3247

33-
## Example Input and Output
3448
### Example A
49+
3550
Input:
36-
```
51+
52+
```text
3753
PLACE 0,0,NORTH
3854
MOVE
3955
REPORT
4056
```
4157

4258
Output:
43-
```
59+
60+
```text
4461
0,1,NORTH
4562
```
63+
4664
### Example B
65+
4766
Input:
48-
```
67+
68+
```text
4969
PLACE 0,0,NORTH
5070
LEFT
5171
REPORT
5272
```
5373

5474
Output:
55-
```
75+
76+
```text
5677
0,0,WEST
5778
```
79+
5880
### Example C
81+
5982
Input:
60-
```
83+
84+
```text
6185
PLACE 1,2,EAST
6286
MOVE
6387
MOVE
@@ -67,46 +91,68 @@ REPORT
6791
```
6892

6993
Output:
70-
```
94+
95+
```text
7196
3,3,NORTH
7297
```
98+
7399
## Setup
74-
#### Make sure you have the latest git, Node.js and Npm installed on your machine
75100

101+
Install dependencies:
102+
103+
```bash
104+
npm install
105+
```
106+
107+
Run tests:
108+
109+
```bash
110+
npm test
76111
```
77-
$ git --version
78-
$ npm --version
79-
$ node --version
112+
113+
## CLI Mode
114+
115+
Start CLI simulator:
116+
117+
```bash
118+
npm start
80119
```
81120

82-
#### Clone the repo from Github
121+
Example input:
83122

84-
`git clone https://github.com/LEO0331/ToyRobot.git`
123+
```text
124+
PLACE 0,0,NORTH
125+
MOVE
126+
REPORT
127+
```
85128

86-
#### Install dependencies with `npm install` in the ToyRobot folder
129+
Exit with `Ctrl + C` (Windows/Linux) or `Cmd + C` (macOS).
87130

88-
`npm install`
131+
## Web Game Mode
89132

90-
#### Run tests
133+
Build and serve the static game locally:
91134

92-
`npm run test`
135+
```bash
136+
npm run web:start
137+
```
93138

94-
#### Run prettier
139+
Then open:
95140

96-
`npx prettier . --write`
141+
- [http://localhost:8080](http://localhost:8080)
97142

98-
#### Start the simulator
143+
Features:
99144

100-
`npm run start` or `npm start`
145+
- Visual 6x6 board with robot direction icon
146+
- Single command input
147+
- Multiline script mode (Step / Run All)
148+
- Command log with success/fail messages
101149

102-
#### Paste your input followed by Enter
150+
## GitHub Pages
103151

104-
```
105-
PLACE 0,0,NORTH -> type Enter
106-
MOVE -> type Enter
107-
REPORT -> type Enter then the result will show
108-
```
152+
Deployment is automated by GitHub Actions via `.github/workflows/deploy-pages.yml`.
109153

110-
#### Exit the simulator
154+
- Push to `main` triggers tests + static build + deployment.
155+
- Expected public URL:
156+
- [https://leo0331.github.io/ToyRobot/](https://leo0331.github.io/ToyRobot/)
111157

112-
`Ctrl + C` or `Cmd + C` to terminate the simulator
158+
If your repo name or owner changes, update the URL accordingly.

index.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Toy Robot Game</title>
7+
<link rel="stylesheet" href="./web/styles.css" />
8+
</head>
9+
<body>
10+
<main class="app">
11+
<section class="panel">
12+
<h1>Toy Robot</h1>
13+
<p class="subhead">Web game mode with the same command rules as CLI.</p>
14+
15+
<div class="controls">
16+
<h2>Single Command</h2>
17+
<div class="row">
18+
<input
19+
id="command-input"
20+
type="text"
21+
placeholder="PLACE 0,0,NORTH"
22+
aria-label="Single command input"
23+
/>
24+
<button id="run-command" type="button">Run</button>
25+
</div>
26+
</div>
27+
28+
<div class="controls">
29+
<h2>Script Mode</h2>
30+
<textarea
31+
id="script-input"
32+
rows="8"
33+
placeholder="PLACE 0,0,NORTH&#10;MOVE&#10;RIGHT&#10;REPORT"
34+
aria-label="Script input"
35+
></textarea>
36+
<div class="row">
37+
<button id="step-script" type="button">Step</button>
38+
<button id="run-script" type="button">Run All</button>
39+
<button id="reset-script" type="button">Reset Script Cursor</button>
40+
<button id="reset-state" type="button">Reset Robot</button>
41+
</div>
42+
</div>
43+
44+
<div class="status">
45+
<h2>Current State</h2>
46+
<p id="state-line">Not placed</p>
47+
</div>
48+
</section>
49+
50+
<section class="panel">
51+
<h2>Board (0..5)</h2>
52+
<div id="board" class="board" aria-live="polite"></div>
53+
</section>
54+
55+
<section class="panel">
56+
<h2>Command Log</h2>
57+
<ol id="log" class="log"></ol>
58+
</section>
59+
</main>
60+
61+
<script type="module" src="./web/app.js"></script>
62+
</body>
63+
</html>

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"main": "index.js",
66
"scripts": {
77
"start": "babel-node ./src/index.js",
8-
"test": "jest"
8+
"test": "jest --runInBand",
9+
"web:build": "node ./scripts/build-site.js",
10+
"web:start": "npm run web:build && node ./scripts/serve-static.js dist"
911
},
1012
"author": "Leo",
1113
"license": "ISC",

scripts/build-site.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const rootDir = path.resolve(process.cwd());
5+
const outDir = path.join(rootDir, "dist");
6+
const filesToCopy = [
7+
["index.html", "index.html"],
8+
["web", "web"],
9+
["src", "src"],
10+
];
11+
12+
fs.rmSync(outDir, { recursive: true, force: true });
13+
fs.mkdirSync(outDir, { recursive: true });
14+
15+
for (const [from, to] of filesToCopy) {
16+
fs.cpSync(path.join(rootDir, from), path.join(outDir, to), {
17+
recursive: true,
18+
});
19+
}
20+
21+
console.log(`Site build complete: ${outDir}`);

0 commit comments

Comments
 (0)