isSelected and userTouch is done and modification on toString

This commit is contained in:
carol 2025-05-11 12:49:06 +02:00
parent 093ce11e8a
commit 4908f3fc43
2 changed files with 74 additions and 6 deletions

View File

@ -7,6 +7,11 @@ public class Board {
public int width; public int width;
public int height; public int height;
private Piece[][] board; private Piece[][] board;
private boolean hasSelectedPiece = false;
private int selectedX = -1;
private int selectedY = -1;
private int turnNumber = 0;
private boolean turnWhite = true;
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width = colNum; this.width = colNum;
@ -107,16 +112,70 @@ public class Board {
} }
} }
return pieces; return pieces;
} }
public void userTouch(int x, int y) {
// TODO
}
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
return false; return hasSelectedPiece && selectedX == x && selectedY == y;
} }
public void userTouch(int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height) return;
Piece clicked = board[x][y];
if (!hasSelectedPiece) {
if (clicked != null && clicked.isWhite() == turnWhite) {
// Select piece
selectedX = x;
selectedY = y;
hasSelectedPiece = true;
}
} else {
if (selectedX == x && selectedY == y) {
// Unselect
hasSelectedPiece = false;
} else {
Piece selectedPiece = board[selectedX][selectedY];
if (selectedPiece != null) {
// Capture if needed
if (board[x][y] != null) {
if (board[x][y].isWhite() == selectedPiece.isWhite()) {
// Can't capture own piece
hasSelectedPiece = false;
return;
}
}
// Move piece
board[x][y] = selectedPiece;
board[selectedX][selectedY] = null;
selectedPiece.setX(x);
selectedPiece.setY(y);
// Switch turn
turnWhite = !turnWhite;
turnNumber++;
// Unselect
hasSelectedPiece = false;
}
}
}
}
//public boolean isSelected(int x, int y) {
//return false
// }
/* saving-loading feature */ /* saving-loading feature */
public String[] toFileRep() { public String[] toFileRep() {
return null; return null;

View File

@ -23,6 +23,15 @@ public class Piece {
return y; return y;
} }
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public PieceType getType() { public PieceType getType() {
return type; return type;