diff --git a/development/code/LogicAdapter.ts b/development/code/LogicAdapter.ts index a5f4aa6a..1e3ce814 100644 --- a/development/code/LogicAdapter.ts +++ b/development/code/LogicAdapter.ts @@ -13,6 +13,7 @@ import { moveElementOnBoard, spawnItemElementOnBoard, spawnPieceElementOnBoard, + highlightLastMove, } from './ui/BoardManager'; import { renderPlayersInformation, renderNewRule } from './ui/Screen'; @@ -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, diff --git a/development/code/logic/PieceLogic.ts b/development/code/logic/PieceLogic.ts index 5eb940cb..01c64e4d 100644 --- a/development/code/logic/PieceLogic.ts +++ b/development/code/logic/PieceLogic.ts @@ -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; diff --git a/development/code/logic/pieces/Bishop.test.ts b/development/code/logic/pieces/Bishop.test.ts index a31b350d..d7ee09cd 100644 --- a/development/code/logic/pieces/Bishop.test.ts +++ b/development/code/logic/pieces/Bishop.test.ts @@ -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', () => { @@ -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); + }); +}); diff --git a/development/code/logic/pieces/King.test.ts b/development/code/logic/pieces/King.test.ts index c3055d52..8330d276 100644 --- a/development/code/logic/pieces/King.test.ts +++ b/development/code/logic/pieces/King.test.ts @@ -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', () => { @@ -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); + }); +}); + diff --git a/development/code/logic/pieces/Knight.test.ts b/development/code/logic/pieces/Knight.test.ts index db7ec70b..a1360a58 100644 --- a/development/code/logic/pieces/Knight.test.ts +++ b/development/code/logic/pieces/Knight.test.ts @@ -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', () => { @@ -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, @@ -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); + }); +}); diff --git a/development/code/logic/pieces/Pawn.test.ts b/development/code/logic/pieces/Pawn.test.ts index b2d61ceb..4e77658e 100644 --- a/development/code/logic/pieces/Pawn.test.ts +++ b/development/code/logic/pieces/Pawn.test.ts @@ -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 = { @@ -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); + }); +}); + diff --git a/development/code/logic/pieces/Queen.test.ts b/development/code/logic/pieces/Queen.test.ts index 0cf4ef3c..51a6083b 100644 --- a/development/code/logic/pieces/Queen.test.ts +++ b/development/code/logic/pieces/Queen.test.ts @@ -1,13 +1,30 @@ import { game } from '../../Game'; -import { OVERWORLD_BOARD_ID } from '../Constants'; +import { HEAVEN_BOARD_ID, HELL_BOARD_ID, OVERWORLD_BOARD_ID } from '../Constants'; +import { onPlayerAction } from '../PieceLogic'; import { Player, PlayerColors } from '../Players'; import { Position } from './PiecesUtilities'; import { Queen } from './Queen'; - -jest.mock('../../ui/BoardManager.ts', () => ({})); -jest.mock('../../ui/Screen.ts', () => ({})); +import { Knight } from './Knight'; 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 Queen movement', () => { @@ -40,3 +57,52 @@ describe('Piece movements', () => { expect(validMoves).not.toContainEqual(invalidPosition); }); }); + +describe('Piece killing', () => { + test ('Validating Queen killing', () => { + const initialKillerPosition: Position = { + coordinates: [2, 2], + boardId: OVERWORLD_BOARD_ID, + }; + const killerQueen = new Queen(initialKillerPosition, whitePlayer); + + const victimPosition: Position = { + coordinates: [2, 5], + boardId: OVERWORLD_BOARD_ID, + }; + const victimPiece = new Knight(victimPosition, blackPlayer); + + + game.initialize(); + game.setPieces([killerQueen,victimPiece]); + onPlayerAction(killerQueen,victimPiece); + + const victimPieceBoardId = victimPiece.position.boardId; + expect(victimPieceBoardId).toEqual(HEAVEN_BOARD_ID); + + let killerNewCoordinates = killerQueen.position.coordinates; + expect(killerNewCoordinates).toEqual(victimPosition.coordinates); + + + // Diagonal kill + const otherVictimPosition: Position = { + coordinates: [6, 6], + boardId: OVERWORLD_BOARD_ID, + }; + const otherVictimPiece = new Queen(otherVictimPosition, blackPlayer); + otherVictimPiece.hasKilled = true; + killerQueen.position = initialKillerPosition; + + game.setPieces([killerQueen, otherVictimPiece]); + onPlayerAction(killerQueen, otherVictimPiece); + + const otherVictimPieceBoardId = otherVictimPiece.position.boardId; + expect(otherVictimPieceBoardId).toEqual(HELL_BOARD_ID); + + killerNewCoordinates = killerQueen.position.coordinates; + expect(killerNewCoordinates).toEqual(otherVictimPosition.coordinates); + + const playerXP = killerQueen.player.xp; + expect(playerXP).toBeGreaterThan(0); + }); +}); diff --git a/development/code/logic/pieces/Rook.test.ts b/development/code/logic/pieces/Rook.test.ts index b3647a7c..3c0a2103 100644 --- a/development/code/logic/pieces/Rook.test.ts +++ b/development/code/logic/pieces/Rook.test.ts @@ -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 { Position } from './PiecesUtilities'; import { Rook } from './Rook'; - -jest.mock('../../ui/BoardManager.ts', () => ({})); -jest.mock('../../ui/Screen.ts', () => ({})); +import { Pawn } from './Pawn'; 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 Rook movement', () => { @@ -33,3 +50,32 @@ describe('Piece movements', () => { expect(validMoves).not.toContainEqual(invalidPosition); }); }); + +describe('Piece killing', () => { + test ('Validating Rook killing', () => { + const initialKillerPosition: Position = { + coordinates: [3, 3], + boardId: OVERWORLD_BOARD_ID, + }; + const killerRook = new Rook(initialKillerPosition, whitePlayer); + + const victimPosition: Position = { + coordinates: [3, 5], + boardId: OVERWORLD_BOARD_ID, + }; + const victimPiece = new Pawn(victimPosition, blackPlayer); + + game.initialize(); + game.setPieces([killerRook, victimPiece]); + onPlayerAction(killerRook, victimPiece); + + const victimPieceBoardId = victimPiece.position.boardId; + expect(victimPieceBoardId).toEqual(HEAVEN_BOARD_ID); + + const killerNewCoordinates = killerRook.position.coordinates; + expect(killerNewCoordinates).toEqual(victimPosition.coordinates); + + const playerXP = killerRook.player.xp; + expect(playerXP).toBeGreaterThan(0); + }); +}); diff --git a/development/code/ui/BoardManager.ts b/development/code/ui/BoardManager.ts index fb28a6f4..18f7eab6 100644 --- a/development/code/ui/BoardManager.ts +++ b/development/code/ui/BoardManager.ts @@ -190,3 +190,18 @@ export function highlightSquare(targetElement: HTMLElement, shouldAddHighlight: } } } + +export 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); +} +