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