Skip to content

Commit

Permalink
Use open and flag from field in controls. fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zikoat committed Aug 11, 2024
1 parent a583ba4 commit 509357a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 77 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"ci": "bun i --production && tsc --noemit && prettier --check . && bun test && vite build",
"test": "vitest",
"test-single": "uvu -r tsm",
"test-watch": "watchlist -e src -- bun run test-single"
"test-watch": "watchlist -e src -- bun run test-single",
"type-watch":"tsc --noEmit --watch"
},
"repository": {
"type": "git",
Expand Down
49 changes: 6 additions & 43 deletions src/Controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const LONG_PRESS_DURATION = 200; // Duration in milliseconds to consider it a lo
export class Controls {
private static cursor: Cursor;
private static field: Field;
private static fieldStorage: FieldPersistence;
private static fieldStorage: FieldPersistence | undefined;

public constructor(
rootObject: PIXI.Container,
Expand Down Expand Up @@ -152,62 +152,25 @@ export class Controls {
private static open() {
const x = Controls.cursor.getX();
const y = Controls.cursor.getY();
const cell = Controls.field.getCell(x, y);
const neighbors = Controls.field.getNeighbors(x, y);
const flaggedNeighbors = neighbors.filter(
(cell) => cell.isFlagged || (cell.isOpen && cell.isMine),
);
const closedNotFlaggedNeighbors = neighbors.filter(
(cell) => !cell.isOpen && !cell.isFlagged,
);

if (Controls.fieldStorage === undefined)
throw new Error("tried to save, but fieldstorage is undefined");

if ((!cell.isOpen && !cell.isFlagged) || (cell.isOpen && cell.isMine)) {
Controls.field.open(cell.x, cell.y);
Controls.fieldStorage.save(Controls.field, Controls.field.fieldName);
} else if (
flaggedNeighbors.length === Controls.field.value(cell.x, cell.y) &&
closedNotFlaggedNeighbors.length > 0
) {
closedNotFlaggedNeighbors.forEach((neighbor) => {
Controls.field.open(neighbor.x, neighbor.y);
});
Controls.fieldStorage.save(Controls.field, Controls.field.fieldName);
} else if (cell.isOpen) {
Controls.flag();
}
this.field.open(x, y);
Controls.fieldStorage.save(Controls.field, Controls.field.fieldName);
}

// todo this logic should be moved to the field. If we want custom behavior then we should save settings on the field to enable/disable multi-flagging.
private static flag() {
const x = Controls.cursor.getX();
const y = Controls.cursor.getY();
const cell = Controls.field.getCell(x, y);
const neighbors = Controls.field.getNeighbors(x, y);
const closedNeighbors = neighbors.filter(
(cell) => !cell.isOpen || (cell.isOpen && cell.isMine),
);
const closedNotFlaggedNeighbors = neighbors.filter(
(cell) => !cell.isOpen && !cell.isFlagged,
);

if (Controls.fieldStorage === undefined)
throw new Error("tried to save, but fieldstorage is undefined");

if (!cell.isOpen) {
Controls.field.flag(cell.x, cell.y);
Controls.fieldStorage.save(Controls.field, Controls.field.fieldName);
} else if (
closedNeighbors.length === Controls.field.value(cell.x, cell.y) &&
closedNotFlaggedNeighbors.length > 0
) {
closedNotFlaggedNeighbors.forEach((neighbor) => {
Controls.field.flag(neighbor.x, neighbor.y);
});
Controls.fieldStorage.save(Controls.field, Controls.field.fieldName);
}
Controls.field.flag(x, y);

Controls.fieldStorage.save(Controls.field, Controls.field.fieldName);
}
}

Expand Down
61 changes: 32 additions & 29 deletions src/Field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ describe("FieldStorage", () => {
// assert( fieldStorage.localStorage.length, 1, "localstorage length before open" );

const openedCells = field1.open(0, 0);
const flaggedCell1 = field1.flag(3, 3);
const flaggedCells1 = field1.flag(3, 3);

assert(field1.getCell(0, 0).isOpen === true, "opened cell should be open");
if (!(flaggedCell1 instanceof Cell))
throw Error("flag does not return cell");
expect(flaggedCells1).toHaveLength(1);
const flaggedCell1 = flaggedCells1[0];
expect(flaggedCell1).toBeInstanceOf(Cell);

assert(openedCells.length === 9);
assert(flaggedCell1.isFlagged === true);
assertCellsAreSame(flaggedCell1, field1.getCell(3, 3));
Expand Down Expand Up @@ -215,17 +217,15 @@ describe("FieldStorage", () => {
field1.open(0, 0);

const map = fieldViewToString(field1, -3, -3, 3, 3);

assert(
map ===
`.......
.xxx...
..422x.
.x202..
.x324x.
...xxx.
.......
`,
assertFieldViewEquals(
map,
`.......
.x.xx..
.x423..
.x202x.
..224x.
..x.xx.
.......`,
);
});
});
Expand All @@ -246,20 +246,20 @@ export function getAllKeys(localStorage: LocalStorage): string[] {
}

function fieldsAreEqual(field1: Field, field2: Field): void {
assert(
fieldViewToString(field2, -3, -3, 3, 3) ===
`.......
.xxx...
..422x.
.x202..
.x324x.
...xxx.
......F
`,
assertFieldViewEquals(
fieldViewToString(field2, -3, -3, 3, 3),
`.......
.x.xx..
.x423..
.x202x.
..224x.
..x.xx.
......F`,
);
assert(
fieldViewToString(field1, -3, -3, 3, 3) ===
fieldViewToString(field2, -3, -3, 3, 3),

assertFieldViewEquals(
fieldViewToString(field1, -3, -3, 3, 3),
fieldViewToString(field2, -3, -3, 3, 3),
);

assert(field1.fieldName === field2.fieldName);
Expand Down Expand Up @@ -326,7 +326,7 @@ test("Chunk should get cell", () => {
});
});

test.only("We should be able to chord flag, chord open and chord open should also chord flag", () => {
test("should be able to chord flag, chord open and chord open should also chord flag", () => {
const field = new Field(0.08, 1, "test1", "testSeed");

field.setCell(4, 1, { isMine: true });
Expand Down Expand Up @@ -448,7 +448,10 @@ function fieldViewToString(
return map.trim();
}

function assertFieldViewEquals(got: string, want: string): void {
function assertFieldViewEquals(
got: string,
want: string,
): asserts got is typeof want {
if (got !== want) {
expect(got, `Field is not the same.\nGot:\n${got}\n\nWant:\n${want}`).toBe(
want,
Expand Down
4 changes: 0 additions & 4 deletions src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ export class Field extends PIXI.EventEmitter {
return output;
}

private isEligibleToOpen(cell: Cell) {
// returns a bool, whether this cell can be opened
//if(this.gameOver) return false;
}

private setSafeCells(x0: number, y0: number) {
// initiate the field with a circle of cells that aren't mines
Expand Down

0 comments on commit 509357a

Please sign in to comment.