This commit is contained in:
willo 2025-05-17 17:54:15 +02:00
commit f5eeb1ef87
4 changed files with 84 additions and 59 deletions

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Board { public class Board {
//defining all attributes to the class itself
private int width; private int width;
private int height; private int height;
private Piece[][] board;//creating an array for the coordinates of the board private Piece[][] board;//creating an array for the coordinates of the board
@ -12,12 +13,11 @@ public class Board {
private boolean isTurnWhite=true; private boolean isTurnWhite=true;
private ArrayList<int[]> highlightedSquares= new ArrayList<>(); //stores highlighted board positions private ArrayList<int[]> highlightedSquares= new ArrayList<>(); //stores highlighted board positions
private Move lastMove; // Tracks the last move; needed for en-passant rule private Move lastMove; // Tracks the last move; needed for en-passant rule
private List<Board> previousStates = new ArrayList<>(); // i added this private List<Board> previousStates = new ArrayList<>();
public Board(int colNum, int lineNum) { //constructor for the board
this.width=colNum; public Board(int colNum, int lineNum) { //here colNum and lineNum are arguments
this.width=colNum; //this refers to the current object
this.height=lineNum; this.height=lineNum;
this.board= new Piece[width][height]; this.board= new Piece[width][height];
} }
@ -72,28 +72,29 @@ public class Board {
return isTurnWhite; return isTurnWhite;
} }
//sets a piece on the board
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
board[y][x]= new Piece (x, y, type, isWhite); board[x][y]= new Piece (x, y, type, isWhite);//I CHANGED THIS (it was board[y][x]= new Piece (x, y, type, isWhite)) before
} }
//populating the entire board with all the pieces
public void populateBoard() { public void populateBoard() {
PieceType[] values= PieceType.values(); PieceType[] values= PieceType.values(); //creating array of all 6 chess pieces //Pawn, Rook, Knight, Bishop, Queen, King
//Pawn, Rook, Knight, Bishop, Queen, King
PieceType[] backRowPieces= {values[1], values[2], values[3], values[4], values[5], values[3], values[2], values[1]}; PieceType[] backRowPieces= {values[1], values[2], values[3], values[4], values[5], values[3], values[2], values[1]};
//row 2 and 7 using only pawns //row 2 and 7 using only pawns
for(int x=0; x<8; x++) { for(int x=0; x<8; x++) { //skips through columns
board[x][1]= new Piece(x,1,values[0], false); board[x][1]= new Piece(x,1,values[0], false); //black piece
board[x][6]= new Piece(x,6,values[0], true); board[x][6]= new Piece(x,6,values[0], true);//white pieces
} }
//row 1 and 8 using other pieces //row 1 and 8 using other pieces
for(int x=0; x<8; x++ ) { for(int x=0; x<8; x++ ) {
board[x][0]= new Piece(x, 0, backRowPieces[x], false); board[x][0]= new Piece(x, 0, backRowPieces[x], false); //black pieces
board[x][7]= new Piece(x, 7, backRowPieces[x], true); board[x][7]= new Piece(x, 7, backRowPieces[x], true);//white piece
} }
} }
//cleaning the board
public void cleanBoard() { public void cleanBoard() {
for(int y=0; y<8; y++) { for(int y=0; y<8; y++) {
for(int x=0; x<8; x++) { for(int x=0; x<8; x++) {
@ -102,54 +103,64 @@ public class Board {
} }
} }
public String toString() { //review the stringbuilder class that already exists in java aand see if we can replace it by smth else
StringBuilder builder= new StringBuilder(); //building the chessboard in the main
for (int y=0; y< height; y++) { public String toString() {
for (int x=0; x<width; x++) { StringBuilder builder= new StringBuilder(); //stringbuilder is a class that exists to build strings in java
Piece thePiece=board[x][y]; //looping through every space
if(thePiece!=null) { for (int y=0; y< height; y++) { //rows
String color =thePiece.isWhite()? "w":"b"; for (int x=0; x<width; x++) { //columns
builder.append(color).append(thePiece.getType().getSummary()); Piece thePiece=board[x][y]; //getting position of a piece
if(thePiece!=null) { //verifying if there is a piece in the space
String color =thePiece.isWhite()? "w":"b"; //verifying the colour of the piece (condition? valueIfTrue; valueIfFalse)
builder.append(color).append(thePiece.getType().getSummary()); //adding the colour and type of piece
} else { } else {
builder.append("--"); builder.append("--"); //if no piece present, append -- to represent nothing
} }
} }
builder.append("\n"); builder.append("\n"); //change line for every row
} }
return builder.toString(); return builder.toString(); //show chessboard on the main
} }
public ArrayList<Piece> getPieces() { //collects all pieces on the board and return them in a list
public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>(); ArrayList<Piece> pieces = new ArrayList<>();
//loop through the board
for(int y=0; y<8; y++) { for(int y=0; y<8; y++) {
for(int x=0; x<8; x++) { for(int x=0; x<8; x++) {
if(board[x][y] != null) { if(board[x][y] != null) {
pieces.add(board[x][y]); pieces.add(board[x][y]); //adds all the pieces present on board
} }
} }
} }
return pieces; return pieces; //returns list of non-empty spaces
} }
//defines what happens when the user clicks on a square
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
Piece clickedPiece=board[x][y]; Piece clickedPiece=board[x][y]; //coordinates of space that is clicked
if(chosenPiece==null) { //when no piece is selected, we need to select one
//when no piece is selected if(chosenPiece==null) {
if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) { if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) { //check current player's turn colour like the piece he clicked
chosenPiece=clickedPiece; chosenPiece=clickedPiece; //selects piece
highlightedPossibleMoves(clickedPiece); highlightedPossibleMoves(clickedPiece);
} }
} }
else { //if a piece is already selected //if a piece is already selected
else {
//if the user clicks the exact same piece again, we cancel the selection
if(isSelected(x,y)) { if(isSelected(x,y)) {
chosenPiece=null; //unselect the chosen piece to revert to normal state chosenPiece=null; //unselect the chosen piece to revert to normal state
highlightedSquares.clear(); highlightedSquares.clear();//removes highlight
} }
else { //move selected piece to new position using MovePiece logics //if the user tries to move selected piece to new position using MovePiece logics
else {
MovePiece movement= new MovePiece(chosenPiece, this); MovePiece movement= new MovePiece(chosenPiece, this);
boolean movementSuccess=false; boolean movementSuccess=false;
//checking the type of piece it is to move it
if(chosenPiece.getType()==PieceType.Pawn) { if(chosenPiece.getType()==PieceType.Pawn) {
movementSuccess=movement.movePawn(x,y); movementSuccess=movement.movePawn(x,y);
} else if (chosenPiece.getType()==PieceType.Rook) { } else if (chosenPiece.getType()==PieceType.Rook) {
@ -163,29 +174,32 @@ public class Board {
}else if (chosenPiece.getType()==PieceType.Knight) { }else if (chosenPiece.getType()==PieceType.Knight) {
movementSuccess=movement.moveKnight(x, y); movementSuccess=movement.moveKnight(x, y);
} }
//once the moving of the piece has worked:
if(movementSuccess) { if(movementSuccess) {
chosenPiece=null; chosenPiece=null; //deselects piece
highlightedSquares.clear(); highlightedSquares.clear(); //clears highlights
turnNumber++; turnNumber++;//changes turn of player
isTurnWhite=! isTurnWhite; isTurnWhite=! isTurnWhite;//the other played is the opposite colour
} else { //invalid move } else { //invalid move and try again
} }
} } //does nothing
} }
} }
public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates //checks if any piece is selected and if its position matches that of the x,y coordinates
public boolean isSelected(int x, int y) {
return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y; return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y;
} }
//returns figure and gives coordinates
public Piece getPiece(int x, int y) { //returns figure and gives coordinates public Piece getPiece(int x, int y) {
if (x >= 0 && x < width && y >= 0 && y < height) { //checks if coordinates are inside the board if (x >= 0 && x < width && y >= 0 && y < height) { //checks if coordinates are inside the board
return board[x][y]; //returns the piece which is currently on this coordinates return board[x][y]; //returns the piece which is currently on this coordinates
} }
return null; return null;
} }
//moving the piece
public void movePiece(int oldX, int oldY, int newX, int newY) { public void movePiece(int oldX, int oldY, int newX, int newY) {
previousStates.add(new Board(this)); previousStates.add(new Board(this));
if (board[oldX][oldY] != null) { //checks if there is a piece at that position if (board[oldX][oldY] != null) { //checks if there is a piece at that position
@ -198,12 +212,12 @@ public class Board {
piece.setY(newY); piece.setY(newY);
lastMove= new Move(oldX, oldY, newX, newY, piece);//tracks the last move made lastMove= new Move(oldX, oldY, newX, newY, piece);//tracks the last move made
System.out.println(" Move saved: " + oldX + "," + oldY + " to " + newX + "," + newY); System.out.println(" Move saved: " + oldX + "," + oldY + " to " + newX + "," + newY);
piece.setDidMove(true);// setting that the piece has been moved piece.setDidMove(true);// setting that the piece has been moved
} }
} }
/* saving-loading feature :*/ /* saving-loading feature :*/
public String[] toFileRep() { public String[] toFileRep() {
@ -216,6 +230,7 @@ public class Board {
} }
/* The following methods require more work ! */ /* The following methods require more work ! */
private void highlightedPossibleMoves(Piece piece) { private void highlightedPossibleMoves(Piece piece) {
@ -287,9 +302,6 @@ return false; }
private void updateTurnLabel() { private void updateTurnLabel() {
// TODO Auto-generated method stub // TODO Auto-generated method stub

View File

@ -10,7 +10,8 @@ public class MovePiece {
this.board = board; this.board = board;
} }
/* public boolean validateMove(int x, int y, boolean simulate) { /* i honestly dont know if i change it to that or not #keshini
* public boolean validateMove(int x, int y, boolean simulate) {
PieceType type = piece.getType(); PieceType type = piece.getType();
switch(type) { switch(type) {
case Pawn: case Pawn:
@ -107,6 +108,7 @@ public class MovePiece {
} }
return false; //if not it was unsuccessful return false; //if not it was unsuccessful
} }
//Knight movement logic //Knight movement logic
public boolean moveKnight(int x, int y) { public boolean moveKnight(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();
@ -129,6 +131,7 @@ public class MovePiece {
} }
return false; return false;
} }
// Bishop Movement Logic // Bishop Movement Logic
public boolean moveBishop(int x, int y) { public boolean moveBishop(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();
@ -143,6 +146,7 @@ public class MovePiece {
} }
return false; return false;
} }
//Queen movement logic //Queen movement logic
public boolean moveQueen(int x, int y) { public boolean moveQueen(int x, int y) {
int currentX=piece.getX(); int currentX=piece.getX();
@ -158,6 +162,7 @@ public class MovePiece {
} }
return false; return false;
} }
//King movement logic //King movement logic
public boolean moveKing(int x, int y) { public boolean moveKing(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();
@ -217,7 +222,8 @@ public class MovePiece {
return true; // No obstacles found return true; // No obstacles found
} }
//adding simulations part for the highlighting the pre-moves that can be performed(: only checking where the piece can go to avoid changing the game state) //adding simulations part for the highlighting the pre-moves that can be performed(only checking where the piece can go to avoid changing the game state)
//Pawn movement logic simulation //Pawn movement logic simulation
public boolean movePawnSimulate(int x, int y) { public boolean movePawnSimulate(int x, int y) {
@ -238,6 +244,7 @@ public class MovePiece {
return true; return true;
}return false; }return false;
} }
//bishop movement simulate //bishop movement simulate
public boolean moveBishopSimulate(int x, int y) { public boolean moveBishopSimulate(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();
@ -251,6 +258,7 @@ public class MovePiece {
} }
return false; return false;
} }
//Rook movement simulate //Rook movement simulate
public boolean moveRookSimulate(int x, int y) { public boolean moveRookSimulate(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();
@ -268,6 +276,7 @@ public class MovePiece {
} }
return false; //if not it was unsuccessful return false; //if not it was unsuccessful
} }
//Knight movement simulate //Knight movement simulate
public boolean moveKnightSimulate(int x, int y) { public boolean moveKnightSimulate(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();
@ -289,6 +298,7 @@ public class MovePiece {
} }
return false; return false;
} }
//Queen movement simulate //Queen movement simulate
public boolean moveQueenSimulate(int x, int y) { public boolean moveQueenSimulate(int x, int y) {
int currentX=piece.getX(); int currentX=piece.getX();
@ -303,6 +313,7 @@ public class MovePiece {
} }
return false; return false;
} }
//King movement simulate //King movement simulate
public boolean moveKingSimulate(int x, int y) { public boolean moveKingSimulate(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();

View File

@ -14,6 +14,7 @@ public class Piece {
this.piece=laPiece; this.piece=laPiece;
this.white=isWhite; this.white=isWhite;
} }
//getters
public int getX() { public int getX() {
return x_coor; return x_coor;
} }
@ -22,7 +23,8 @@ public class Piece {
return y_coor; return y_coor;
} }
public void setX(int x) { //update its coordinates in its own object needed for BOARD line 138,139 //setters
public void setX(int x) { //update its coordinates in its own object
this.x_coor = x; this.x_coor = x;
} }

View File

@ -1,16 +1,17 @@
package backend; package backend;
public enum PieceType { public enum PieceType {
Pawn, Rook, Knight, Bishop, Queen, King; Pawn, Rook, Knight, Bishop, Queen, King; //creating a list with all possible types in chess
public String getSummary() { public String getSummary() { //method to get short letter code for each piece
if(this == PieceType.Knight) { if(this == PieceType.Knight) {
return "N"; return "N";
} }
return this.name().substring(0, 1); return this.name().substring(0, 1);
} }
public static PieceType fromSummary(char c) { //static part lets me call it without needing an object to call it
public static PieceType fromSummary(char c) { //takes letter to return the piece type
if(c=='P') { if(c=='P') {
return PieceType.Pawn; return PieceType.Pawn;
}else if(c=='N') { }else if(c=='N') {
@ -24,5 +25,4 @@ public enum PieceType {
} }
return PieceType.Queen; return PieceType.Queen;
} }
} }