Skip to content

Commit c2e55bb

Browse files
committed
Initial implementation
1 parent 92c9b7e commit c2e55bb

21 files changed

+8633
-1
lines changed

.github/workflows/main.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
on: push
2+
3+
jobs:
4+
test:
5+
runs-on: ubuntu-16.04
6+
steps:
7+
- name: checkout actions
8+
uses: actions/checkout@master
9+
10+
- name: set Node to 12.x
11+
uses: actions/setup-node@v1
12+
with:
13+
node-version: "12.x"
14+
15+
- name: install dependencies
16+
run: npm ci
17+
18+
- name: run tests
19+
run: npm t

.gitignore

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

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
settings

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
# ts-challenge
1+
# ts-challenge
2+
3+
How to run:
4+
5+
- click here https://cscleison.github.io/ts-challenge/
6+
7+
OR
8+
9+
- `npm i`
10+
- `npm start`
11+
- open `index.html`
12+
13+
Design decisions:
14+
15+
- validates all the settings before the game starts
16+
- all functions are immutable

__tests__/board.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Board, checkTile, runAction, TileStatus, Turtle } from "../src/board";
2+
import { Tile } from "../src/navigation";
3+
4+
const zeroTile: Tile = { x: 0, y: 0 };
5+
6+
describe("run action", () => {
7+
it("should move the turtle north", () => {
8+
const moved: Turtle = {
9+
direction: 0,
10+
tile: { x: 0, y: -1 }
11+
};
12+
expect(runAction("m", { direction: 0, tile: zeroTile })).toEqual(moved);
13+
});
14+
15+
it("should rotate the turtle", () => {
16+
const rotated: Turtle = {
17+
direction: 1,
18+
tile: zeroTile
19+
};
20+
expect(runAction("r", { direction: 0, tile: zeroTile })).toEqual(rotated);
21+
});
22+
});
23+
24+
describe("check tile", () => {
25+
const mine: Tile = { x: 1, y: 1 };
26+
const exit: Tile = { x: 2, y: 2 };
27+
28+
const board: Board = {
29+
xLength: 3,
30+
yLength: 3,
31+
mines: [mine],
32+
exit
33+
};
34+
const checkStatus = checkTile(board);
35+
36+
it("should be on the exit", () => {
37+
const onExit: TileStatus = "onExit";
38+
expect(checkStatus({ ...exit })).toBe(onExit);
39+
});
40+
41+
it("should be on a mine", () => {
42+
const onMine: TileStatus = "onMine";
43+
expect(checkStatus({ ...mine })).toBe(onMine);
44+
});
45+
46+
it("should be out of bounds", () => {
47+
const outOfBounds: TileStatus = "outOfBounds";
48+
const outsiders: Tile[] = [
49+
{ x: -1, y: 0 },
50+
{ x: -100, y: 0 },
51+
{ x: 0, y: -1 },
52+
{ x: 0, y: -100 },
53+
{ x: 0, y: 3 },
54+
{ x: 0, y: 300 },
55+
{ x: 3, y: 0 },
56+
{ x: 300, y: 0 }
57+
];
58+
outsiders.forEach(t => expect(checkStatus(t)).toBe(outOfBounds));
59+
});
60+
61+
it("should be in danger", () => {
62+
const inDanger: TileStatus = "inDanger";
63+
expect(checkStatus(zeroTile)).toBe(inDanger);
64+
});
65+
});

__tests__/game.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Action, Board, TileStatus, Turtle } from "../src/board";
2+
import { runSequence } from "../src/game";
3+
4+
describe("run sequence", () => {
5+
const zeroTurtle: Turtle = { direction: 0, tile: { x: 0, y: 0 } };
6+
const board: Board = {
7+
xLength: 3,
8+
yLength: 3,
9+
mines: [{ x: 2, y: 0 }],
10+
exit: { x: 0, y: 1 }
11+
};
12+
13+
it("should return outOfBounds", () => {
14+
const outOfBounds: TileStatus = "outOfBounds";
15+
expect(runSequence(["m"], zeroTurtle, board)).toBe(outOfBounds);
16+
});
17+
18+
it("should return onMine", () => {
19+
const onMine: TileStatus = "onMine";
20+
const explosives: Action[][] = [
21+
["r", "m", "m"],
22+
["r", "m", "m", "m"] // extra move to see if it stops
23+
];
24+
25+
explosives.forEach(sequence =>
26+
expect(runSequence(sequence, zeroTurtle, board)).toBe(onMine)
27+
);
28+
});
29+
30+
it("should return onExit", () => {
31+
const onExit: TileStatus = "onExit";
32+
const successful: Action[][] = [
33+
["r", "r", "m"],
34+
["r", "r", "m", "m"] // extra move to see if it stops
35+
];
36+
37+
successful.forEach(sequence =>
38+
expect(runSequence(sequence, zeroTurtle, board)).toBe(onExit)
39+
);
40+
});
41+
42+
it("should return inDanger", () => {
43+
const inDanger: TileStatus = "inDanger";
44+
const dangerous: Action[][] = [["r", "m"], ["r", "r", "r", "r"]];
45+
46+
dangerous.forEach(sequence =>
47+
expect(runSequence(sequence, zeroTurtle, board)).toBe(inDanger)
48+
);
49+
});
50+
});

__tests__/navigation.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
DIRECTIONS,
3+
isOnTiles,
4+
isSameTile,
5+
move,
6+
rotate,
7+
Tile
8+
} from "../src/navigation";
9+
10+
const zeroTile: Tile = { x: 0, y: 0 };
11+
12+
describe("move", () => {
13+
it("should move a tile north", () => {
14+
const moved: Tile = { x: 0, y: -1 };
15+
expect(move(zeroTile, DIRECTIONS.North)).toEqual(moved);
16+
});
17+
18+
it("should move a tile east", () => {
19+
const moved: Tile = { x: 1, y: 0 };
20+
expect(move(zeroTile, DIRECTIONS.East)).toEqual(moved);
21+
});
22+
23+
it("should move a tile south", () => {
24+
const moved: Tile = { x: 0, y: 1 };
25+
expect(move(zeroTile, DIRECTIONS.South)).toEqual(moved);
26+
});
27+
28+
it("should move a tile west", () => {
29+
const moved: Tile = { x: -1, y: 0 };
30+
expect(move(zeroTile, DIRECTIONS.West)).toEqual(moved);
31+
});
32+
});
33+
34+
describe("rotate", () => {
35+
it("should rotate from north to east", () => {
36+
expect(rotate(DIRECTIONS.North)).toBe(DIRECTIONS.East);
37+
});
38+
39+
it("should rotate from west to north", () => {
40+
expect(rotate(DIRECTIONS.West)).toBe(DIRECTIONS.North);
41+
});
42+
});
43+
44+
describe("is the same tile", () => {
45+
it("should be the same tile", () => {
46+
expect(isSameTile(zeroTile, zeroTile)).toBeTruthy();
47+
expect(isSameTile(zeroTile, { ...zeroTile })).toBeTruthy();
48+
});
49+
50+
it("should NOT be the same tile", () => {
51+
expect(isSameTile(zeroTile, { x: 1, y: 0 })).toBeFalsy();
52+
expect(isSameTile(zeroTile, { x: 0, y: 1 })).toBeFalsy();
53+
});
54+
});
55+
56+
describe("is on tiles", () => {
57+
it("should be on tiles", () => {
58+
expect(isOnTiles(zeroTile, [{ ...zeroTile }, { x: 1, y: 1 }])).toBeTruthy();
59+
});
60+
61+
it("should NOT be on tiles", () => {
62+
expect(isOnTiles(zeroTile, [{ x: 1, y: 1 }, { x: 2, y: 2 }])).toBeFalsy();
63+
});
64+
});

0 commit comments

Comments
 (0)