Setting up

This commit is contained in:
USer 2025-05-15 22:12:11 +02:00
parent 4203e9f88f
commit 41eeb12bb3
5 changed files with 197 additions and 22 deletions

View File

@ -1,4 +1,3 @@
import backend.Board;
import backend.Move;
import backend.Piece;

View File

@ -7,11 +7,20 @@ public class Board {
private int width;
private int height;
private ArrayList<Piece> Pieces;
private boolean[][] highlighted;
// NEW FIELDS FOR userTouch
private boolean hasSelection = false; // did we already click to pick up a piece?
private int selectedX, selectedY; // if so, which square is in hand?
private int turnNumber = 0; // how many half-moves have been played?
private boolean turnWhite = true; // true = White to play, false = Black
//
public Board(int width, int height) {
this.width = width;
this.height = height;
this.Pieces = new ArrayList<>();
this.highlighted = new boolean[width][height];
}
public int getWidth() {
@ -23,20 +32,26 @@ public class Board {
}
public int getTurnNumber() {
//TODO
return 0;
return turnNumber;
}
public boolean isTurnWhite() {
//TODO
return false;
return turnWhite;
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
//TODO
Piece newPiece = new Piece(x, y, isWhite, type);
Pieces.add(newPiece);
// 1) Remove any piece already at this square
for (int i = 0; i < Pieces.size(); i++) {
Piece p = Pieces.get(i);
if (p.getX() == x && p.getY() == y) {
Pieces.remove(i);
break;
}
}
// 2) Add the new piece
Pieces.add(new Piece(x, y, isWhite, type));
}
public void populateBoard() {
@ -119,16 +134,176 @@ public class Board {
return sb.toString();
}
private void clearHighlights() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
highlighted[i][j] = false;
}
}
}
public void userTouch(int x, int y) {
// 1) Find if you clicked on a piece at (x,y)
Piece clicked = null;
for (int i = 0; i < Pieces.size(); i++) {
Piece p = Pieces.get(i);
if (p.getX() == x && p.getY() == y) {
// compare the piece coordinate to the square clicked coordinate
clicked = p;
break;
}
}
// 2) If no piece is currently selected, try to select one
if (!hasSelection) { // hasSeletion is false at the beginning
if (clicked != null && clicked.isWhite() == turnWhite) {
// mark that we have a selection
hasSelection = true;
selectedX = x;
selectedY = y;
// compute and show legal moves:
clearHighlights();
computeHighlights(clicked);
}
return;
}
// 3) If you click the same square twice cancel selection and clear highlights
if (selectedX == x && selectedY == y) {
hasSelection = false;
clearHighlights();
return;
}
// 4) Otherwise its a move: but only if (x,y) was highlighted
if (!isHighlighted(x, y)) {
// if clicked somewhere illegal, ignore it (keep your piece in hand)
return;
}
// 5) Now perform the capture+relocate exactly as before
Pieces.removeIf(p -> p.getX() == x && p.getY() == y);
Piece toMove = null;
for (Piece p : Pieces) {
if (p.getX() == selectedX && p.getY() == selectedY) {
toMove = p;
break;
}
}
if (toMove != null) {
Pieces.remove(toMove);
Pieces.add(new Piece(x, y, toMove.isWhite(), toMove.getType()));
}
// 6) Advance turn and clear selection/highlights
turnNumber++;
turnWhite = !turnWhite;
hasSelection = false;
clearHighlights();
}
public boolean isSelected(int x, int y) {
//TODO
return false;
// return true if the exact square is currently picked up
return hasSelection && selectedX == x && selectedY == y;
// hasSelection is true the moment the user clicks on a piece for the first time
}
// Fill highlighted[][] = true for every square this piece could legally move to
private void computeHighlights(Piece p) {
PieceType type = p.getType();
int sx = p.getX(), sy = p.getY();
switch(type) {
case Pawn:
int dir = p.isWhite() ? -1 : +1; // white pawns move up the board (y), black down (y+)
// one step forward
markIfEmpty(sx, sy + dir);
// two-step from home rank
if ((p.isWhite() && sy == height-2) || (!p.isWhite() && sy == 1)) {
markIfEmpty(sx, sy + 2*dir);
}
// captures
markIfEnemy(sx-1, sy+dir, p.isWhite());
markIfEnemy(sx+1, sy+dir, p.isWhite());
break;
case Knight:
int[][] offsets = {{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
for (int[] o: offsets) markIfFreeOrEnemy(sx+o[0], sy+o[1], p.isWhite());
break;
case Bishop:
slide(sx, sy, 1,1, p.isWhite());
slide(sx, sy, 1,-1, p.isWhite());
slide(sx, sy,-1,1, p.isWhite());
slide(sx, sy,-1,-1, p.isWhite());
break;
case Rook:
slide(sx, sy, 1,0, p.isWhite());
slide(sx, sy,-1,0, p.isWhite());
slide(sx, sy, 0,1, p.isWhite());
slide(sx, sy, 0,-1, p.isWhite());
break;
case Queen:
// combine rook + bishop
computeHighlights(new Piece(sx,sy,p.isWhite(),PieceType.Bishop));
computeHighlights(new Piece(sx,sy,p.isWhite(),PieceType.Rook));
break;
case King:
for (int dx=-1; dx<=1; dx++)
for (int dy=-1; dy<=1; dy++)
if (!(dx==0 && dy==0))
markIfFreeOrEnemy(sx+dx, sy+dy, p.isWhite());
break;
}
}
// Helpers to mark a square if its onboard and empty / enemy:
private void markIfEmpty(int x, int y) {
if (onBoard(x,y) && pieceAt(x,y)==null) highlighted[x][y] = true;
}
private void markIfEnemy(int x, int y, boolean isWhite) {
Piece q = pieceAt(x,y);
if (q!=null && q.isWhite()!=isWhite) highlighted[x][y] = true;
}
private void markIfFreeOrEnemy(int x, int y, boolean isWhite) {
if (!onBoard(x,y)) return;
Piece q = pieceAt(x,y);
if (q == null || q.isWhite() != isWhite) highlighted[x][y] = true;
}
// Raytracing for sliding pieces (rook, bishop, queen):
private void slide(int sx, int sy, int dx, int dy, boolean isWhite) {
int x = sx+dx, y = sy+dy;
while(onBoard(x,y)) {
Piece q = pieceAt(x,y);
if (q == null) {
highlighted[x][y] = true;
} else {
if (q.isWhite() != isWhite) highlighted[x][y] = true;
break;
}
x += dx; y += dy;
}
}
// Utility to check bounds
private boolean onBoard(int x, int y) {
return x>=0 && x<width && y>=0 && y<height;
}
// Find a piece at (x,y) or null
private Piece pieceAt(int x, int y) {
for (Piece p : Pieces) {
if (p.getX()==x && p.getY()==y) return p;
}
return null;
}
/* saving-loading feature :*/
public String[] toFileRep() {
@ -144,8 +319,8 @@ public class Board {
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
//TODO
return false;
// only highlight when a piece is selected and this flag was set
return highlighted[x][y];
}
public void undoLastMove() {

View File

@ -1,3 +1,4 @@
package backend;
import windowInterface.MyInterface;