the pieces can now move, but they are not constrained by rules yet.

Also, the blacks were the one starting, I switched it so the whites
could start first. Worked on the board class, especially userTouch to
solve the problem of pieces that couldn't move.
This commit is contained in:
manon 2025-04-23 11:49:41 +02:00
parent 67832c4767
commit 2d0f562795
2 changed files with 56 additions and 6 deletions

View File

@ -1,2 +1,3 @@
eclipse.preferences.version=1
encoding//src/backend/Board.java=UTF-8
encoding/<project>=windows-1252

View File

@ -8,10 +8,14 @@ public class Board {
private int turnNumber;
private boolean isWhiteTurn;
ArrayList<Piece> pieces = new ArrayList<>();
private int selectedX = -1;
private int selectedY = -1;
public Board(int colNum, int lineNum) {
this.colNum=colNum;
this.lineNum=lineNum;
this.colNum = colNum;
this.lineNum = lineNum;
this.turnNumber = 0;
this.isWhiteTurn = true; // White starts first
}
public int getWidth() {
@ -107,15 +111,60 @@ public class Board {
public ArrayList<Piece> getPieces() {
return pieces;
}
public Piece getPieceAt(int x, int y) {
Piece foundPiece = null;
for (int i = 0; i < pieces.size(); i++) {
Piece piece = pieces.get(i);
if (piece.getX() == x && piece.getY() == y) {
foundPiece = piece;
}
}
return foundPiece;
}
public void userTouch(int x, int y) {
//TODO
System.out.println("userTouch triggered at: " + x + ", " + y);
Piece selectedPiece = getPieceAt(selectedX, selectedY);
Piece clickedPiece = getPieceAt(x, y);
if (selectedPiece == null) {
if (clickedPiece != null && clickedPiece.isWhite() == isWhiteTurn) {
System.out.println("Selecting piece at: " + x + ", " + y);
selectedX = x;
selectedY = y;
} else {
System.out.println("No valid piece to select at: " + x + ", " + y);
}
return;
}
if (x == selectedX && y == selectedY) {
System.out.println("Unselecting piece at: " + x + ", " + y);
selectedX = -1;
selectedY = -1;
return;
}
if (clickedPiece != null) {
System.out.println("Capturing piece at: " + x + ", " + y);
pieces.remove(clickedPiece);
}
System.out.println("Moving piece to: " + x + ", " + y);
selectedPiece.setX(x);
selectedPiece.setY(y);
turnNumber++;
isWhiteTurn = !isWhiteTurn;
selectedX = -1;
selectedY = -1;
}
public boolean isSelected(int x, int y) {
//TODO
return false;
return x == selectedX && y == selectedY;
}
/* saving-loading feature :*/