Skip to content

Commit

Permalink
Merge pull request #200 from Ido-Barnea/111-add-double-queen-piece
Browse files Browse the repository at this point in the history
Added `Double Queen` piece
  • Loading branch information
Ido-Barnea authored Feb 20, 2024
2 parents b08aaea + 083b34c commit 1e05bb3
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 50 deletions.
55 changes: 38 additions & 17 deletions development/code/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ let isFriendlyFire = false;
let isPieceKilled = false;
let wasItemPlacedThisTurn = false;
let fellOffTheBoardPiece: Piece | undefined;
let actionsLeft = 0;
let isGameFinished = false;

function initializeGame() {
Expand All @@ -72,12 +73,36 @@ function initializeGame() {
});
}

function endTurn() {
function endMove(canRecover = true) {
rulesManager.activeRules.forEach((rule) => {
rule.trigger();
});

Logger.logMessages();

// element.remove() is scheduled to run in the next event sycle while alert() runs immedietely.
// To make sure the element is removed before displaying the winning alert, we need to add
// a small delay before displaying the alert.
setTimeout(() => {
if (isGameFinished) {
const livingKingPlayer = pieces.filter(piece => piece instanceof King)[0].player;

showGameEndAlert(livingKingPlayer.color);
window.location.reload();
}
}, 10);

resetVariables();


actionsLeft--;
if (!canRecover) actionsLeft = 0;
if (actionsLeft !== 0) return;

endTurn();
}

function endTurn() {
updatePlayerDetails();

currentPlayerIndex = currentPlayerIndex + 1 < players.length ? currentPlayerIndex + 1 : 0;
Expand All @@ -87,33 +112,19 @@ function endTurn() {
roundCounter++;
}

Logger.logMessages();

players.forEach((player) => {
switchInventory(player);
});

renderScreen();
wasItemPlacedThisTurn = false;

// element.remove() is scheduled to run in the next event sycle while alert() runs immedietely.
// To make sure the element is removed before displaying the winning alert, we need to add
// a small delay before displaying the alert.
setTimeout(() => {
if (isGameFinished) {
const livingKingPlayer = pieces.filter(piece => piece instanceof King)[0].player;

showGameEndAlert(livingKingPlayer.color);
window.location.reload();
}
}, 10);
}

function resetVariables() {
isCastling = false;
isFriendlyFire = false;
isPieceKilled = false;
fellOffTheBoardPiece = undefined;
wasItemPlacedThisTurn = false;

pieces.forEach((piece) => {
if (piece.player !== getCurrentPlayer() && piece instanceof Pawn) {
Expand Down Expand Up @@ -212,6 +223,14 @@ function setFellOffTheBoardPiece(_fellOffTheBoardPiece: Piece | undefined) {
fellOffTheBoardPiece = _fellOffTheBoardPiece;
}

function getActionsLeft() {
return actionsLeft;
}

function setActionsLeft(actions: number) {
actionsLeft = actions;
}

function endGame() {
isGameFinished = true;
}
Expand All @@ -227,7 +246,9 @@ function getWasItemPlacedThisTurn(){
export const game = {
initialize: initializeGame,
end: endGame,
endTurn,
endMove,
getActionsLeft,
setActionsLeft,
getCurrentPlayer,
switchIsCastling,
getPlayers,
Expand Down
25 changes: 14 additions & 11 deletions development/code/logic/PieceLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ function revertPieceMoveOnBoard(piece: Piece) {
movePieceOnBoard(piece, piece.position);
}

export function onPieceFellOffTheBoard(draggedPiece: Piece) {
draggedPiece.position.boardId = VOID_BOARD_ID;
killPieceByGame(draggedPiece, 'gravity');
game.setFellOffTheBoardPiece(draggedPiece);
game.endMove(false);
}

export function onPlayerAction(
draggedPiece: Piece,
target: Piece | Square | Item,
Expand All @@ -91,6 +98,9 @@ export function onPlayerAction(
// If it did, return.
if (pieceBoard !== newPieceBoard) return;

if (game.getActionsLeft() === 0) {
game.setActionsLeft(draggedPiece.actions);
}

if (target instanceof Piece) {
onActionAttackMove(draggedPiece, target);
Expand All @@ -110,13 +120,6 @@ function onActionNonAttackMove(
onActionPieceToSquare(draggedPiece, targetSquare);
}

export function onPieceFellOffTheBoard(draggedPiece: Piece) {
draggedPiece.position.boardId = VOID_BOARD_ID;
killPieceByGame(draggedPiece, 'gravity');
game.setFellOffTheBoardPiece(draggedPiece);
game.endTurn();
}

function onActionAttackMove(
draggedPiece: Piece,
targetPiece: Piece,
Expand Down Expand Up @@ -206,7 +209,7 @@ function move(
};

draggedPiece.hasMoved = true;
if (shouldEndTurn) game.endTurn();
if (shouldEndTurn) game.endMove();
}

function killPieceByAnotherPiece(
Expand All @@ -215,7 +218,7 @@ function killPieceByAnotherPiece(
): boolean {
if (targetPiece.equipedItem instanceof Shield) {
revertPieceMoveOnBoard(draggedPiece);
game.endTurn();
game.endMove();
return false;
}

Expand All @@ -235,7 +238,7 @@ function killPieceByGame(
killCause: string,
): boolean {
if (targetPiece.equipedItem instanceof Shield) {
game.endTurn();
game.endMove();
return false;
}

Expand Down Expand Up @@ -329,7 +332,7 @@ function pieceMovedOnTrap(
destroyItemOnBoard(trap);

if (!isSuccessfulKill) return;
game.endTurn();
game.endMove(false);
}

export function pieceMovedOnPiggyBank(
Expand Down
70 changes: 70 additions & 0 deletions development/code/logic/pieces/DoubleQueen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { doubleQueenResource } from '../../ui/Resources';
import { Piece } from './Piece';
import { Player, PlayerColors } from '../Players';
import { Position } from './PiecesUtilities';
import { getPieceByPosition } from '../Utilities';

export class DoubleQueen extends Piece {
constructor(position: Position, player: Player) {
const icon = player.color === PlayerColors.WHITE
? '♕x2'
: '♛x2';

super(doubleQueenResource, icon, 'Double Queen', player, position);

this.actions = 2;
this.price = 5;
}

getLegalMoves(): Array<Position> {
const validMoves: Array<Position> = [];
const currentCoordinates = this.position.coordinates;

// Iterate over all possible directions for the queen
const directions = [
{ deltaX: 1, deltaY: 0 },
{ deltaX: -1, deltaY: 0 },
{ deltaX: 0, deltaY: 1 },
{ deltaX: 0, deltaY: -1 },
{ deltaX: 1, deltaY: 1 },
{ deltaX: -1, deltaY: -1 },
{ deltaX: 1, deltaY: -1 },
{ deltaX: -1, deltaY: 1 },
];

for (const direction of directions) {
let stepX = direction.deltaX;
let stepY = direction.deltaY;

// Iterate until the edge of the board
while (true) {
const nextX = currentCoordinates[0] + stepX;
const nextY = currentCoordinates[1] + stepY;

// Check if the next position is within the board boundaries
if (nextX < 0 || nextX >= 8 || nextY < 0 || nextY >= 8) {
break;
}

const nextPosition: Position = {
coordinates: [nextX, nextY],
boardId: this.position.boardId,
};

// Add the position to the list of valid moves
validMoves.push(nextPosition);

// If the move encounters another piece, stop iterating in this direction
if (getPieceByPosition(nextPosition)) {
break;
}

// Move further in the current direction
stepX += direction.deltaX;
stepY += direction.deltaY;
}
}

return validMoves;
}
}
18 changes: 9 additions & 9 deletions development/code/logic/pieces/Piece.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Player } from '../Players';
import { Item } from '../items/Items';
import { PieceType, Position, Square } from './PiecesUtilities';
import { Position, Square } from './PiecesUtilities';

export class Piece implements PieceType {
export class Piece {
resource: string;
pieceIcon: string;
name: string;
player: Player;
position: Position;
upgrades: Array<Piece>;
actions: number;
price: number;
upgrades: Array<new (position: Position, player: Player) => Piece>;
equipedItem: Item | undefined;
hasMoved: boolean;
killCount: number;
Expand All @@ -20,18 +21,17 @@ export class Piece implements PieceType {
name: string,
player: Player,
position: Position,
upgrades: Array<Piece> = [],
price = 1,
equipedItem: Item | undefined = undefined,
) {
this.resource = resource;
this.pieceIcon = pieceIcon;
this.name = name;
this.player = player;
this.position = position;
this.upgrades = upgrades;
this.price = price;
this.equipedItem = equipedItem;

this.actions = 1;
this.price = 1;
this.upgrades = [];
this.equipedItem = undefined;
this.hasMoved = false;
this.killCount = 0;
}
Expand Down
12 changes: 0 additions & 12 deletions development/code/logic/pieces/PiecesUtilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Piece } from './Piece';
import { Player } from '../Players';
import { Item } from '../items/Items';
import { comparePositions } from '../Utilities';
import { game } from '../../Game';
Expand All @@ -14,17 +13,6 @@ export type Square = {
occupent?: Piece;
};

export interface PieceType {
resource: string;
pieceIcon: string;
name: string;
player: Player;
position: Position;
upgrades: Array<Piece>;
hasMoved: boolean;
killCount: number;
}

export function getItemByPosition(
position: Position,
): Item | undefined {
Expand Down
3 changes: 3 additions & 0 deletions development/code/logic/pieces/Queen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Piece } from './Piece';
import { Player, PlayerColors } from '../Players';
import { Position } from './PiecesUtilities';
import { getPieceByPosition } from '../Utilities';
import { DoubleQueen } from './DoubleQueen';

export class Queen extends Piece {
constructor(position: Position, player: Player) {
Expand All @@ -11,6 +12,8 @@ export class Queen extends Piece {
: '♛';

super(queenResource, icon, 'Queen', player, position);

this.upgrades = [DoubleQueen];
}

getLegalMoves(): Array<Position> {
Expand Down
4 changes: 3 additions & 1 deletion development/code/ui/Resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export const knightResource =
'<svg class="untargetable" height="1em" viewBox="0 0 448 512"><path class="untargetable outline" d="M96 48L82.7 61.3C70.7 73.3 64 89.5 64 106.5V238.9c0 10.7 5.3 20.7 14.2 26.6l10.6 7c14.3 9.6 32.7 10.7 48.1 3l3.2-1.6c2.6-1.3 5-2.8 7.3-4.5l49.4-37c6.6-5 15.7-5 22.3 0c10.2 7.7 9.9 23.1-.7 30.3L90.4 350C73.9 361.3 64 380 64 400H384l28.9-159c2.1-11.3 3.1-22.8 3.1-34.3V192C416 86 330 0 224 0H83.8C72.9 0 64 8.9 64 19.8c0 7.5 4.2 14.3 10.9 17.7L96 48zm24 68a20 20 0 1 1 40 0 20 20 0 1 1 -40 0zM22.6 473.4c-4.2 4.2-6.6 10-6.6 16C16 501.9 26.1 512 38.6 512H409.4c12.5 0 22.6-10.1 22.6-22.6c0-6-2.4-11.8-6.6-16L384 432H64L22.6 473.4z"/></svg>';
export const pawnResource =
'<svg class="untargetable" height="1em" viewBox="0 0 320 512"><path class="untargetable outline" d="M215.5 224c29.2-18.4 48.5-50.9 48.5-88c0-57.4-46.6-104-104-104S56 78.6 56 136c0 37.1 19.4 69.6 48.5 88H96c-17.7 0-32 14.3-32 32c0 16.5 12.5 30 28.5 31.8L80 400H240L227.5 287.8c16-1.8 28.5-15.3 28.5-31.8c0-17.7-14.3-32-32-32h-8.5zM22.6 473.4c-4.2 4.2-6.6 10-6.6 16C16 501.9 26.1 512 38.6 512H281.4c12.5 0 22.6-10.1 22.6-22.6c0-6-2.4-11.8-6.6-16L256 432H64L22.6 473.4z"/></svg>';

export const doubleQueenResource =
'<svg class="untargetable" viewBox="0 0 512 512"><path class="untargetable outline" d="M 257.539 -120.046 C 300.648 -120.046 327.591 -73.379 306.036 -36.046 C 296.033 -18.72 277.546 -8.046 257.539 -8.046 C 214.43 -8.046 187.487 -54.713 209.042 -92.046 C 219.045 -109.372 237.532 -120.046 257.539 -120.046 Z M 135.639 23.754 C 138.939 10.754 150.639 -0.046 165.839 -0.046 C 178.139 -0.046 188.439 7.154 193.539 16.954 C 205.539 40.154 229.739 55.954 257.539 55.954 C 285.339 55.954 309.539 40.154 321.539 16.954 C 326.639 7.154 336.939 -0.046 349.239 -0.046 C 364.539 -0.046 376.239 10.754 379.439 23.754 C 386.439 51.554 411.639 72.054 441.539 72.054 C 452.339 72.054 462.539 69.354 471.339 64.654 C 479.739 60.254 490.239 60.154 498.939 65.554 C 511.939 73.554 516.039 90.554 508.139 103.554 L 401.239 279.954 L 385.539 279.954 L 345.139 279.954 L 169.939 279.954 L 129.539 279.954 L 113.839 279.954 L 6.939 103.554 C -0.961 90.554 3.139 73.554 16.139 65.554 C 24.839 60.254 35.339 60.254 43.739 64.654 C 52.639 69.354 62.739 72.054 73.539 72.054 C 103.439 72.054 128.639 51.554 135.639 23.754 Z M 257.539 103.954 Z" transform="matrix(1, 0, 0, 1, 0, -2.842170943040401e-14)"/><path class="untargetable outline" d="M 256.78 230.833 C 299.889 230.833 326.832 277.5 305.277 314.833 C 295.274 332.159 276.787 342.833 256.78 342.833 C 213.671 342.833 186.728 296.166 208.283 258.833 C 218.286 241.507 236.773 230.833 256.78 230.833 Z M 134.88 374.633 C 138.18 361.633 149.88 350.833 165.08 350.833 C 177.38 350.833 187.68 358.033 192.78 367.833 C 204.78 391.033 228.98 406.833 256.78 406.833 C 284.58 406.833 308.78 391.033 320.78 367.833 C 325.88 358.033 336.18 350.833 348.48 350.833 C 363.78 350.833 375.48 361.633 378.68 374.633 C 385.68 402.433 410.88 422.933 440.78 422.933 C 451.58 422.933 461.78 420.233 470.58 415.533 C 478.98 411.133 489.48 411.033 498.18 416.433 C 511.18 424.433 515.28 441.433 507.38 454.433 L 400.48 630.833 L 384.78 630.833 L 344.38 630.833 L 169.18 630.833 L 128.78 630.833 L 113.08 630.833 L 6.179 454.433 C -1.721 441.433 2.379 424.433 15.379 416.433 C 24.079 411.133 34.579 411.133 42.979 415.533 C 51.88 420.233 61.98 422.933 72.78 422.933 C 102.68 422.933 127.88 402.433 134.88 374.633 Z M 256.78 454.833 Z" style="transform-origin: 256.78px 430.833px;" transform="matrix(-0.99991697073, -0.01289900206, 0.01289900206, -0.99991697073, 0.000022789518, -0.000060497251)"/></svg>';

export const piggyBankResource =
'<svg class="untargetable" height="16" width="18" viewBox="0 0 576 512"><path class="untargetable outline" fill="HotPink" d="M400 96l0 .7c-5.3-.4-10.6-.7-16-.7H256c-16.5 0-32.5 2.1-47.8 6c-.1-2-.2-4-.2-6c0-53 43-96 96-96s96 43 96 96zm-16 32c3.5 0 7 .1 10.4 .3c4.2 .3 8.4 .7 12.6 1.3C424.6 109.1 450.8 96 480 96h11.5c10.4 0 18 9.8 15.5 19.9l-13.8 55.2c15.8 14.8 28.7 32.8 37.5 52.9H544c17.7 0 32 14.3 32 32v96c0 17.7-14.3 32-32 32H512c-9.1 12.1-19.9 22.9-32 32v64c0 17.7-14.3 32-32 32H416c-17.7 0-32-14.3-32-32V448H256v32c0 17.7-14.3 32-32 32H192c-17.7 0-32-14.3-32-32V416c-34.9-26.2-58.7-66.3-63.2-112H68c-37.6 0-68-30.4-68-68s30.4-68 68-68h4c13.3 0 24 10.7 24 24s-10.7 24-24 24H68c-11 0-20 9-20 20s9 20 20 20H99.2c12.1-59.8 57.7-107.5 116.3-122.8c12.9-3.4 26.5-5.2 40.5-5.2H384zm64 136a24 24 0 1 0 -48 0 24 24 0 1 0 48 0z"/></svg>';
export const trapResource =
Expand Down

0 comments on commit 1e05bb3

Please sign in to comment.