turn Number fixed and added

This commit is contained in:
martinbst 2025-05-07 14:34:39 +02:00
parent 24a9be40f9
commit 25a0ed5bf2
1 changed files with 392 additions and 391 deletions

View File

@ -1,391 +1,392 @@
package backend; package backend;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
public class Board { public class Board {
private KingCheck kingCheck = new KingCheck(); private KingCheck kingCheck = new KingCheck();
private int width = 8; private int width = 8;
private int height = 8; private int height = 8;
private ArrayList<ArrayList<Piece>> board = new ArrayList<>(); private ArrayList<ArrayList<Piece>> board = new ArrayList<>();
private boolean select = false; private boolean select = false;
private int xm; private int xm;
private int ym; private int ym;
private int turnNumber; private int turnNumber;
private boolean turnColor; private boolean turnColor;
private Move lastMove; // new field private Move lastMove; // new field
private ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>(); private ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>();
private LinkedList<BoardHistory> boardHistory = new LinkedList<>(); private LinkedList<BoardHistory> boardHistory = new LinkedList<>();
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width = colNum; this.width = colNum;
this.height = lineNum; this.height = lineNum;
int rows = 8; int rows = 8;
int cols = 8; int cols = 8;
for (int i = 0; i < rows; i++) { for (int i = 0; i < rows; i++) {
ArrayList<Piece> row = new ArrayList<>(); ArrayList<Piece> row = new ArrayList<>();
ArrayList<Boolean> rowB = new ArrayList<>(); ArrayList<Boolean> rowB = new ArrayList<>();
for (int j = 0; j < cols; j++) { for (int j = 0; j < cols; j++) {
row.add(null); // Fill with null row.add(null); // Fill with null
rowB.add(false); rowB.add(false);
} }
this.board.add(row); this.board.add(row);
this.possibleMoves.add(rowB); this.possibleMoves.add(rowB);
} }
this.turnNumber = 0; this.turnNumber = 0;
this.turnColor = true; this.turnColor = true;
boardHistory.add(new BoardHistory(board,turnNumber,turnColor)); boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
} }
public int getWidth() { public int getWidth() {
return this.width; return this.width;
} }
public int getHeight() { public int getHeight() {
return this.height; return this.height;
} }
public int getTurnNumber() { public int getTurnNumber() {
return this.turnNumber; return this.turnNumber;
} }
public boolean isTurnWhite() { public boolean isTurnWhite() {
return this.turnColor; return this.turnColor;
} }
public void setPiece(int x, int y, PieceType type, boolean isWhite) { public void setPiece(int x, int y, PieceType type, boolean isWhite) {
Piece piece = PieceCreation.createPiece(x,y,type,isWhite); Piece piece = PieceCreation.createPiece(x,y,type,isWhite);
board.get(y).set(x, piece); board.get(y).set(x, piece);
} }
public void populateBoard() {//populates the board public void populateBoard() {//populates the board
for (int x = 0; x < this.width;x++) { for (int x = 0; x < this.width;x++) {
for (int y = 0; y < this.height; y++) { for (int y = 0; y < this.height; y++) {
if (y ==1 || y == 6) { if (y ==1 || y == 6) {
if (y == 1) { if (y == 1) {
this.setPiece(x,y,PieceType.Pawn,false);//Place the pawns this.setPiece(x,y,PieceType.Pawn,false);//Place the pawns
} }
else if (y == 6) {this.setPiece(x,y,PieceType.Pawn,true);} else if (y == 6) {this.setPiece(x,y,PieceType.Pawn,true);}
} }
if (y == 0) { if (y == 0) {
boolean col = false; boolean col = false;
if (x == 0 || x == 7) { if (x == 0 || x == 7) {
this.setPiece(x,y,PieceType.Rook,col);//Places the rooks this.setPiece(x,y,PieceType.Rook,col);//Places the rooks
} else if (x == 1 || x == 6) {this.setPiece(x,y,PieceType.Knight,col);}// Place the Knights } else if (x == 1 || x == 6) {this.setPiece(x,y,PieceType.Knight,col);}// Place the Knights
else if (x == 2 || x == 5) {this.setPiece(x,y,PieceType.Bishop,col);}// Place the Bishops else if (x == 2 || x == 5) {this.setPiece(x,y,PieceType.Bishop,col);}// Place the Bishops
else if (x == 3) {this.setPiece(x,y,PieceType.Queen,col);}// Places the queen else if (x == 3) {this.setPiece(x,y,PieceType.Queen,col);}// Places the queen
else if (x == 4) {this.setPiece(x,y,PieceType.King,col);}// Places the King else if (x == 4) {this.setPiece(x,y,PieceType.King,col);}// Places the King
} else if (y == 7) { } else if (y == 7) {
boolean col = true; boolean col = true;
if (x == 0 || x == 7) { if (x == 0 || x == 7) {
this.setPiece(x,y,PieceType.Rook,col); this.setPiece(x,y,PieceType.Rook,col);
} else if (x == 1 || x == 6) {this.setPiece(x,y,PieceType.Knight,col);} } else if (x == 1 || x == 6) {this.setPiece(x,y,PieceType.Knight,col);}
else if (x == 2 || x == 5) {this.setPiece(x,y,PieceType.Bishop,col);} else if (x == 2 || x == 5) {this.setPiece(x,y,PieceType.Bishop,col);}
else if (x == 3) {this.setPiece(x,y,PieceType.Queen,col);} else if (x == 3) {this.setPiece(x,y,PieceType.Queen,col);}
else if (x == 4) {this.setPiece(x,y,PieceType.King,col);} else if (x == 4) {this.setPiece(x,y,PieceType.King,col);}
} }
} }
} }
} }
public void cleanBoard() { public void cleanBoard() {
int rows = 8; int rows = 8;
int cols = 8; int cols = 8;
for (int x = 0; x < rows; x++) { for (int x = 0; x < rows; x++) {
for (int y = 0; y < cols; y++) { for (int y = 0; y < cols; y++) {
this.board.get(y).set(x, null); this.board.get(y).set(x, null);
} }
} }
this.turnNumber = 0; this.turnNumber = 0;
this.turnColor = true; this.turnColor = true;
boardHistory.clear(); boardHistory.clear();
} }
@Override @Override
public String toString() { public String toString() {
return "Board [width=" + width + ", height=" + height + ", board=" + board + "]"; return "Board [width=" + width + ", height=" + height + ", board=" + board + "]";
} }
public ArrayList<Piece> getPieces() { public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>(); ArrayList<Piece> pieces = new ArrayList<>();
for (ArrayList<Piece> row : board) { for (ArrayList<Piece> row : board) {
for (Piece piece : row) { for (Piece piece : row) {
if (piece != null) { if (piece != null) {
pieces.add(piece); pieces.add(piece);
} }
} }
} }
return pieces; return pieces;
} }
public Piece getPiece(int x, int y) { public Piece getPiece(int x, int y) {
return board.get(y).get(x); return board.get(y).get(x);
} }
public void movePiece(int x, int y) { public void movePiece(int x, int y) {
if (select) { if (select) {
boardHistory.add(new BoardHistory(board,turnNumber,turnColor)); boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
Piece pieceToMove = this.board.get(this.ym).get(this.xm); Piece pieceToMove = this.board.get(this.ym).get(this.xm);
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2; boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
if (pieceToMove instanceof Pawn && x != xm && board.get(y).get(x) == null) { if (pieceToMove instanceof Pawn && x != xm && board.get(y).get(x) == null) {
// En passant capture // En passant capture
board.get(ym).set(x, null); board.get(ym).set(x, null);
lastMove = new Move(xm, ym, x, y); lastMove = new Move(xm, ym, x, y);
} }
//check for castling mode: //check for castling mode:
if (isCastlingMove) { if (isCastlingMove) {
//kingside //kingside
if (x > xm) { if (x > xm) {
Piece rook = board.get(ym).get(7); Piece rook = board.get(ym).get(7);
board.get(ym).set(5, rook); board.get(ym).set(5, rook);
board.get(ym).set(7, null); board.get(ym).set(7, null);
if (rook != null) { if (rook != null) {
rook.x = 5; rook.x = 5;
rook.setMoved(true); rook.setMoved(true);
} }
} }
if (x < xm) { if (x < xm) {
Piece rook = board.get(ym).get(0); Piece rook = board.get(ym).get(0);
board.get(ym).set(3, rook); board.get(ym).set(3, rook);
board.get(ym).set(0, null); board.get(ym).set(0, null);
if (rook != null) { if (rook != null) {
rook.x = 3; rook.x = 3;
rook.setMoved(true); rook.setMoved(true);
} }
} }
} }
board.get(this.ym).set(this.xm,null); board.get(this.ym).set(this.xm,null);
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite()); this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
Piece movedPiece = this.getPiece(x, y); Piece movedPiece = this.getPiece(x, y);
lastMove = new Move(xm, ym, x, y); lastMove = new Move(xm, ym, x, y);
if (movedPiece instanceof Pawn) { if (movedPiece instanceof Pawn) {
int promotionRow = movedPiece.isWhite() ? 0 : 7; int promotionRow = movedPiece.isWhite() ? 0 : 7;
if (y == promotionRow) { if (y == promotionRow) {
Object[] choices = { "Queen", "Rook", "Bishop", "Knight" }; Object[] choices = { "Queen", "Rook", "Bishop", "Knight" };
String selected = (String) JOptionPane.showInputDialog( String selected = (String) JOptionPane.showInputDialog(
null, null,
"Choose a piece for promotion:", "Choose a piece for promotion:",
"Pawn Promotion", "Pawn Promotion",
JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE,
null, null,
choices, choices,
"Queen" "Queen"
); );
Piece promoted; Piece promoted;
switch (selected) { switch (selected) {
case "Rook": case "Rook":
promoted = new Rook(x, y, false); promoted = new Rook(x, y, false);
break; break;
case "Bishop": case "Bishop":
promoted = new Bishop(x, y, false); promoted = new Bishop(x, y, false);
break; break;
case "Knight": case "Knight":
promoted = new Knight(x, y, false); promoted = new Knight(x, y, false);
break; break;
default: default:
promoted = new Queen(x, y, false); promoted = new Queen(x, y, false);
} }
board.get(y).set(x, promoted); board.get(y).set(x, promoted);
this.setPiece(x,y,promoted.getType(),pieceToMove.isWhite()); this.setPiece(x,y,promoted.getType(),pieceToMove.isWhite());
// You can replace Queen with any piece type as needed // You can replace Queen with any piece type as needed
} }
} }
if (movedPiece != null) { if (movedPiece != null) {
movedPiece.setMoved(true); movedPiece.setMoved(true);
} }
} }
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
if ((this.select == false && board.get(y).get(x) != null && this.board.get(y).get(x).isWhite() == turnColor) || (select == true && board.get(y).get(x) != null && board.get(y).get(x).isWhite() == board.get(ym).get(xm).isWhite())) { if ((this.select == false && board.get(y).get(x) != null && this.board.get(y).get(x).isWhite() == turnColor) || (select == true && board.get(y).get(x) != null && board.get(y).get(x).isWhite() == board.get(ym).get(xm).isWhite())) {
this.xm = x; this.xm = x;
this.ym = y; this.ym = y;
select = true; select = true;
possibleMoves = kingCheck.getLegalMoves(board.get(y).get(x), board,lastMove); possibleMoves = kingCheck.getLegalMoves(board.get(y).get(x), board,lastMove);
// possibleMoves = board.get(y).get(x).getPossibleMoves(board); // possibleMoves = board.get(y).get(x).getPossibleMoves(board);
// possibleMoves = board.get(y).get(x).getPossibleMoves(board, lastMove);//same as kingCheck // possibleMoves = board.get(y).get(x).getPossibleMoves(board, lastMove);//same as kingCheck
} }
else if (select == true && this.xm != x || this.ym != y){ else if (select == true && this.xm != x || this.ym != y){
if (isHighlighted(x,y)) { if (isHighlighted(x,y)) {
this.movePiece(x, y); this.movePiece(x, y);
select = false; select = false;
this.turnNumber += 1;
// System.out.println(this.toString()); // Debug this.turnColor = !this.turnColor;
this.turnColor = !this.turnColor;
} if(!this.turnColor) {
} else { this.turnNumber += 1;
// System.out.println("a"); // Debug ! }
select = false; }
} }
else {
select = false;
} }
}
public boolean isSelected(int x, int y) {
boolean S; public boolean isSelected(int x, int y) {
if (this.xm == x && this.ym == y) {S = true;} boolean S;
else {S = false;} if (this.xm == x && this.ym == y) {S = true;}
return S; else {S = false;}
} return S;
}
public boolean isCheck(int x, int y) {
boolean out = false; public boolean isCheck(int x, int y) {
if (board.get(y).get(x) != null) { boolean out = false;
if (board.get(y).get(x).getType() == PieceType.King && board.get(y).get(x).isWhite == turnColor){ if (board.get(y).get(x) != null) {
if (kingCheck.isKingInCheck(board,turnColor)) { if (board.get(y).get(x).getType() == PieceType.King && board.get(y).get(x).isWhite == turnColor){
out = true; if (kingCheck.isKingInCheck(board,turnColor)) {
} out = true;
} }
} }
return out; }
} return out;
}
/* saving-loading feature :*/
/* saving-loading feature :*/
public String[] toFileRep() {
FileBoard boardToFile = new FileBoard(board,turnNumber,turnColor); public String[] toFileRep() {
return boardToFile.toFile(); FileBoard boardToFile = new FileBoard(board,turnNumber,turnColor);
} return boardToFile.toFile();
}
public Board(String[] array) {
FileBoard fileToBoard = new FileBoard(array); public Board(String[] array) {
board = fileToBoard.getBoard(); FileBoard fileToBoard = new FileBoard(array);
turnNumber = fileToBoard.getTurnNumber(); board = fileToBoard.getBoard();
turnColor = fileToBoard.isTurnColor(); turnNumber = fileToBoard.getTurnNumber();
boardHistory.add(new BoardHistory(board,turnNumber,turnColor)); turnColor = fileToBoard.isTurnColor();
} boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
// public Board toBoard(String[] array) { }
// FileBoard fileToBoard = new FileBoard(array); // public Board toBoard(String[] array) {
// Board boardF = new Board(fileToBoard.getBoard(),fileToBoard.getTurnNumber(),fileToBoard.isTurnColor()); // FileBoard fileToBoard = new FileBoard(array);
// return boardF; // Board boardF = new Board(fileToBoard.getBoard(),fileToBoard.getTurnNumber(),fileToBoard.isTurnColor());
//// this.turnColor = fileToBoard.isTurnColor(); // return boardF;
//// this.turnNumber = fileToBoard.getTurnNumber(); //// this.turnColor = fileToBoard.isTurnColor();
//// boardHistory.add(new BoardHistory(board,turnNumber,turnColor)); //// this.turnNumber = fileToBoard.getTurnNumber();
// } //// boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
// }
/* The following methods require more work ! */
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
boolean toBeReturned; public boolean isHighlighted(int x, int y) {
if (select) { boolean toBeReturned;
toBeReturned = possibleMoves.get(y).get(x); if (select) {
} toBeReturned = possibleMoves.get(y).get(x);
else { toBeReturned = false;} }
return toBeReturned; else { toBeReturned = false;}
} return toBeReturned;
}
public ArrayList<ArrayList<Piece>> getBoard() {
return board; public ArrayList<ArrayList<Piece>> getBoard() {
} return board;
}
public void undoLastMove() {
if (boardHistory.size()>=1) { public void undoLastMove() {
board = boardHistory.get(boardHistory.size() - 1).getBoard(); if (boardHistory.size()>=1) {
this.turnNumber = boardHistory.getLast().getTurnNumber(); board = boardHistory.get(boardHistory.size() - 1).getBoard();
this.turnColor = boardHistory.getLast().isTurnColor(); this.turnNumber = boardHistory.getLast().getTurnNumber();
boardHistory.removeLast(); this.turnColor = boardHistory.getLast().isTurnColor();
// System.out.println(boardHistory); // Debug boardHistory.removeLast();
} // System.out.println(boardHistory); // Debug
} }
}
public Board(Board board) {
//TODO public Board(Board board) {
//TODO
}
}
public void playMove(Move move) {
int xm = move.getFromX(); public void playMove(Move move) {
int ym = move.getFromY(); int xm = move.getFromX();
int x = move.getToX(); int ym = move.getFromY();
int y = move.getToY(); int x = move.getToX();
boardHistory.add(new BoardHistory(board,turnNumber,turnColor)); int y = move.getToY();
Piece pieceToMove = this.board.get(ym).get(xm); boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2; Piece pieceToMove = this.board.get(ym).get(xm);
lastMove = new Move(xm, ym, x, y); boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
lastMove = new Move(xm, ym, x, y);
if (pieceToMove instanceof Pawn && x != xm && board.get(y).get(x) == null) {
// En passant capture if (pieceToMove instanceof Pawn && x != xm && board.get(y).get(x) == null) {
board.get(ym).set(x, null); // En passant capture
} board.get(ym).set(x, null);
}
//check for castling mode:
if (isCastlingMove) { //check for castling mode:
if (isCastlingMove) {
//kingside
if (x > xm) { //kingside
Piece rook = board.get(ym).get(7); if (x > xm) {
board.get(ym).set(5, rook); Piece rook = board.get(ym).get(7);
board.get(ym).set(7, null); board.get(ym).set(5, rook);
if (rook != null) { board.get(ym).set(7, null);
rook.x = 5; if (rook != null) {
rook.setMoved(true); rook.x = 5;
} rook.setMoved(true);
} }
if (x < xm) { }
Piece rook = board.get(ym).get(0); if (x < xm) {
board.get(ym).set(3, rook); Piece rook = board.get(ym).get(0);
board.get(ym).set(0, null); board.get(ym).set(3, rook);
if (rook != null) { board.get(ym).set(0, null);
rook.x = 3; if (rook != null) {
rook.setMoved(true); rook.x = 3;
} rook.setMoved(true);
} }
} }
}
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
Piece movedPiece = this.getPiece(x, y); this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
if (movedPiece != null) { Piece movedPiece = this.getPiece(x, y);
movedPiece.setMoved(true); if (movedPiece != null) {
} movedPiece.setMoved(true);
if (movedPiece instanceof Pawn) { }
int promotionRow = movedPiece.isWhite() ? 0 : 7; if (movedPiece instanceof Pawn) {
if (y == promotionRow) { int promotionRow = movedPiece.isWhite() ? 0 : 7;
// Replace pawn with promoted piece if (y == promotionRow) {
// System.out.println() // Replace pawn with promoted piece
this.setPiece(x,y,PieceType.Queen,pieceToMove.isWhite()); // System.out.println()
// You can replace Queen with any piece type as needed this.setPiece(x,y,PieceType.Queen,pieceToMove.isWhite());
} // You can replace Queen with any piece type as needed
} }
board.get(ym).set(xm,null); }
this.turnColor = !this.turnColor; board.get(ym).set(xm,null);
this.turnNumber +=1; this.turnColor = !this.turnColor;
} this.turnNumber +=1;
}
public void setBoard(ArrayList<ArrayList<Piece>> board) {
this.board = board; public void setBoard(ArrayList<ArrayList<Piece>> board) {
} this.board = board;
}
public void setTurnNumber(int turnNumber) {
this.turnNumber = turnNumber; public void setTurnNumber(int turnNumber) {
} this.turnNumber = turnNumber;
}
public void setTurnColor(boolean turnColor) {
this.turnColor = turnColor; public void setTurnColor(boolean turnColor) {
} this.turnColor = turnColor;
}
public void setBoardHistory(LinkedList<BoardHistory> boardHistory) {
this.boardHistory = boardHistory; public void setBoardHistory(LinkedList<BoardHistory> boardHistory) {
} this.boardHistory = boardHistory;
}
public boolean isTurnColor() {
return turnColor; public boolean isTurnColor() {
} return turnColor;
}
public Move getLastMove() {
return lastMove; public Move getLastMove() {
} return lastMove;
}
}
}
////
////