Skip to content

Commit

Permalink
check - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asiaziola committed Feb 13, 2021
1 parent ad6874e commit 0c72494
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/services/game-logic/GameEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions test/services/game-logic/Board.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
36 changes: 36 additions & 0 deletions test/services/game-logic/GameEngine.test.ts
Original file line number Diff line number Diff line change
@@ -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 `, () => {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
});

0 comments on commit 0c72494

Please sign in to comment.