diff --git a/src/backend/Board.java b/src/backend/Board.java index 4454735..16add34 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -3,9 +3,13 @@ package backend; import java.util.ArrayList; public class Board { + private String boardString; // Represent the board's state as a string private int line; private int col; 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) { this.col=colNum; @@ -22,17 +26,14 @@ public class Board { } public int getTurnNumber() { - //TODO - return 0; + return this.turnNumber; } public boolean isTurnWhite() { - //TODO - return false; + return this.turnWhite; } public void setPiece(boolean isWhite, PieceType type, int x, int y) { - //TODO datastructure treeset // Create a new Piece object with the provided attributes Piece piece =new Piece (isWhite, type,x,y); @@ -122,12 +123,51 @@ public class Board { public void userTouch(int x, int y) { //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) { - //TODO - return false; + return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y; } /* saving-loading feature :*/