From d35f17cc4ff38b19ebeacabfa57399f0634aa704 Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Tue, 20 Feb 2024 15:19:07 +0200 Subject: [PATCH] [#111] Created DoubleQueen piece class --- development/code/logic/pieces/DoubleQueen.ts | 70 +++++++++++++++++++ development/code/logic/pieces/Piece.ts | 18 ++--- .../code/logic/pieces/PiecesUtilities.ts | 12 ---- development/code/logic/pieces/Queen.ts | 3 + 4 files changed, 82 insertions(+), 21 deletions(-) create mode 100644 development/code/logic/pieces/DoubleQueen.ts diff --git a/development/code/logic/pieces/DoubleQueen.ts b/development/code/logic/pieces/DoubleQueen.ts new file mode 100644 index 00000000..01d828a0 --- /dev/null +++ b/development/code/logic/pieces/DoubleQueen.ts @@ -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 { + const validMoves: Array = []; + 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; + } +} \ No newline at end of file diff --git a/development/code/logic/pieces/Piece.ts b/development/code/logic/pieces/Piece.ts index e79468a7..264ef1f6 100644 --- a/development/code/logic/pieces/Piece.ts +++ b/development/code/logic/pieces/Piece.ts @@ -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; + actions: number; price: number; + upgrades: Array Piece>; equipedItem: Item | undefined; hasMoved: boolean; killCount: number; @@ -20,18 +21,17 @@ export class Piece implements PieceType { name: string, player: Player, position: Position, - upgrades: Array = [], - 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; } diff --git a/development/code/logic/pieces/PiecesUtilities.ts b/development/code/logic/pieces/PiecesUtilities.ts index 19e0859a..191c9945 100644 --- a/development/code/logic/pieces/PiecesUtilities.ts +++ b/development/code/logic/pieces/PiecesUtilities.ts @@ -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'; @@ -14,17 +13,6 @@ export type Square = { occupent?: Piece; }; -export interface PieceType { - resource: string; - pieceIcon: string; - name: string; - player: Player; - position: Position; - upgrades: Array; - hasMoved: boolean; - killCount: number; -} - export function getItemByPosition( position: Position, ): Item | undefined { diff --git a/development/code/logic/pieces/Queen.ts b/development/code/logic/pieces/Queen.ts index b34e1132..a56ffc13 100644 --- a/development/code/logic/pieces/Queen.ts +++ b/development/code/logic/pieces/Queen.ts @@ -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) { @@ -11,6 +12,8 @@ export class Queen extends Piece { : '♛'; super(queenResource, icon, 'Queen', player, position); + + this.upgrades = [DoubleQueen]; } getLegalMoves(): Array {