juste better
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package chess.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
import chess.model.visitor.KingIdentifier;
|
||||
@@ -148,44 +150,39 @@ public class ChessBoard {
|
||||
if (attackPiece.getColor() != player)
|
||||
continue;
|
||||
|
||||
if (getAllowedMoves(attackCoords) != null)
|
||||
if (!getAllowedMoves(attackCoords).isEmpty())
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean[][] getAllowedMoves(Coordinate pieceCoords) {
|
||||
public List<Coordinate> getAllowedMoves(Coordinate pieceCoords) {
|
||||
Piece piece = pieceAt(pieceCoords);
|
||||
if (piece == null)
|
||||
return null;
|
||||
|
||||
Color player = piece.getColor();
|
||||
Color player = piece.getColor();
|
||||
|
||||
List<Coordinate> result = new ArrayList<>();
|
||||
|
||||
boolean[][] result = new boolean[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX];
|
||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||
Move move = new Move(pieceCoords, new Coordinate(i, j));
|
||||
Coordinate destination = new Coordinate(i, j);
|
||||
Move move = new Move(pieceCoords, destination);
|
||||
|
||||
PiecePathChecker piecePathChecker = new PiecePathChecker(this,
|
||||
move);
|
||||
if (!piecePathChecker.isValid())
|
||||
continue;
|
||||
|
||||
|
||||
applyMove(move);
|
||||
result[i][j] = !isKingInCheck(player);
|
||||
if (!isKingInCheck(player))
|
||||
result.add(destination);
|
||||
undoLastMove();
|
||||
}
|
||||
}
|
||||
|
||||
// return null is no valid moves are possible
|
||||
for (int x = 0; x < 8; x++) {
|
||||
for (int y = 0; y < 8; y++) {
|
||||
if (result[x][y])
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,16 +24,4 @@ public class Game {
|
||||
playerTurn = Color.getEnemy(playerTurn);
|
||||
}
|
||||
|
||||
public boolean[][] getAllowedMoves(Coordinate coordinate) {
|
||||
Piece piece = this.board.pieceAt(coordinate);
|
||||
|
||||
if (piece == null)
|
||||
return null;
|
||||
|
||||
if (piece.getColor() != getPlayerTurn())
|
||||
return null;
|
||||
|
||||
return this.board.getAllowedMoves(coordinate);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user