-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from Ido-Barnea/111-add-double-queen-piece
Added `Double Queen` piece
- Loading branch information
Showing
7 changed files
with
137 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters