Skip to content

Commit

Permalink
Merge pull request #158 from Ido-Barnea/154-write-tests-for-killing-o…
Browse files Browse the repository at this point in the history
…ther-pieces

Write tests for killing other pieces
  • Loading branch information
Monkey-bored authored Jan 23, 2024
2 parents 4fab1e5 + 6c5eb1a commit 0a52a40
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 41 deletions.
16 changes: 1 addition & 15 deletions development/code/LogicAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
moveElementOnBoard,
spawnItemElementOnBoard,
spawnPieceElementOnBoard,
highlightLastMove,
} from './ui/BoardManager';
import { renderPlayersInformation, renderNewRule } from './ui/Screen';

Expand Down Expand Up @@ -112,21 +113,6 @@ function highlightLegalMoves(
}
}

function highlightLastMove(
originSquareElement: HTMLElement,
targetSquareElement: HTMLElement,
boardId: string,
) {
const allSquareElements = getAllSquareElements(boardId);
for (const squareElement of allSquareElements) {
highlightSquare(squareElement, false, false);
}

highlightSquare(originSquareElement, true, false);
highlightSquare(targetSquareElement, true, false);
}


export function onPieceSelected(
pieceElement: HTMLElement,
boardId: string,
Expand Down
2 changes: 1 addition & 1 deletion development/code/logic/PieceLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function handleOverworldKill(
) {
targetPiece.position = targetPosition;

if (targetPiece.hasKilled) {
if (targetPiece.hasKilled || targetPiece instanceof King) {
targetPiece.position.boardId = HELL_BOARD_ID;
} else {
targetPiece.position.boardId = HEAVEN_BOARD_ID;
Expand Down
55 changes: 51 additions & 4 deletions development/code/logic/pieces/Bishop.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { game } from '../../Game';
import { OVERWORLD_BOARD_ID } from '../Constants';
import { HELL_BOARD_ID, OVERWORLD_BOARD_ID } from '../Constants';
import { onPlayerAction } from '../PieceLogic';
import { Player, PlayerColors } from '../Players';
import { Bishop } from './Bishop';
import { Position } from './PiecesUtilities';

jest.mock('../../ui/BoardManager.ts', () => ({}));
jest.mock('../../ui/Screen.ts', () => ({}));
import { Queen } from './Queen';

const whitePlayer = new Player(PlayerColors.WHITE);
const blackPlayer = new Player(PlayerColors.BLACK);

jest.mock('../../ui/BoardManager.ts', () => ({
destroyElementOnBoard: jest.fn(),
moveElementOnBoard: jest.fn(),
spawnPieceElementOnBoard: jest.fn(),
getSquareElementById: jest.fn(),
getAllSquareElements: jest.fn(),
highlightLastMove: jest.fn(),
}));
jest.mock('../../ui/Screen.ts', () => ({
renderNewRule: jest.fn(),
renderPlayersInformation: jest.fn(),
}));
jest.mock('../../ui/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

describe('Piece movements', () => {
test('Validating Bishop movement', () => {
Expand All @@ -33,3 +50,33 @@ describe('Piece movements', () => {
expect(validMoves).not.toContainEqual(invalidPosition);
});
});

describe('Piece killing', () => {
test ('Validating Bishop killing', () => {
const initialKillerPosition: Position = {
coordinates: [1, 1],
boardId: OVERWORLD_BOARD_ID,
};
const killerBishop = new Bishop(initialKillerPosition, whitePlayer);

const victimPosition: Position = {
coordinates: [5, 5],
boardId: OVERWORLD_BOARD_ID,
};
const victimPiece = new Queen(victimPosition, blackPlayer);
victimPiece.hasKilled = true;

game.initialize();
game.setPieces([killerBishop, victimPiece]);
onPlayerAction(killerBishop, victimPiece);

const victimPieceBoardId = victimPiece.position.boardId;
expect(victimPieceBoardId).toEqual(HELL_BOARD_ID);

const killerNewCoordinates = killerBishop.position.coordinates;
expect(killerNewCoordinates).toEqual(victimPosition.coordinates);

const playerXP = killerBishop.player.xp;
expect(playerXP).toBeGreaterThan(0);
});
});
53 changes: 50 additions & 3 deletions development/code/logic/pieces/King.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { game } from '../../Game';
import { OVERWORLD_BOARD_ID } from '../Constants';
import { HELL_BOARD_ID, OVERWORLD_BOARD_ID } from '../Constants';
import { onPlayerAction } from '../PieceLogic';
import { Player, PlayerColors } from '../Players';
import { King } from './King';
import { Position } from './PiecesUtilities';

jest.mock('../../ui/BoardManager.ts', () => ({}));
jest.mock('../../ui/Screen.ts', () => ({}));

const whitePlayer = new Player(PlayerColors.WHITE);
const blackPlayer = new Player(PlayerColors.BLACK);

jest.mock('../../ui/BoardManager.ts', () => ({
destroyElementOnBoard: jest.fn(),
moveElementOnBoard: jest.fn(),
spawnPieceElementOnBoard: jest.fn(),
getSquareElementById: jest.fn(),
getAllSquareElements: jest.fn(),
highlightLastMove: jest.fn(),
}));
jest.mock('../../ui/Screen.ts', () => ({
renderNewRule: jest.fn(),
renderPlayersInformation: jest.fn(),
}));
jest.mock('../../ui/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

describe('Piece movements', () => {
test('Validating King movement', () => {
Expand Down Expand Up @@ -40,3 +57,33 @@ describe('Piece movements', () => {
expect(validMoves).not.toContainEqual(invalidPosition);
});
});

describe('Piece killing', () => {
test('Validating King killing', () => {
const initialKillerPosition: Position = {
coordinates: [3, 4],
boardId: OVERWORLD_BOARD_ID,
};
const killerKing = new King(initialKillerPosition, whitePlayer);

const victimPosition: Position = {
coordinates: [4, 5],
boardId: OVERWORLD_BOARD_ID,
};
const victimPiece = new King(victimPosition, blackPlayer);

game.initialize();
game.setPieces([killerKing, victimPiece]);
onPlayerAction(killerKing, victimPiece);

const victimPieceBoardId = victimPiece.position.boardId;
expect(victimPieceBoardId).toEqual(HELL_BOARD_ID);

const killerNewCoordinates = killerKing.position.coordinates;
expect(killerNewCoordinates).toEqual(victimPosition.coordinates);

const playerXP = killerKing.player.xp;
expect(playerXP).toBeGreaterThan(0);
});
});

72 changes: 67 additions & 5 deletions development/code/logic/pieces/Knight.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { game } from '../../Game';
import { OVERWORLD_BOARD_ID } from '../Constants';
import { HEAVEN_BOARD_ID, OVERWORLD_BOARD_ID } from '../Constants';
import { onPlayerAction } from '../PieceLogic';
import { Player, PlayerColors } from '../Players';
import { Knight } from './Knight';
import { Position } from './PiecesUtilities';

jest.mock('../../ui/BoardManager.ts', () => ({}));
jest.mock('../../ui/Screen.ts', () => ({}));
import { Rook } from './Rook';

const whitePlayer = new Player(PlayerColors.WHITE);
const blackPlayer = new Player(PlayerColors.BLACK);

jest.mock('../../ui/BoardManager.ts', () => ({
destroyElementOnBoard: jest.fn(),
moveElementOnBoard: jest.fn(),
spawnPieceElementOnBoard: jest.fn(),
getSquareElementById: jest.fn(),
getAllSquareElements: jest.fn(),
highlightLastMove: jest.fn(),
}));
jest.mock('../../ui/Screen.ts', () => ({
renderNewRule: jest.fn(),
renderPlayersInformation: jest.fn(),
}));
jest.mock('../../ui/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

describe('Piece movements', () => {
test('Validating Knight movement', () => {
Expand All @@ -24,7 +41,7 @@ describe('Piece movements', () => {
};
let validMoves = knight.getLegalMoves();
expect(validMoves).toContainEqual(newPosition);

const invalidPosition: Position = {
coordinates: [1, 5],
boardId: OVERWORLD_BOARD_ID,
Expand All @@ -33,3 +50,48 @@ describe('Piece movements', () => {
expect(validMoves).not.toContainEqual(invalidPosition);
});
});

describe('Piece killing', () => {
test('Validating Knight killing', () => {
const initialKillerPosition: Position = {
coordinates: [3, 3],
boardId: OVERWORLD_BOARD_ID,
};
const killerKnight = new Knight(initialKillerPosition, whitePlayer);

const victimPosition: Position = {
coordinates: [1, 4],
boardId: OVERWORLD_BOARD_ID,
};
const victimPiece = new Knight(victimPosition, blackPlayer);

game.initialize();
game.setPieces([killerKnight, victimPiece]);
onPlayerAction(killerKnight, victimPiece);

const victimPieceBoardId = victimPiece.position.boardId;
expect(victimPieceBoardId).toEqual(HEAVEN_BOARD_ID);

let killerNewCoordinates = killerKnight.position.coordinates;
expect(killerNewCoordinates).toEqual(victimPosition.coordinates);

const othervictimPosition: Position = {
coordinates: [2, 5],
boardId: OVERWORLD_BOARD_ID,
};
const otherVictimPiece = new Rook(othervictimPosition, blackPlayer);
killerKnight.position = initialKillerPosition;

game.setPieces([killerKnight, otherVictimPiece]);
onPlayerAction(killerKnight, otherVictimPiece);

const otherVictimPieceBoardId = otherVictimPiece.position.boardId;
expect(otherVictimPieceBoardId).toEqual(HEAVEN_BOARD_ID);

killerNewCoordinates = killerKnight.position.coordinates;
expect(killerNewCoordinates).toEqual(othervictimPosition.coordinates);

const playerXP = killerKnight.player.xp;
expect(playerXP).toBeGreaterThan(0);
});
});
84 changes: 79 additions & 5 deletions development/code/logic/pieces/Pawn.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import { game } from '../../Game';
import { OVERWORLD_BOARD_ID } from '../Constants';
import { HEAVEN_BOARD_ID, OVERWORLD_BOARD_ID } from '../Constants';
import { onPlayerAction } from '../PieceLogic';
import { Player, PlayerColors } from '../Players';
import { Pawn } from './Pawn';
import { Position } from './PiecesUtilities';

jest.mock('../../ui/BoardManager.ts', () => ({}));
jest.mock('../../ui/Screen.ts', () => ({}));
import { Position, Square } from './PiecesUtilities';

const whitePlayer = new Player(PlayerColors.WHITE);
const blackPlayer = new Player(PlayerColors.BLACK);

jest.mock('../../ui/BoardManager.ts', () => ({
destroyElementOnBoard: jest.fn(),
moveElementOnBoard: jest.fn(),
spawnPieceElementOnBoard: jest.fn(),
getSquareElementById: jest.fn(),
getAllSquareElements: jest.fn(),
highlightLastMove: jest.fn(),
}));
jest.mock('../../ui/Screen.ts', () => ({
renderNewRule: jest.fn(),
renderPlayersInformation: jest.fn(),
}));
jest.mock('../../ui/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

describe('Piece movements', () => {
test('Validating Pawn movement', () => {
const initialPosition: Position = {
Expand Down Expand Up @@ -79,3 +94,62 @@ describe('Piece movements', () => {
expect(validMoves).not.toContainEqual(invalidPosition);
});
});

describe('Piece killing', () => {
test ('Validating Pawn killing', () => {
const initialKillerPosition: Position = {
coordinates: [4, 4],
boardId: OVERWORLD_BOARD_ID,
};
const killerPawn = new Pawn(initialKillerPosition, whitePlayer);

const victimPosition: Position = {
coordinates: [3, 3],
boardId: OVERWORLD_BOARD_ID,
};
const victimPiece = new Pawn(victimPosition, blackPlayer);

game.initialize();
game.setPieces([killerPawn, victimPiece]);
onPlayerAction(killerPawn, victimPiece);

const victimPieceBoardId = victimPiece.position.boardId;
expect(victimPieceBoardId).toEqual(HEAVEN_BOARD_ID);

let killerNewCoordinates = killerPawn.position.coordinates;
expect(killerNewCoordinates).toEqual(victimPosition.coordinates);

const enPassantVictimPosition: Position = {
coordinates: [2, 3],
boardId: OVERWORLD_BOARD_ID,
};
const enPassantPawn = new Pawn(enPassantVictimPosition, blackPlayer);
enPassantPawn.possibleEnPassantPositions = [
{
coordinates: [2, 2],
boardId: OVERWORLD_BOARD_ID,
},
{
coordinates: [2, 3],
boardId: OVERWORLD_BOARD_ID,
},
];
enPassantPawn.isInitialDoubleStep = true;

const enPassantAttackSquare = enPassantPawn.possibleEnPassantPositions[0];
const enPassantSquare: Square = { position: enPassantAttackSquare };

game.setPieces([killerPawn, enPassantPawn]);
onPlayerAction(killerPawn, enPassantSquare);

const enPassantPawnBoardId = enPassantPawn.position.boardId;
expect(enPassantPawnBoardId).toEqual(HEAVEN_BOARD_ID);

killerNewCoordinates = killerPawn.position.coordinates;
expect(killerNewCoordinates).toEqual(enPassantAttackSquare.coordinates);

const playerXP = killerPawn.player.xp;
expect(playerXP).toBeGreaterThan(0);
});
});

Loading

0 comments on commit 0a52a40

Please sign in to comment.