end of part 1 (unfinished)

This commit is contained in:
Valentine GIRAL 2025-05-06 12:59:55 +02:00
parent 13914dd390
commit 0ef1139f6a
1 changed files with 47 additions and 7 deletions

View File

@ -3,9 +3,13 @@ package backend;
import java.util.ArrayList; import java.util.ArrayList;
public class Board { public class Board {
private String boardString; // Represent the board's state as a string
private int line; private int line;
private int col; private int col;
private Piece[][] board; // 2D array to hold pieces private Piece[][] board; // 2D array to hold pieces
private int [] selectedPosition = null; // No piece selected initially
private int turnNumber=0; // Tracks the number of turns elapsed
private boolean turnWhite=true; // True if it's White's turn, False if it's Black's turn
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.col=colNum; this.col=colNum;
@ -22,17 +26,14 @@ public class Board {
} }
public int getTurnNumber() { public int getTurnNumber() {
//TODO return this.turnNumber;
return 0;
} }
public boolean isTurnWhite() { public boolean isTurnWhite() {
//TODO return this.turnWhite;
return false;
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
//TODO datastructure treeset
// Create a new Piece object with the provided attributes // Create a new Piece object with the provided attributes
Piece piece =new Piece (isWhite, type,x,y); Piece piece =new Piece (isWhite, type,x,y);
@ -122,12 +123,51 @@ public class Board {
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
//TODO //TODO
// First Touch: No position is selected, and a piece is at the position x,y
if (this.selectedPosition==null && getBoardChar(x,y)!='.') {
// Select the position at x, y and store it as an array
this.selectedPosition[0] = x;
this.selectedPosition[1] = y;
}
// Second Touch: A piece is already selected
else if (this.isSelected(x,y)){
// If the selected position is clicked again, deselect it
this.selectedPosition=null;
}
// If a piece is selected and the user clicks a new position
else if (this.selectedPosition!=null){
// Move the piece from the selected position to x, y
int fromX = this.selectedPosition[0];
int fromY = this.selectedPosition[1];
// Move the piece from (fromX, fromY) to (x, y)
this.movePiece(fromX, fromY, x, y);
// Update turn
this.turnNumber++;
this.turnWhite=!this.turnWhite;
// Deselect the position after moving
this.selectedPosition = null;
}
} }
public void movePiece(int fromX, int fromY, int toX, int toY) {
// Move the piece on the board
this.board[toX][toY] = this.board[fromX][fromY]; // Place piece at the new position
this.board[fromX][fromY] = null; // Clear the old position
}
private char getBoardChar(int x, int y) {
int index = y * 8 + x;
return boardString.charAt(index);
}
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
//TODO return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y;
return false;
} }
/* saving-loading feature :*/ /* saving-loading feature :*/