Skip to content

Commit

Permalink
[#111] Created DoubleQueen piece class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido-Barnea committed Feb 20, 2024
1 parent e5235c4 commit d35f17c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 21 deletions.
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

0 comments on commit d35f17c

Please sign in to comment.