en passant

This commit is contained in:
Lucie 2025-05-01 20:32:26 +02:00
parent 92ee8a45f4
commit 46d7812ed5
1 changed files with 174 additions and 429 deletions

View File

@ -11,6 +11,15 @@ public class Board {
private int y; private int y;
private int turns; private int turns;
private boolean whiteTurn; private boolean whiteTurn;
private int[][] knightMoves = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};
private int[][] kingMoves = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
private int[][] whitePawn = {{6}, {0, -1}, {1, -1}, {-1, -1}, {0, -2}};
private int[][] blackPawn = {{1}, {0, 1}, {1, 1}, {-1, 1}, {0, 2}};
private boolean enPassant;
private int enPassantX;
private int enPassantY;
private int pawnX;
private int pawnY;
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.colNum = colNum; this.colNum = colNum;
@ -20,6 +29,9 @@ public class Board {
y = -1; y = -1;
turns = 0; turns = 0;
whiteTurn = true; whiteTurn = true;
enPassant = false;
pawnX = -1;
pawnY = -1;
} }
public int getWidth() { public int getWidth() {
@ -39,51 +51,35 @@ public class Board {
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
Piece piece = new Piece(isWhite,type,x,y); board[y][x] = new Piece(isWhite, type, x, y);
board[y][x] = piece;
} }
public void populateBoard() { public void populateBoard() {
for (int i = 0; i < lineNum; i++) { for (int i = 0; i < lineNum; i++) {
boolean white = (i > 5);
boolean white = true;
if (i == 0 || i == 1) {
white = false;
}
for (int j = 0; j < colNum; j++) {
if (i == 1 || i == 6) { if (i == 1 || i == 6) {
Piece pawn = new Piece(white, PieceType.Pawn, j, i); for (int j = 0; j < colNum; j++) {
board[i][j] = pawn; setPiece(white, PieceType.Pawn, j, i);
}
} }
else if (i == 0 || i == 7){ else if (i == 0 || i == 7){
if (j == 0 || j == 7) { setPiece(white, PieceType.Rook, 0, i);
Piece rook = new Piece(white, PieceType.Rook, j, i); setPiece(white, PieceType.Rook, 7, i);
board[i][j] = rook; setPiece(white, PieceType.Knight, 1, i);
} setPiece(white, PieceType.Knight, 6, i);
else if (j == 1 || j == 6) { setPiece(white, PieceType.Bishop, 2, i);
Piece knight = new Piece(white, PieceType.Knight, j, i); setPiece(white, PieceType.Bishop, 5, i);
board[i][j] = knight; setPiece(white, PieceType.Queen, 3, i);
} setPiece(white, PieceType.King, 4, i);
else if (j == 2 || j == 5) {
Piece bishop = new Piece(white, PieceType.Bishop, j, i);
board[i][j] = bishop;
}
else if (j == 3) {
Piece queen = new Piece(white, PieceType.Queen, j, i);
board[i][j] = queen;
}
else if (j == 4) {
Piece king = new Piece(white, PieceType.King, j, i);
board[i][j] = king;
}
}
} }
} }
} }
public void cleanBoard() { public void cleanBoard() {
x = -1;
y = -1;
turns = 0;
whiteTurn = true;
for (int i = 0; i < lineNum; i++) { for (int i = 0; i < lineNum; i++) {
for (int j = 0; j < colNum; j++) { for (int j = 0; j < colNum; j++) {
board[i][j] = null; board[i][j] = null;
@ -95,19 +91,16 @@ public class Board {
String str = ""; String str = "";
for (int i = 0; i < lineNum; i++) { for (int i = 0; i < lineNum; i++) {
for (int j = 0; j < colNum; j++) { for (int j = 0; j < colNum; j++) {
if (board[i][j] == null) { if (board[i][j] == null) {
str += " "; str += " ";
} }
else if (board[i][j].isWhite() == true) { else if (board[i][j].isWhite()) {
str += "W"; str += "W" + board[i][j].getType().getSummary();
str += board[i][j].getType().getSummary();
} }
else if (board[i][j].isWhite() == false) { else if (board[i][j].isWhite() == false) {
str += "B"; str += "B" + board[i][j].getType().getSummary();
str += board[i][j].getType().getSummary();
} }
if (j != 7) { if (j != colNum-1) {
str += ","; str += ",";
} }
} }
@ -118,11 +111,10 @@ public class Board {
public ArrayList<Piece> getPieces() { public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>(); ArrayList<Piece> pieces = new ArrayList<>();
for (int col = 0; col < colNum; col ++) { for (int i = 0; i < colNum; i++) {
for (int row = 0; row < lineNum; row ++) { for (int j = 0; j < lineNum; j++) {
Piece Piece = board[row][col]; if (board[j][i] != null) {
if (Piece != null) { pieces.add(board[j][i]);
pieces.add(Piece);
} }
} }
} }
@ -130,383 +122,146 @@ public class Board {
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
boolean prevSelect = true;
if (this.x == -1 && this.y == -1) { if (this.x == -1 && this.y == -1) {
prevSelect = false; if (board[y][x] != null && board[y][x].isWhite() == whiteTurn) {
this.x = x;
this.y = y;
} }
if (prevSelect == true) {
if (this.x == x && this.y == y) {
this.x = -1;
this.y = -1;
}
else if (board[this.y][this.x] == null) {
this.x = -1;
this.y = -1;
} }
else { else {
if (this.x == x && this.y == y || board[this.y][this.x] == null) {
this.x = -1;
this.y = -1;
}
else if (isHighlighted(x,y)) {
if (enPassant == true && board[this.y][this.x].getType() == PieceType.Pawn && x == enPassantX && y == enPassantY) {
board[this.y][this.x].setX(x); board[this.y][this.x].setX(x);
board[this.y][this.x].setY(y); board[this.y][this.x].setY(y);
board[y][x] = board[this.y][this.x]; board[y][x] = board[this.y][this.x];
board[this.y][this.x] = null; board[this.y][this.x] = null;
board[pawnY][pawnX] = null;
enPassant = false;
pawnX = -1;
pawnY = -1;
}
else {
if (board[this.y][this.x].getType() == PieceType.Pawn && (y == this.y+2 || y == this.y-2)) {
enPassant = true;
pawnX = x;
pawnY = y;
}
board[this.y][this.x].setX(x);
board[this.y][this.x].setY(y);
board[y][x] = board[this.y][this.x];
board[this.y][this.x] = null;
}
turns += 1; turns += 1;
this.x = -1; this.x = -1;
this.y = -1; this.y = -1;
if (whiteTurn == true) { whiteTurn = !whiteTurn;
whiteTurn = false;
} }
else {
whiteTurn = true;
} }
} }
public boolean inBoard(int x, int y) {
if (x > -1 && x < colNum && y > -1 && y < lineNum) {
return true;
}
return false;
}
public void linearMoves(ArrayList<int[]> moves, int x, int y, int x2, int y2, boolean white) {
int i = x + x2;
int j = y + y2;
boolean metPiece = false;
while (metPiece == false && i > -1 && inBoard(i,j)) {
if (board[j][i] != null) {
metPiece = true;
if (board[j][i].isWhite() != white) {
moves.add(new int[]{i, j});
}
} }
else { else {
this.x = x; moves.add(new int[]{i, j});
this.y = y; }
i += x2;
j += y2;
} }
} }
public ArrayList<int[]> getMove(PieceType type, boolean isWhite, int x, int y) { public ArrayList<int[]> getMove(PieceType type, boolean isWhite, int x, int y) {
ArrayList<int[]> moves = new ArrayList<>(); ArrayList<int[]> moves = new ArrayList<>();
int i = x; int i;
int j = y; int j;
boolean metPiece; int[][] pieceMoves;
if (type == PieceType.Rook) { if (type == PieceType.Rook || type == PieceType.Queen) {
metPiece = false; linearMoves(moves, x, y, 1, 0, isWhite);
i = x; linearMoves(moves, x, y, -1, 0, isWhite);
j = y; linearMoves(moves, x, y, 0, -1, isWhite);
while (metPiece == false && i+1 < 8) { linearMoves(moves, x, y, 0, 1, isWhite);
if (board[j][i+1] != null) { }
moves.add(new int[] {i+1, j});
metPiece = true; if (type == PieceType.Bishop || type == PieceType.Queen) {
linearMoves(moves, x, y, 1, 1, isWhite);
linearMoves(moves, x, y, -1, 1, isWhite);
linearMoves(moves, x, y, 1, -1, isWhite);
linearMoves(moves, x, y, -1, -1, isWhite);
}
if (type == PieceType.Knight || type == PieceType.King) {
if (type == PieceType.Knight) {
pieceMoves = knightMoves;
} }
else { else {
i += 1; pieceMoves = kingMoves;
}
for (int n = 0; n < pieceMoves.length; n++) {
i = x + pieceMoves[n][0];
j = y + pieceMoves[n][1];
if (inBoard(i,j)) {
if (board[j][i] != null && board[j][i].isWhite() != isWhite) {
moves.add(new int[] {i, j}); moves.add(new int[] {i, j});
} }
} else if (board[j][i] == null){
metPiece = false;
i = x;
j = y;
while (metPiece == false && j+1 < 8) {
if (board[j+1][i] != null) {
moves.add(new int[] {i, j+1});
metPiece = true;
}
else {
j += 1;
moves.add(new int[] {i, j}); moves.add(new int[] {i, j});
} }
} }
metPiece = false;
i = x;
j = y;
while (metPiece == false && i-1 > -1) {
if (board[j][i-1] != null) {
moves.add(new int[] {i-1, j});
metPiece = true;
}
else {
i -= 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && j-1 > -1) {
if (board[j-1][i] != null) {
moves.add(new int[] {i, j-1});
metPiece = true;
}
else {
j -= 1;
moves.add(new int[] {i, j});
}
} }
} }
else if (type == PieceType.Knight) { if (type == PieceType.Pawn) {
if (x+1 < 8) {
if (y+2 < 8) {
moves.add(new int[] {x+1, y+2});
}
if (y-2 > -1) {
moves.add(new int[] {x+1, y-2});
}
}
if (x+2 < 8) {
if (y+1 < 8) {
moves.add(new int[] {x+2, y+1});
}
if (y-1 > -1) {
moves.add(new int[] {x+2, y-1});
}
}
if (x-1 > -1) {
if (y+2 < 8) {
moves.add(new int[] {x-1, y+2});
}
if (y-2 > -1) {
moves.add(new int[] {x-1, y-2});
}
}
if (x-2 > -1) {
if (y+1 < 8) {
moves.add(new int[] {x-2, y+1});
}
if (y-1 > -1) {
moves.add(new int[] {x-2, y-1});
}
}
}
else if (type == PieceType.Bishop) {
metPiece = false;
i = x;
j = y;
while (metPiece == false && i+1 < 8 && j+1 < 8) {
if (board[j+1][i+1] != null) {
moves.add(new int[] {i+1, j+1});
metPiece = true;
}
else {
i += 1;
j += 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && i-1 > -1 && j+1 < 8) {
if (board[j+1][i-1] != null) {
moves.add(new int[] {i-1, j+1});
metPiece = true;
}
else {
i -= 1;
j += 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && i+1 < 8 && j-1 > -1) {
if (board[j-1][i+1] != null) {
moves.add(new int[] {i+1, j-1});
metPiece = true;
}
else {
i += 1;
j -= 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && i-1 > -1 && j-1 > -1) {
if (board[j-1][i-1] != null) {
moves.add(new int[] {i-1, j-1});
metPiece = true;
}
else {
i -= 1;
j -= 1;
moves.add(new int[] {i, j});
}
}
}
else if (type == PieceType.Queen) {
metPiece = false;
i = x;
j = y;
while (metPiece == false && i+1 < 8) {
if (board[j][i+1] != null) {
moves.add(new int[] {i+1, j});
metPiece = true;
}
else {
i += 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && j+1 < 8) {
if (board[j+1][i] != null) {
moves.add(new int[] {i, j+1});
metPiece = true;
}
else {
j += 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && i-1 > -1) {
if (board[j][i-1] != null) {
moves.add(new int[] {i-1, j});
metPiece = true;
}
else {
i -= 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && j-1 > -1) {
if (board[j-1][i] != null) {
moves.add(new int[] {i, j-1});
metPiece = true;
}
else {
j -= 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && i+1 < 8 && j+1 < 8) {
if (board[j+1][i+1] != null) {
moves.add(new int[] {i+1, j+1});
metPiece = true;
}
else {
i += 1;
j += 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && i-1 > -1 && j+1 < 8) {
if (board[j+1][i-1] != null) {
moves.add(new int[] {i-1, j+1});
metPiece = true;
}
else {
i -= 1;
j += 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && i+1 < 8 && j-1 > -1) {
if (board[j-1][i+1] != null) {
moves.add(new int[] {i+1, j-1});
metPiece = true;
}
else {
i += 1;
j -= 1;
moves.add(new int[] {i, j});
}
}
metPiece = false;
i = x;
j = y;
while (metPiece == false && i-1 > -1 && j-1 > -1) {
if (board[j-1][i-1] != null) {
moves.add(new int[] {i-1, j-1});
metPiece = true;
}
else {
i -= 1;
j -= 1;
moves.add(new int[] {i, j});
}
}
}
else if (type == PieceType.King) {
if (x+1 < 8) {
moves.add(new int[] {x+1, y});
if (y+1 < 8) {
moves.add(new int[] {x+1, y+1});
}
if (y-1 > -1) {
moves.add(new int[] {x+1, y-1});
}
}
if (x-1 > -1) {
moves.add(new int[] {x-1, y});
if (y+1 < 8) {
moves.add(new int[] {x-1, y+1});
}
if (y-1 > -1) {
moves.add(new int[] {x-1, y-1});
}
}
if (y+1 < 8) {
moves.add(new int[] {x, y+1});
}
if (y-1 > -1) {
moves.add(new int[] {x, y-1});
}
}
else {
if (isWhite == false) { if (isWhite == false) {
if (y == 1) { pieceMoves = blackPawn;
if (board[j+1][i] == null) {
moves.add(new int[] {i, j+1});
if (board[j+2][i] == null) {
moves.add(new int[] {i, j+2});
}
}
} }
else { else {
if (board[j+1][i] == null) { pieceMoves = whitePawn;
moves.add(new int[] {i, j+1});
} }
}
if (x != 7) { i = x + pieceMoves[1][0];
if (board[j+1][i+1] != null) { j = y + pieceMoves[1][1];
moves.add(new int[] {i+1, j+1}); if (inBoard(i,j) && board[j][i] == null) {
} moves.add(new int[] {i, j});
} if (y == pieceMoves[0][0]) {
if (x != 0) { i = x + pieceMoves[4][0];
if (board[j+1][i-1] != null) { j = y + pieceMoves[4][1];
moves.add(new int[] {i-1, j+1}); if (inBoard(i,j) && board[j][i] == null) {
moves.add(new int[] {i, j});
} }
} }
} }
else {
if (y == 6) { for (int n = 2; n < 4; n++) {
if (board[j-1][i] == null) { i = x + pieceMoves[n][0];
moves.add(new int[] {i, j-1}); j = y + pieceMoves[n][1];
if (board[j-2][i] == null) { if (inBoard(i,j) && board[j][i] != null && board[j][i].isWhite() != isWhite) {
moves.add(new int[] {i, j-2}); moves.add(new int[] {i, j});
}
}
}
else {
if (board[j-1][i] == null) {
moves.add(new int[] {i, j-1});
}
}
if (x != 7) {
if (board[j-1][i+1] != null) {
moves.add(new int[] {i+1, j-1});
}
}
if (x != 0) {
if (board[j-1][i-1] != null) {
moves.add(new int[] {i-1, j-1});
} }
else if(inBoard(i,j) && board[j][i] == null && enPassant == true && i == pawnX && y == pawnY) {
moves.add(new int[] {i, j});
enPassantX = i;
enPassantY = j;
} }
} }
} }
@ -514,29 +269,20 @@ public class Board {
} }
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
if (this.x != -1 && this.y != -1) {
if (this.x == x && this.y == y) { if (this.x == x && this.y == y) {
return true; return true;
} }
else if (board[this.y][this.x] == null) {
this.x = -1;
this.y = -1;
return false;
}
}
return false; return false;
} }
/* saving-loading feature :*/ /* saving-loading feature :*/
public String[] toFileRep() { public String[] toFileRep() {
String myFile = this.toString();
String turn = "B"; String turn = "B";
if (this.isTurnWhite() == true) { if (isTurnWhite()) {
turn = "W"; turn = "W";
} }
myFile += turn; String myFile = toString() + turn;
String[] myArray = myFile.split("\n"); String[] myArray = myFile.split("\n");
return myArray; return myArray;
} }
@ -549,23 +295,23 @@ public class Board {
this.y = -1; this.y = -1;
this.turns = 0; this.turns = 0;
this.whiteTurn = true; this.whiteTurn = true;
this.enPassant = false;
String[] line; String[] line;
boolean white; boolean white;
char character; for (int i = 0; i < colNum; i++) {
PieceType type;
for (int i = 0; i < 8; i++) {
line = array[i].split(","); line = array[i].split(",");
for (int j = 0; j < 8; j++) { for (int j = 0; j < lineNum; j++) {
if (!line[j].equals(" ")) { if (line[j] != " ") {
white = line[j].charAt(0) == 'W'; white = false;
character = line[j].charAt(1); if (line[j].charAt(0) == 'W') {
type = PieceType.fromSummary(character); white = true;
setPiece(white, type, j, i); }
setPiece(white, PieceType.fromSummary(line[j].charAt(1)), j, i);
} }
} }
} }
if (array[8] == "B") { if (array[lineNum] == "B") {
whiteTurn = false; whiteTurn = false;
} }
} }
@ -575,10 +321,9 @@ public class Board {
public boolean isHighlighted(int x, int y) { public boolean isHighlighted(int x, int y) {
if (this.x != -1 && this.y != -1) { if (this.x != -1 && this.y != -1) {
ArrayList<int[]> moves = this.getMove(board[this.y][this.x].getType(), board[this.y][this.x].isWhite(), this.x, this.y); ArrayList<int[]> moves = getMove(board[this.y][this.x].getType(), board[this.y][this.x].isWhite(), this.x, this.y);
for (int i = 0; i < moves.size(); i++) { for (int i = 0; i < moves.size(); i++) {
int[] coordinates = moves.get(i); if (x == moves.get(i)[0] && y == moves.get(i)[1]) {
if (x == coordinates[0] && y == coordinates[1]) {
return true; return true;
} }
} }