move rules for the rest of the pieces (rook, bishop, queen, king)
This commit is contained in:
parent
39e1c4a8fe
commit
4764a49a7c
|
|
@ -14,11 +14,11 @@ public class Main {
|
|||
testBoard.populateBoard();
|
||||
System.out.println(testBoard.toString());
|
||||
|
||||
Piece testPiece = new Piece(1,2,false,PieceType.Pawn);
|
||||
System.out.println(testPiece.getType());
|
||||
System.out.println(testPiece.getX());
|
||||
System.out.println(testPiece.getY());
|
||||
System.out.println(testPiece.isWhite());
|
||||
//Piece testPiece = new Piece(1,2,false,PieceType.Pawn);
|
||||
//System.out.println(testPiece.getType());
|
||||
//System.out.println(testPiece.getX());
|
||||
//System.out.println(testPiece.getY());
|
||||
//System.out.println(testPiece.isWhite());
|
||||
|
||||
|
||||
// launches graphical interface :
|
||||
|
|
|
|||
|
|
@ -218,6 +218,14 @@ public class Board {
|
|||
return moveHelper.getPawnMoves();
|
||||
case Knight:
|
||||
return moveHelper.getKnightMoves();
|
||||
case Rook:
|
||||
return moveHelper.getRookMoves();
|
||||
case Bishop:
|
||||
return moveHelper.getBishopMoves();
|
||||
case Queen:
|
||||
return moveHelper.getQueenMoves();
|
||||
case King:
|
||||
return moveHelper.getKingMoves();
|
||||
default:
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,4 +70,119 @@ public class Move {
|
|||
|
||||
return moves;
|
||||
}
|
||||
|
||||
public ArrayList<int[]> getRookMoves() {
|
||||
ArrayList<int[]> moves = new ArrayList<>();
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
int[][] directions = {
|
||||
{1, 0}, {-1, 0}, // Right, Left
|
||||
{0, 1}, {0, -1} // Down, Up
|
||||
};
|
||||
|
||||
for (int[] dir : directions) {
|
||||
int dx = dir[0];
|
||||
int dy = dir[1];
|
||||
int currX = x + dx;
|
||||
int currY = y + dy;
|
||||
|
||||
boolean blocked = false;
|
||||
|
||||
while (!blocked && currX >= 0 && currX < board.getWidth() && currY >= 0 && currY < board.getHeight()) {
|
||||
Piece target = board.getPieceAt(currX, currY);
|
||||
|
||||
if (target == null) {
|
||||
moves.add(new int[]{currX, currY});
|
||||
} else {
|
||||
if (target.isWhite() != isWhite) {
|
||||
moves.add(new int[]{currX, currY}); // capture allowed
|
||||
}
|
||||
blocked = true; // either way, we stop after hitting a piece
|
||||
}
|
||||
|
||||
currX += dx;
|
||||
currY += dy;
|
||||
}
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
public ArrayList<int[]> getBishopMoves() {
|
||||
ArrayList<int[]> moves = new ArrayList<>();
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
int[][] directions = {
|
||||
{1, 1}, {-1, 1}, // Down-right, Down-left
|
||||
{1, -1}, {-1, -1} // Up-right, Up-left
|
||||
};
|
||||
|
||||
for (int[] dir : directions) {
|
||||
int dx = dir[0];
|
||||
int dy = dir[1];
|
||||
int currX = x + dx;
|
||||
int currY = y + dy;
|
||||
|
||||
boolean blocked = false;
|
||||
|
||||
while (!blocked && currX >= 0 && currX < board.getWidth() && currY >= 0 && currY < board.getHeight()) {
|
||||
Piece target = board.getPieceAt(currX, currY);
|
||||
|
||||
if (target == null) {
|
||||
moves.add(new int[]{currX, currY});
|
||||
} else {
|
||||
if (target.isWhite() != isWhite) {
|
||||
moves.add(new int[]{currX, currY}); // capture allowed
|
||||
}
|
||||
blocked = true;
|
||||
}
|
||||
|
||||
currX += dx;
|
||||
currY += dy;
|
||||
}
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
public ArrayList<int[]> getQueenMoves() {
|
||||
ArrayList<int[]> moves = new ArrayList<>();
|
||||
|
||||
// Combine Rook and Bishop moves
|
||||
moves.addAll(getRookMoves());
|
||||
moves.addAll(getBishopMoves());
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
public ArrayList<int[]> getKingMoves() {
|
||||
ArrayList<int[]> moves = new ArrayList<>();
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
int[][] offsets = {
|
||||
{-1, -1}, {-1, 0}, {-1, 1},
|
||||
{0, -1}, {0, 1},
|
||||
{1, -1}, {1, 0}, {1, 1}
|
||||
};
|
||||
|
||||
for (int[] offset : offsets) {
|
||||
int newX = x + offset[0];
|
||||
int newY = y + offset[1];
|
||||
|
||||
if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
|
||||
Piece target = board.getPieceAt(newX, newY);
|
||||
if (target == null || target.isWhite() != isWhite) {
|
||||
moves.add(new int[]{newX, newY});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue