Skip to content

Commit

Permalink
fix constant z diagonal not considered win
Browse files Browse the repository at this point in the history
  • Loading branch information
soryy708 committed May 22, 2023
1 parent 0c661fe commit d9e35f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/front/game/win.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ describe('Win checker unit tests', () => {
});
});

describe('When constant z diagonal of same type', () => {
const board = new AbstractBoard();
const winChecker = new WinChecker(board);
board.stack({ x: 0, y: 0 }, new TestPiece());
board.stack({ x: 1, y: 1 }, new TestPiece());
board.stack({ x: 2, y: 2 }, new TestPiece());
board.stack({ x: 3, y: 3 }, new TestPiece());

const actual = winChecker.getWinner();
expect(actual).toBeTruthy();
});

describe('When internal ascending diagonal of same type', () => {
it('Should return a winner', () => {
const board = new AbstractBoard();
Expand Down
9 changes: 6 additions & 3 deletions src/front/game/win.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export class WinChecker {

private getDiagonals(): Array<PotentialWin> {
return flat([
this.getDiagonalsOfVector(1, 1, 0),
this.getDiagonalsOfVector(1, -1, 0),
this.getDiagonalsOfVector(1, 0, 1),
this.getDiagonalsOfVector(1, 0, -1),
this.getDiagonalsOfVector(0, 1, 1),
Expand All @@ -85,11 +87,12 @@ export class WinChecker {
): Array<PotentialWin> {
const zeroToThree = [0, 1, 2, 3] as const;
return zeroToThree.map((i) => {
const perpendicularX = y !== 0 ? 1 : 0;
const perpendicularY = x !== 0 ? 1 : 0;
const perpendicularX = x === 0 ? 1 : 0;
const perpendicularY = y === 0 ? 1 : 0;
const perpendicularZ = z === 0 ? 1 : 0;
const xStart = (x < 0 ? 3 : 0) + i * perpendicularX;
const yStart = (y < 0 ? 3 : 0) + i * perpendicularY;
const zStart = z < 0 ? 3 : 0;
const zStart = (z < 0 ? 3 : 0) + i * perpendicularZ;
return zeroToThree.map((j) =>
this.board.getCell(
(xStart + j * x) as never,
Expand Down

0 comments on commit d9e35f4

Please sign in to comment.