From 279517fd17bded8812f82d2191bb5b00ffe7da4d Mon Sep 17 00:00:00 2001 From: keshi Date: Sat, 17 May 2025 17:29:10 +0200 Subject: [PATCH] added comments to every line for board so i dont get lost and understand --- src/backend/Board.java | 116 ++++++++++++++++++++----------------- src/backend/MovePiece.java | 15 ++++- src/backend/Piece.java | 6 +- src/backend/PieceType.java | 8 +-- 4 files changed, 85 insertions(+), 60 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index c4de5a4..54b70b3 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; public class Board { + //defining all attributes to the class itself private int width; private int height; private Piece[][] board;//creating an array for the coordinates of the board @@ -12,12 +13,11 @@ public class Board { private boolean isTurnWhite=true; private ArrayList highlightedSquares= new ArrayList<>(); //stores highlighted board positions private Move lastMove; // Tracks the last move; needed for en-passant rule - private List previousStates = new ArrayList<>(); // i added this - - + private List previousStates = new ArrayList<>(); - public Board(int colNum, int lineNum) { - this.width=colNum; + //constructor for the board + 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.board= new Piece[width][height]; } @@ -72,28 +72,29 @@ public class Board { return isTurnWhite; } + //sets a piece on the board 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() { - PieceType[] values= PieceType.values(); - //Pawn, Rook, Knight, Bishop, Queen, King + PieceType[] values= PieceType.values(); //creating array of all 6 chess pieces //Pawn, Rook, Knight, Bishop, Queen, King 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 - for(int x=0; x<8; x++) { - board[x][1]= new Piece(x,1,values[0], false); - board[x][6]= new Piece(x,6,values[0], true); + for(int x=0; x<8; x++) { //skips through columns + board[x][1]= new Piece(x,1,values[0], false); //black piece + board[x][6]= new Piece(x,6,values[0], true);//white pieces } //row 1 and 8 using other pieces for(int x=0; x<8; x++ ) { - board[x][0]= new Piece(x, 0, backRowPieces[x], false); - board[x][7]= new Piece(x, 7, backRowPieces[x], true); + board[x][0]= new Piece(x, 0, backRowPieces[x], false); //black pieces + board[x][7]= new Piece(x, 7, backRowPieces[x], true);//white piece } } - + //cleaning the board public void cleanBoard() { for(int y=0; y<8; y++) { 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(); - for (int y=0; y< height; y++) { - for (int x=0; x getPieces() { + //collects all pieces on the board and return them in a list + public ArrayList getPieces() { ArrayList pieces = new ArrayList<>(); + //loop through the board for(int y=0; y<8; y++) { for(int x=0; x<8; x++) { 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) { - Piece clickedPiece=board[x][y]; - if(chosenPiece==null) { - //when no piece is selected - if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) { - chosenPiece=clickedPiece; + Piece clickedPiece=board[x][y]; //coordinates of space that is clicked + //when no piece is selected, we need to select one + if(chosenPiece==null) { + if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) { //check current player's turn colour like the piece he clicked + chosenPiece=clickedPiece; //selects piece 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)) { 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); boolean movementSuccess=false; + //checking the type of piece it is to move it if(chosenPiece.getType()==PieceType.Pawn) { movementSuccess=movement.movePawn(x,y); } else if (chosenPiece.getType()==PieceType.Rook) { @@ -163,29 +174,32 @@ public class Board { }else if (chosenPiece.getType()==PieceType.Knight) { movementSuccess=movement.moveKnight(x, y); } + //once the moving of the piece has worked: if(movementSuccess) { - chosenPiece=null; - highlightedSquares.clear(); - turnNumber++; - isTurnWhite=! isTurnWhite; - } else { //invalid move + chosenPiece=null; //deselects piece + highlightedSquares.clear(); //clears highlights + turnNumber++;//changes turn of player + isTurnWhite=! isTurnWhite;//the other played is the opposite colour + } 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; } - - public Piece getPiece(int x, int y) { //returns figure and gives coordinates + //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 return board[x][y]; //returns the piece which is currently on this coordinates } return null; } + //moving the piece public void movePiece(int oldX, int oldY, int newX, int newY) { previousStates.add(new Board(this)); if (board[oldX][oldY] != null) { //checks if there is a piece at that position @@ -198,12 +212,12 @@ public class Board { piece.setY(newY); lastMove= new Move(oldX, oldY, newX, newY, piece);//tracks the last move made 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 :*/ public String[] toFileRep() { @@ -216,6 +230,7 @@ public class Board { } + /* The following methods require more work ! */ private void highlightedPossibleMoves(Piece piece) { @@ -287,9 +302,6 @@ return false; } - - - private void updateTurnLabel() { // TODO Auto-generated method stub diff --git a/src/backend/MovePiece.java b/src/backend/MovePiece.java index ff406e2..a5a2c5b 100644 --- a/src/backend/MovePiece.java +++ b/src/backend/MovePiece.java @@ -10,7 +10,8 @@ public class MovePiece { 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(); switch(type) { case Pawn: @@ -107,6 +108,7 @@ public class MovePiece { } return false; //if not it was unsuccessful } + //Knight movement logic public boolean moveKnight(int x, int y) { int currentX = piece.getX(); @@ -129,6 +131,7 @@ public class MovePiece { } return false; } + // Bishop Movement Logic public boolean moveBishop(int x, int y) { int currentX = piece.getX(); @@ -143,6 +146,7 @@ public class MovePiece { } return false; } + //Queen movement logic public boolean moveQueen(int x, int y) { int currentX=piece.getX(); @@ -158,6 +162,7 @@ public class MovePiece { } return false; } + //King movement logic public boolean moveKing(int x, int y) { int currentX = piece.getX(); @@ -211,7 +216,8 @@ public class MovePiece { 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 public boolean movePawnSimulate(int x, int y) { @@ -232,6 +238,7 @@ public class MovePiece { return true; }return false; } + //bishop movement simulate public boolean moveBishopSimulate(int x, int y) { int currentX = piece.getX(); @@ -245,6 +252,7 @@ public class MovePiece { } return false; } + //Rook movement simulate public boolean moveRookSimulate(int x, int y) { int currentX = piece.getX(); @@ -262,6 +270,7 @@ public class MovePiece { } return false; //if not it was unsuccessful } + //Knight movement simulate public boolean moveKnightSimulate(int x, int y) { int currentX = piece.getX(); @@ -283,6 +292,7 @@ public class MovePiece { } return false; } + //Queen movement simulate public boolean moveQueenSimulate(int x, int y) { int currentX=piece.getX(); @@ -297,6 +307,7 @@ public class MovePiece { } return false; } + //King movement simulate public boolean moveKingSimulate(int x, int y) { int currentX = piece.getX(); diff --git a/src/backend/Piece.java b/src/backend/Piece.java index 0a8490d..0392529 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -14,6 +14,7 @@ public class Piece { this.piece=laPiece; this.white=isWhite; } + //getters public int getX() { return x_coor; } @@ -22,7 +23,8 @@ public class Piece { 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; } @@ -39,7 +41,7 @@ public class Piece { return white; } - public boolean didMove() { // added getter + public boolean getDidMove() { // added getter return didMove; } diff --git a/src/backend/PieceType.java b/src/backend/PieceType.java index baceab1..0d0dab0 100644 --- a/src/backend/PieceType.java +++ b/src/backend/PieceType.java @@ -1,16 +1,17 @@ package backend; 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) { return "N"; } 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') { return PieceType.Pawn; }else if(c=='N') { @@ -24,5 +25,4 @@ public enum PieceType { } return PieceType.Queen; } - }