From 41eeb12bb3f585e424a2638676014d19411f1b3d Mon Sep 17 00:00:00 2001 From: USer Date: Thu, 15 May 2025 22:12:11 +0200 Subject: [PATCH] Setting up --- OOP_group1A1_project/src/Main.java | 3 +- .../src/backend/AutoPlayer.java | 2 +- OOP_group1A1_project/src/backend/Board.java | 211 ++++++++++++++++-- OOP_group1A1_project/src/backend/Game.java | 1 + OOP_group1A1_project/src/backend/Move.java | 2 +- 5 files changed, 197 insertions(+), 22 deletions(-) diff --git a/OOP_group1A1_project/src/Main.java b/OOP_group1A1_project/src/Main.java index 2d91725..f0266e9 100644 --- a/OOP_group1A1_project/src/Main.java +++ b/OOP_group1A1_project/src/Main.java @@ -1,4 +1,3 @@ - import backend.Board; import backend.Move; import backend.Piece; @@ -20,4 +19,4 @@ public class Main { mjf.setVisible(true); } -} +} \ No newline at end of file diff --git a/OOP_group1A1_project/src/backend/AutoPlayer.java b/OOP_group1A1_project/src/backend/AutoPlayer.java index a48c0c2..7caeb12 100644 --- a/OOP_group1A1_project/src/backend/AutoPlayer.java +++ b/OOP_group1A1_project/src/backend/AutoPlayer.java @@ -14,4 +14,4 @@ public class AutoPlayer { } -} +} \ No newline at end of file diff --git a/OOP_group1A1_project/src/backend/Board.java b/OOP_group1A1_project/src/backend/Board.java index 81f150e..13fbb16 100644 --- a/OOP_group1A1_project/src/backend/Board.java +++ b/OOP_group1A1_project/src/backend/Board.java @@ -7,11 +7,20 @@ public class Board { private int width; private int height; private ArrayList 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(); } - - public void userTouch(int x, int y) { - - } - - public boolean isSelected(int x, int y) { - //TODO - return false; + 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 it’s 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) { + // 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 it’s on‐board 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; + } + + // Ray‐tracing 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=0 && y