Merge branch 'master' of https://gitarero.ecam.fr/keshini.nistar/OOP_Groupe_1A3_Project.git
This commit is contained in:
commit
f5eeb1ef87
|
|
@ -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<int[]> highlightedSquares= new ArrayList<>(); //stores highlighted board positions
|
||||
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) {
|
||||
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<width; x++) {
|
||||
Piece thePiece=board[x][y];
|
||||
if(thePiece!=null) {
|
||||
String color =thePiece.isWhite()? "w":"b";
|
||||
builder.append(color).append(thePiece.getType().getSummary());
|
||||
|
||||
//building the chessboard in the main
|
||||
public String toString() {
|
||||
StringBuilder builder= new StringBuilder(); //stringbuilder is a class that exists to build strings in java
|
||||
//looping through every space
|
||||
for (int y=0; y< height; y++) { //rows
|
||||
for (int x=0; x<width; x++) { //columns
|
||||
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 {
|
||||
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<>();
|
||||
//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
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
@ -217,7 +222,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) {
|
||||
|
|
@ -238,6 +244,7 @@ public class MovePiece {
|
|||
return true;
|
||||
}return false;
|
||||
}
|
||||
|
||||
//bishop movement simulate
|
||||
public boolean moveBishopSimulate(int x, int y) {
|
||||
int currentX = piece.getX();
|
||||
|
|
@ -251,6 +258,7 @@ public class MovePiece {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//Rook movement simulate
|
||||
public boolean moveRookSimulate(int x, int y) {
|
||||
int currentX = piece.getX();
|
||||
|
|
@ -268,6 +276,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();
|
||||
|
|
@ -289,6 +298,7 @@ public class MovePiece {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//Queen movement simulate
|
||||
public boolean moveQueenSimulate(int x, int y) {
|
||||
int currentX=piece.getX();
|
||||
|
|
@ -303,6 +313,7 @@ public class MovePiece {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//King movement simulate
|
||||
public boolean moveKingSimulate(int x, int y) {
|
||||
int currentX = piece.getX();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue