Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
asiaziola committed Feb 14, 2021
1 parent bd1150e commit f51c53b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/controllers/GameController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GameController {

if (isLegalMove) {
this.playSound(this.activeSquare, square);
this.gameEngine.movePiece(this.activeSquare, square);
this.gameEngine.movePiece(this.activeSquare, square, false);
this.boardView.render(this.gameEngine.board);
this.changePlayer();
}
Expand Down
8 changes: 4 additions & 4 deletions src/services/game-logic/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ class Board {
return this.state[square.row][square.column];
}

public checkAllSquares = (f: any) => {
public checkAllSquares = (callback: any) => {
this.state.forEach((row) => {
row.forEach((square) => {
f(square);
callback(square);
});
});
};

public movePiece(location: Square, destination: Square): void {
public movePiece(location: Square, destination: Square, potentialMove: boolean): void {
const piece = this.getPiece(location);
if (piece) this.movesHistory.push([piece, destination]);
if (!potentialMove && piece) this.movesHistory.push([piece, destination]);

this.state[location.row][location.column]?.move(destination);
this.state[destination.row][destination.column] = this.state[location.row][location.column];
Expand Down
27 changes: 16 additions & 11 deletions src/services/game-logic/GameEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ class GameEngine {
.filter((move) => this.moveNotResultWithCheck(move, piece, square));
};

moveNotResultWithCheck(move: Square, piece: Piece | null, square: Square) {
const potentialMove: Square = { row: move.row, column: move.column };
moveNotResultWithCheck(move: Square, piece: Piece | null, square: Square): boolean {
const potentialMove = new Square(move.row, move.column);
const potentialPiece = this.board.getPiece(potentialMove);

this.movePiece(square, potentialMove);
this.movePiece(square, potentialMove, true);
piece!.hasMoved = true;
const isMovePossible = !this.isCheck(piece);

this.movePiece(potentialMove, square);
this.movePiece(potentialMove, square, true);
piece!.hasMoved = false;

if (potentialPiece)
Expand All @@ -40,9 +41,9 @@ class GameEngine {
return isMovePossible;
}

private isCheck(piece: Piece | null): Boolean {
private isCheck(piece: Piece | null): boolean {
const color = piece?.color === Colors.BLACK ? Colors.WHITE : Colors.BLACK;
let currentPlayerKing: Square | undefined = this.getKingForCheck(piece)?.position;
let currentPlayerKing = this.getKingForCheck(piece)?.position;
const isChecked = this.isKingUnderCheck(color, currentPlayerKing);
return isChecked;
}
Expand All @@ -62,7 +63,9 @@ class GameEngine {
getKingForCheck = (piece: Piece | null): King | null => {
let king = null;
this.board.checkAllSquares((square: Piece) => {
if (square && square?.color === piece?.color && square.name === PieceNames.KING) king = square;
if (square && square?.color === piece?.color && square.name === PieceNames.KING) {
king = square;
}
});
return king;
};
Expand All @@ -77,11 +80,11 @@ class GameEngine {
}
}

public movePiece(location: Square, destination: Square): void {
public movePiece(location: Square, destination: Square, potentialMove: boolean): void {
const piece = this.board.getPiece(location);
if (piece) {
this.runSpecialRoutines(piece?.position, destination);
this.board.movePiece(location, destination);
this.board.movePiece(location, destination, potentialMove);
}
}

Expand All @@ -98,12 +101,14 @@ class GameEngine {
if (destination.column - location.column === Constants.KINGSIDE_CASTLING) {
this.board.movePiece(
{ row: location.row, column: Constants.KINGSIDE_ROOK_COLUMN },
{ row: location.row, column: Constants.KINGSIDE_ROOK_DESTINATION_COLUMN }
{ row: location.row, column: Constants.KINGSIDE_ROOK_DESTINATION_COLUMN },
false
);
} else if (destination.column - location.column === Constants.QUEENSIDE_CASTLING) {
this.board.movePiece(
{ row: location.row, column: Constants.QUEENSIDE_ROOK_COLUMN },
{ row: location.row, column: Constants.QUEENSIDE_ROOK_DESTINATION_COLUMN }
{ row: location.row, column: Constants.QUEENSIDE_ROOK_DESTINATION_COLUMN },
false
);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/services/game-logic/pieces/King.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class King extends Piece {
[1, -1]
];
if (!this.hasMoved) positions = [...positions, [0, 2], [0, -2]];
const [row, column] = positions;

const possibleMoves = positions.map(([row, column]) => ({
row: this.position.row + row,
Expand Down

0 comments on commit f51c53b

Please sign in to comment.