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 1571db3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 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
17 changes: 10 additions & 7 deletions src/services/game-logic/GameEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ class GameEngine {
};

moveNotResultWithCheck(move: Square, piece: Piece | null, square: Square) {
const potentialMove: Square = { row: move.row, column: move.column };
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 Down Expand Up @@ -77,11 +78,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 +99,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

0 comments on commit 1571db3

Please sign in to comment.