Skip to content

Commit

Permalink
[#25] Wrote pieces movement test file and cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido-Barnea committed Dec 4, 2023
1 parent 8b5f949 commit 996f051
Show file tree
Hide file tree
Showing 17 changed files with 521 additions and 289 deletions.
10 changes: 5 additions & 5 deletions development/src/game.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Logger } from './logger';
import { Logger } from './ui/logger';
import {
initializeEventListeners,
setOnAction,
setOnFallOffTheBoard,
setOnHighlight,
} from './events';
import { highlightSquare, initializeBoards } from './boards';
} from './ui/events';
import { highlightSquare, initializeBoards } from './ui/boards';
import {
onAction,
onFallOffTheBoard,
getCurrentPlayer,
players,
roundCounter,
} from './logic';
import { Rule } from './rules';
} from './logic/logic';
import { Rule } from './logic/rules';

const infoDisplay = document.getElementById('info-display');
const rulesContainer = document.getElementById('rules-container');
Expand Down
21 changes: 21 additions & 0 deletions development/src/logic/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const OVERWORLD_BOARD_ID = 'board-overworld';
export const HELL_BOARD_ID = 'board-hell';
export const HEAVEN_BOARD_ID = 'board-heaven';
export const BOTTOM_NOTATION_ID = 'bottom-notations';
export const LEFT_NOTATION_ID = 'left-notations';

export const OVERWORLD_BOARD_BUTTON_ID = 'board-overworld-button';
export const HELL_BOARD_BUTTON_ID = 'board-hell-button';
export const HEAVEN_BOARD_BUTTON_ID = 'board-heaven-button';

export const LIGHT_OVERWORLD_SQUARE_COLOR = 'beige-background';
export const DARK_OVERWORLD_SQUARE_COLOR = 'brown-background';
export const LIGHT_HELL_SQUARE_COLOR = 'dark-orange-background';
export const DARK_HELL_SQUARE_COLOR = 'dark-red-background';
export const LIGHT_HEAVEN_SQUARE_COLOR = 'water-background';
export const DARK_HEAVEN_SQUARE_COLOR = 'blue-background';
export const GRAY_SQUARE_COLOR = 'light-gray-background';

export const NOTATIONS_LETTERS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
export const NOTATIONS_NUMBERS = ['8', '7', '6', '5', '4', '3', '2', '1'];
export const BOARD_WIDTH = 8;
21 changes: 1 addition & 20 deletions development/src/items.ts → development/src/logic/items.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { spawnItemOnBoard } from './boards';
import { Logger } from './logger';
import { items } from './logic';
import { Logger } from '../ui/logger';
import { Piece, Position } from './pieces';
import { Player } from './players';
import { trapResource } from './resources';

export class Inventory {
items: Array<Item> = [];
Expand Down Expand Up @@ -64,19 +61,3 @@ export class Item implements ItemType {
Logger.logGeneral(`${piece.player.color} used a ${this.name}.`);
};
}

export class Trap extends Item {
constructor(player: Player, position: Position) {
super('trap', trapResource, player, position);
}

apply(piece: Piece) {
Logger.logGeneral(`${this.player.color} ${piece.name}
placed a ${this.name} on ${piece.position.coordinates}.`);

this.position = piece.position;
items.push(this);

spawnItemOnBoard(this);
}
}
25 changes: 6 additions & 19 deletions development/src/logic.ts → development/src/logic/logic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Player } from './players';
import { Player, PlayerColors } from './players';
import {
Piece,
Pawn,
Expand All @@ -10,24 +10,17 @@ import {
Square,
Position,
} from './pieces';
import { Logger } from './logger';
import { Logger } from '../ui/logger';
import {
movePieceOnBoard,
destroyPieceOnBoard,
spawnPieceOnBoard,
OVERWORLD_BOARD_ID,
HELL_BOARD_ID,
HEAVEN_BOARD_ID,
destroyItemOnBoard,
} from './boards';
} from '../ui/boards';
import { activeRules } from './rules';
import { updatePlayersInformation } from './game';
import { updatePlayersInformation } from '../game';
import { Item } from './items';

export enum PlayerColors {
WHITE = 'White',
BLACK = 'Black',
};
import { HEAVEN_BOARD_ID, HELL_BOARD_ID, OVERWORLD_BOARD_ID } from './constants';

const whitePlayer = new Player(PlayerColors.WHITE);
const blackPlayer = new Player(PlayerColors.BLACK);
Expand Down Expand Up @@ -100,12 +93,6 @@ export function switchIsCastling() {
isCastling = !isCastling;
}

export function getPieceByPosition(
position: Position,
): Piece | undefined {
return pieces.find((piece) => comparePositions(position, piece.position));
}

export function getPieceByPositionAndBoard(
position: Position,
): Piece | undefined {
Expand Down Expand Up @@ -267,7 +254,7 @@ function actOnTurnPieceToPiece(draggedPiece: Piece, targetPiece: Piece) {
if (targetPiece.hasKilled) {
targetPiece.position = {
coordinates: targetPiece.position.coordinates,
board: HELL_BOARD_ID,
board: HEAVEN_BOARD_ID,
};
} else {
targetPiece.position = {
Expand Down
205 changes: 205 additions & 0 deletions development/src/logic/pieces.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import { OVERWORLD_BOARD_ID } from './constants';
import { Player, PlayerColors } from './players';
import { Position, Pawn, Bishop, Knight, Rook, Queen, King } from './pieces';

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

function comparePositions(
firstPosition: Position,
secondPosition: Position,
): boolean {
const arePositionsEqual =
firstPosition.coordinates[0] === secondPosition.coordinates[0] &&
firstPosition.coordinates[1] === secondPosition.coordinates[1];
const areBoardsEqual = firstPosition.board === secondPosition.board;

return areBoardsEqual && arePositionsEqual;
}

jest.mock('./logic', () => ({
getCurrentPlayer: () => whitePlayer,
switchIsCastling: jest.fn(),
items: [],
comparePositions: comparePositions,
getPieceByPositionAndBoard: () => undefined,
}));

describe('Piece movements', () => {
test('Validating Pawn movement', () => {
const initialPosition: Position = {
coordinates: [0, 6],
board: OVERWORLD_BOARD_ID,
};
const pawn = new Pawn(initialPosition, whitePlayer);

const singleStepMove: Position = {
coordinates: [0, 5],
board: OVERWORLD_BOARD_ID,
};
const singleStepValidMove = pawn.validateMove({
position: singleStepMove,
});
expect(singleStepValidMove).toEqual(singleStepMove);

pawn.position = initialPosition;
const twoStepsInitialMove: Position = {
coordinates: [0, 4],
board: OVERWORLD_BOARD_ID,
};
const twoStepsInitialValidMove = pawn.validateMove({
position: twoStepsInitialMove,
});
expect(twoStepsInitialValidMove).toEqual(twoStepsInitialMove);

const diagonalAttackMove = new Pawn(
{
coordinates: [1, 5],
board: OVERWORLD_BOARD_ID,
},
blackPlayer,
);
const diagonalAttackValidMove = pawn.validateMove(diagonalAttackMove);
expect(diagonalAttackValidMove).toEqual(diagonalAttackMove.position);

pawn.position = initialPosition;
const invalidPosition: Position = {
coordinates: [0, 3],
board: OVERWORLD_BOARD_ID,
};
const invalidMove = pawn.validateMove({ position: invalidPosition });
expect(invalidMove).toEqual(initialPosition);
});

test('Validating Bishop movement', () => {
const initialPosition: Position = {
coordinates: [2, 7],
board: OVERWORLD_BOARD_ID,
};
const bishop = new Bishop(initialPosition, whitePlayer);

const newPosition: Position = {
coordinates: [4, 5],
board: OVERWORLD_BOARD_ID,
};
const validMove = bishop.validateMove({ position: newPosition });
expect(validMove).toEqual(newPosition);

const invalidPosition: Position = {
coordinates: [2, 2],
board: OVERWORLD_BOARD_ID,
};
const invalidMove = bishop.validateMove({ position: invalidPosition });
expect(invalidMove).toEqual(initialPosition);
});

test('Validating Knight movement', () => {
const initialPosition: Position = {
coordinates: [1, 7],
board: OVERWORLD_BOARD_ID,
};
const knight = new Knight(initialPosition, whitePlayer);

const newPosition: Position = {
coordinates: [2, 5],
board: OVERWORLD_BOARD_ID,
};
const validMove = knight.validateMove({ position: newPosition });
expect(validMove).toEqual(newPosition);

const invalidPosition: Position = {
coordinates: [1, 5],
board: OVERWORLD_BOARD_ID,
};
const invalidMove = knight.validateMove({ position: invalidPosition });
expect(invalidMove).toEqual(initialPosition);
});

test('Validating Rook movement', () => {
const initialPosition: Position = {
coordinates: [0, 7],
board: OVERWORLD_BOARD_ID,
};
const rook = new Rook(initialPosition, whitePlayer);

const newPosition: Position = {
coordinates: [7, 7],
board: OVERWORLD_BOARD_ID,
};
const validMove = rook.validateMove({ position: newPosition });
expect(validMove).toEqual(newPosition);

const invalidPosition: Position = {
coordinates: [1, 6],
board: OVERWORLD_BOARD_ID,
};
const invalidMove = rook.validateMove({ position: invalidPosition });
expect(invalidMove).toEqual(initialPosition);
});

test('Validating Queen movement', () => {
const initialPosition: Position = {
coordinates: [3, 7],
board: OVERWORLD_BOARD_ID,
};
const queen = new Queen(initialPosition, whitePlayer);

const newStraightPosition: Position = {
coordinates: [3, 4],
board: OVERWORLD_BOARD_ID,
};
const validStraightMove = queen.validateMove({
position: newStraightPosition,
});
expect(validStraightMove).toEqual(newStraightPosition);

const newDiagonalPosition: Position = {
coordinates: [6, 4],
board: OVERWORLD_BOARD_ID,
};
const validDiagonalMove = queen.validateMove({
position: newDiagonalPosition,
});
expect(validDiagonalMove).toEqual(newDiagonalPosition);

const invalidPosition: Position = {
coordinates: [0, 0],
board: OVERWORLD_BOARD_ID,
};
const invalidMove = queen.validateMove({ position: invalidPosition });
expect(invalidMove).toEqual(initialPosition);
});

test('Validating King movement', () => {
const initialPosition: Position = {
coordinates: [4, 7],
board: OVERWORLD_BOARD_ID,
};
const king = new King(initialPosition, whitePlayer);

const newStraightPosition: Position = {
coordinates: [4, 6],
board: OVERWORLD_BOARD_ID,
};
const validStraightMove = king.validateMove({
position: newStraightPosition,
});
expect(validStraightMove).toEqual(newStraightPosition);

const newDiagonalPosition: Position = {
coordinates: [5, 8],
board: OVERWORLD_BOARD_ID,
};
const validDiagonalMove = king.validateMove({
position: newDiagonalPosition,
});
expect(validDiagonalMove).toEqual(newDiagonalPosition);

const invalidPosition: Position = {
coordinates: [0, 0],
board: OVERWORLD_BOARD_ID,
};
const invalidMove = king.validateMove({ position: invalidPosition });
expect(invalidMove).toEqual(initialPosition);
});
});
7 changes: 3 additions & 4 deletions development/src/pieces.ts → development/src/logic/pieces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import {
rookResource,
queenResource,
kingResource,
} from './resources';
import { Player } from './players';
} from '../ui/resources';
import { Player, PlayerColors } from './players';
import {
getCurrentPlayer,
switchIsCastling,
getPieceByPositionAndBoard,
items,
comparePositions,
PlayerColors,
} from './logic';
import { OVERWORLD_BOARD_ID } from './boards';
import { Item } from './items';
import { OVERWORLD_BOARD_ID } from './constants';

export type Position = {
coordinates: [number, number],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ type PlayerType = {
inventory: Inventory;
};

export enum PlayerColors {
WHITE = 'White',
BLACK = 'Black',
};

export class Player implements PlayerType {
color: string;
xp: number;
Expand Down
4 changes: 2 additions & 2 deletions development/src/rules.ts → development/src/logic/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
roundCounter,
players,
} from './logic';
import { Logger } from './logger';
import { updateRules } from './game';
import { Logger } from '../ui/logger';
import { updateRules } from '../game';

export class Rule {
id: number;
Expand Down
Loading

0 comments on commit 996f051

Please sign in to comment.