From 0c724942dec1e71a45d16c99aaa328bc360520a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asia=20Zio=C5=82a?= Date: Sat, 13 Feb 2021 13:10:14 +0100 Subject: [PATCH] check - tests --- src/services/game-logic/GameEngine.ts | 7 ++-- test/services/game-logic/Board.test.ts | 11 +++++++ test/services/game-logic/GameEngine.test.ts | 36 +++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/services/game-logic/GameEngine.ts b/src/services/game-logic/GameEngine.ts index 75564a3..f2be7ae 100644 --- a/src/services/game-logic/GameEngine.ts +++ b/src/services/game-logic/GameEngine.ts @@ -24,12 +24,11 @@ class GameEngine { .filter((move) => this.moveNotResultWithCheck(move, piece, square)); }; - private moveNotResultWithCheck(move: Square, piece: Piece | null, square: Square) { + moveNotResultWithCheck(move: Square, piece: Piece | null, square: Square) { const potentialMove: Square = { row: move.row, column: move.column }; const potentialPiece = this.board.getPiece(potentialMove); this.movePiece(square, potentialMove); - piece!.hasMoved = true; const isMovePossible = !this.isCheck(piece); this.movePiece(potentialMove, square); @@ -44,11 +43,11 @@ class GameEngine { private isCheck(piece: Piece | null): Boolean { const color = piece?.color === Colors.BLACK ? Colors.WHITE : Colors.BLACK; let currentPlayerKing: Square | undefined = this.getKingForCheck(piece)?.position; - const isChecked = this.findOponentLegalMoves(color, currentPlayerKing); + const isChecked = this.isKingUnderCheck(color, currentPlayerKing); return isChecked; } - findOponentLegalMoves(color: Colors, piecePosition: Square | undefined): boolean { + isKingUnderCheck(color: Colors, piecePosition: Square | undefined): boolean { let checked = false; this.board.checkAllSquares((square: Piece) => { if (square && square.color === color) { diff --git a/test/services/game-logic/Board.test.ts b/test/services/game-logic/Board.test.ts index 373f968..989f2d1 100644 --- a/test/services/game-logic/Board.test.ts +++ b/test/services/game-logic/Board.test.ts @@ -18,3 +18,14 @@ describe('Testing Board.setup()', () => { }); }); }); + +test(`should run function on all squares`, () => { + let arr = []; + const board = new Board(); + + board.checkAllSquares((square) => { + arr.push(square); + }); + + expect(arr.length).toBe(32); +}); diff --git a/test/services/game-logic/GameEngine.test.ts b/test/services/game-logic/GameEngine.test.ts index 6959333..e759e74 100644 --- a/test/services/game-logic/GameEngine.test.ts +++ b/test/services/game-logic/GameEngine.test.ts @@ -1,5 +1,10 @@ import { Square } from '../../../src/models/Square'; import { GameEngine } from '../../../src/services/game-logic/GameEngine'; +import { Colors } from '../../../src/enums'; +import { Board } from '../../../src/services/game-logic/Board'; +import { King } from '../../../src/services/game-logic/pieces/King'; + +const gameEngine = new GameEngine(); describe('Testing GameEngine.getLegalMoves() while playing Pawn', () => { test(`Pawn shall return 2 capturing moves `, () => { @@ -16,6 +21,7 @@ describe('Testing GameEngine.getLegalMoves() while playing Pawn', () => { // Test expect(legalMoves).toEqual(expected); }); + test(`Extra En Passat move while playing white`, () => { // Setup const gameEngine = new GameEngine(); @@ -32,6 +38,7 @@ describe('Testing GameEngine.getLegalMoves() while playing Pawn', () => { // Test expect(legalMoves).toEqual(expected); }); + test(`White En Passat removes Black Pawn`, () => { // Setup const gameEngine = new GameEngine(); @@ -49,3 +56,32 @@ describe('Testing GameEngine.getLegalMoves() while playing Pawn', () => { expect(removedPiece).toBe(null); }); }); + +test(`should target king`, () => { + const piece = gameEngine.board.getPiece(new Square(1, 4)); + const king = gameEngine.getKingForCheck(piece); + const expected = new Square(0, 4); + + expect(king instanceof King).toBe(true); + expect(king.position).toStrictEqual(expected); +}); + +test(`should return true when check`, () => { + const color = Colors.BLACK; + const piece = new King({ column: 7, row: 3 }, Colors.WHITE); + + const isKingChecked = gameEngine.isKingUnderCheck(color, piece.position); + + expect(isKingChecked).toBe(true); +}); + +test(`should verify if oponent move result with check`, () => { + const board = new Board(); + const square = { row: 6, column: 3 }; + const move = new Square(4, 3); + const piece = board.getPiece(square); + + const notCheck = gameEngine.moveNotResultWithCheck(move, piece, square); + + expect(notCheck).toBe(true); +});