pawn movements

This commit is contained in:
Romain MURPHY 2025-04-12 11:19:05 +02:00
parent 5c36efc43f
commit f5c67f6d81
10 changed files with 354 additions and 17 deletions

View File

@ -0,0 +1,53 @@
package backend;
import java.util.ArrayList;
public class Bishop extends Piece {
public Bishop(int x, int y, boolean isWhite) {
super(x, y, PieceType.Bishop, isWhite);
}
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();
// Initialize 8x8 move grid with false
for (int i = 0; i < 8; i++) {
ArrayList<Boolean> row = new ArrayList<>();
for (int j = 0; j < 8; j++) {
row.add(false);
}
moves.add(row);
}
// Diagonal directions: top-left, top-right, bottom-left, bottom-right
int[][] directions = {
{-1, -1},
{-1, 1},
{1, -1},
{1, 1}
};
for (int[] dir : directions) {
int newY = y + dir[0];
int newX = x + dir[1];
while (newX >= 0 && newX < 8 && newY >= 0 && newY < 8) {
Piece target = board.get(newY).get(newX);
if (target == null) {
moves.get(newY).set(newX, true); // Empty square
} else {
if (target.isWhite() != this.isWhite) {
moves.get(newY).set(newX, true); // Capture opponent
}
break; // Blocked by any piece
}
newY += dir[0];
newX += dir[1];
}
}
return moves;
}
}

View File

@ -45,7 +45,7 @@ public class Board {
} }
public void setPiece(int x, int y, PieceType type, boolean isWhite) { public void setPiece(int x, int y, PieceType type, boolean isWhite) {
Piece piece = new Piece(x,y,type,isWhite); Piece piece = PieceCreation.createPiece(x,y,type,isWhite);
board.get(y).set(x, piece); board.get(y).set(x, piece);
} }
@ -82,14 +82,11 @@ public class Board {
public void cleanBoard() { public void cleanBoard() {
int rows = 8; int rows = 8;
int cols = 8; int cols = 8;
for (int i = 0; i < rows; i++) { for (int x = 0; x < rows; x++) {
ArrayList<Piece> row = new ArrayList<>(); for (int y = 0; y < cols; y++) {
for (int j = 0; j < cols; j++) { this.board.get(y).set(x, null);
row.add(null); // Fill with null
} }
this.board.add(row);
} }
} }
@Override @Override

View File

@ -0,0 +1,50 @@
package backend;
import java.util.ArrayList;
public class King extends Piece {
public King(int x, int y, boolean isWhite) {
super(x, y, PieceType.King, isWhite);
}
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();
// Initialize 8x8 move grid with false
for (int i = 0; i < 8; i++) {
ArrayList<Boolean> row = new ArrayList<>();
for (int j = 0; j < 8; j++) {
row.add(false);
}
moves.add(row);
}
// All 8 directions
int[][] directions = {
{-1, 0}, // up
{1, 0}, // down
{0, -1}, // left
{0, 1}, // right
{-1, -1}, // top-left
{-1, 1}, // top-right
{1, -1}, // bottom-left
{1, 1} // bottom-right
};
for (int[] dir : directions) {
int newY = y + dir[0];
int newX = x + dir[1];
if (newX >= 0 && newX < 8 && newY >= 0 && newY < 8) {
Piece target = board.get(newY).get(newX);
if (target == null || target.isWhite() != this.isWhite) {
moves.get(newY).set(newX, true);
}
}
}
return moves;
}
}

View File

@ -0,0 +1,46 @@
package backend;
import java.util.ArrayList;
public class Knight extends Piece {
public Knight(int x, int y, boolean isWhite) {
super(x, y, PieceType.Knight, isWhite);
}
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();
// Initialize empty move grid
for (int i = 0; i < 8; i++) {
ArrayList<Boolean> row = new ArrayList<>();
for (int j = 0; j < 8; j++) {
row.add(false);
}
moves.add(row);
}
// L-shaped moves: (±2, ±1) and (±1, ±2)
int[][] deltas = {
{-2, -1}, {-2, 1},
{-1, -2}, {-1, 2},
{1, -2}, {1, 2},
{2, -1}, {2, 1}
};
for (int[] delta : deltas) {
int newY = y + delta[0];
int newX = x + delta[1];
if (newX >= 0 && newX < 8 && newY >= 0 && newY < 8) {
Piece target = board.get(newY).get(newX);
if (target == null || target.isWhite() != this.isWhite) {
moves.get(newY).set(newX, true); // Empty or opponent
}
}
}
return moves;
}
}

View File

@ -0,0 +1,50 @@
package backend;
import java.util.ArrayList;
public class Pawn extends Piece {
public Pawn(int x, int y, boolean isWhite) {
super(x, y, PieceType.Pawn, isWhite);
}
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();
for (int i = 0; i < 8; i++) {
ArrayList<Boolean> row = new ArrayList<>();
for (int j = 0; j < 8; j++) {
row.add(false);
}
moves.add(row);
}
int direction = isWhite ? -1 : 1;
int startRow = isWhite ? 6 : 1;
// Move forward
int nextY = y + direction;
if (nextY >= 0 && nextY < 8) {
moves.get(nextY).set(x, true);
// Two steps forward
// int doubleY = y + 2 * direction;
// if (y == startRow && board.get(doubleY).get(x) == null) {
// moves.get(doubleY).set(x, true);
// }
}
// Captures
for (int dx : new int[]{-1, 1}) {
int newX = x + dx;
if (newX >= 0 && newX < 8 && nextY >= 0 && nextY < 8) {
Piece target = board.get(nextY).get(newX);
if (target != null && target.isWhite() != this.isWhite) {
moves.get(nextY).set(newX, true);
}
}
}
return moves;
}
}

View File

@ -1,6 +1,8 @@
package backend; package backend;
public class Piece { import java.util.ArrayList;
public abstract class Piece {
public int x; public int x;
public int y; public int y;
public PieceType type; public PieceType type;
@ -27,6 +29,9 @@ public class Piece {
public boolean isWhite() { public boolean isWhite() {
return this.isWhite; return this.isWhite;
} }
public abstract ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board);
@Override @Override
public String toString() { public String toString() {
return "Piece [x=" + x + ", y=" + y + ", type=" + type + ", isWhite=" + isWhite + "]"; return "Piece [x=" + x + ", y=" + y + ", type=" + type + ", isWhite=" + isWhite + "]";

View File

@ -0,0 +1,15 @@
package backend;
public class PieceCreation {
public static Piece createPiece(int x, int y, PieceType type, boolean isWhite) {
switch (type) {
case Pawn: return new Pawn(x, y, isWhite);
case Rook: return new Rook(x, y, isWhite);
case Knight: return new Knight(x, y, isWhite);
case Bishop: return new Bishop(x, y, isWhite);
case Queen: return new Queen(x, y, isWhite);
case King: return new King(x, y, isWhite);
default: throw new IllegalArgumentException("Unknown piece type: " + type);
}
}
}

View File

@ -28,19 +28,30 @@ public class PossibleMovements {
} }
possibleMoves.add(row); possibleMoves.add(row);
} }
// Pawn movements for white
if (turnColor) { if (turnColor) {
if (type == PieceType.Pawn) { if (type == PieceType.Pawn) {
if (x == 0) { if (y!= 0) {
if (board.get(y).get(x+1) != null) { if (x == 0) {
possibleMoves.get(y).set(x+1, true); if (board.get(y-1).get(x+1) != null) {
possibleMoves.get(y-1).set(x+1, true);
}
}
else if (x==7) {
if (board.get(y-1).get(x-1) != null) {
possibleMoves.get(y-1).set(x-1, true);
}
}
else {
if (board.get(y-1).get(x-1) != null) {
possibleMoves.get(y-1).set(x-1, true);
}
if (board.get(y-1).get(x+1) != null) {
possibleMoves.get(y-1).set(x+1, true);
}
possibleMoves.get(y-1).set(x, true);
} }
} }
if (x==7) {
if (board.get(y).get(x-1) != null) {
possibleMoves.get(y).set(x-1, true);
}
}
for (int xi = 1; xi<7;i++) {
} }
} }
} }

View File

@ -0,0 +1,57 @@
package backend;
import java.util.ArrayList;
public class Queen extends Piece {
public Queen(int x, int y, boolean isWhite) {
super(x, y, PieceType.Queen, isWhite);
}
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();
// Initialize 8x8 move grid with false
for (int i = 0; i < 8; i++) {
ArrayList<Boolean> row = new ArrayList<>();
for (int j = 0; j < 8; j++) {
row.add(false);
}
moves.add(row);
}
// Queen directions: Rook + Bishop
int[][] directions = {
{-1, 0}, // up
{1, 0}, // down
{0, -1}, // left
{0, 1}, // right
{-1, -1}, // top-left
{-1, 1}, // top-right
{1, -1}, // bottom-left
{1, 1} // bottom-right
};
for (int[] dir : directions) {
int newY = y + dir[0];
int newX = x + dir[1];
while (newX >= 0 && newX < 8 && newY >= 0 && newY < 8) {
Piece target = board.get(newY).get(newX);
if (target == null) {
moves.get(newY).set(newX, true);
} else {
if (target.isWhite() != this.isWhite) {
moves.get(newY).set(newX, true); // Can capture
}
break; // Blocked
}
newY += dir[0];
newX += dir[1];
}
}
return moves;
}
}

View File

@ -0,0 +1,53 @@
package backend;
import java.util.ArrayList;
public class Rook extends Piece {
public Rook(int x, int y, boolean isWhite) {
super(x, y, PieceType.Rook, isWhite);
}
@Override
public ArrayList<ArrayList<Boolean>> getPossibleMoves(ArrayList<ArrayList<Piece>> board) {
ArrayList<ArrayList<Boolean>> moves = new ArrayList<>();
// Initialize empty move grid
for (int i = 0; i < 8; i++) {
ArrayList<Boolean> row = new ArrayList<>();
for (int j = 0; j < 8; j++) {
row.add(false);
}
moves.add(row);
}
// Directions: up, down, left, right
int[][] directions = {
{-1, 0}, // up
{1, 0}, // down
{0, -1}, // left
{0, 1} // right
};
for (int[] dir : directions) {
int newY = y + dir[0];
int newX = x + dir[1];
while (newX >= 0 && newX < 8 && newY >= 0 && newY < 8) {
Piece target = board.get(newY).get(newX);
if (target == null) {
moves.get(newY).set(newX, true); // Empty square
} else {
if (target.isWhite() != this.isWhite) {
moves.get(newY).set(newX, true); // Capture opponent
}
break; // Stop at the first piece (can't jump)
}
newY += dir[0];
newX += dir[1];
}
}
return moves;
}
}