Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write tests for killing other pieces #158

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
56 changes: 52 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,34 @@ 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);
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved

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

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

// Checks that the piece got XP for the kills
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved
const playerXp = killerBishop.player.xp;
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved
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;
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved
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;
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved
expect(playerXp).toBeGreaterThan(0);
});
});
86 changes: 81 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,64 @@ 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);

// Killer position is now 3, 3
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved
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;

// The square that the killer moves to
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved
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;
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved
expect(playerXp).toBeGreaterThan(0);
});
});

Loading
Loading