Compare commits

...

2 Commits

Author SHA1 Message Date
keshi 811d9a28d5 remove marietta's edit to try highlight again 2025-05-11 16:08:57 +02:00
keshi 0c88880e81 currently working on highlights again cuz it didnt work 2025-05-11 16:02:42 +02:00
4 changed files with 51 additions and 92 deletions

View File

@ -1,19 +1,15 @@
package backend; package backend;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.JPanel;
public class Board {
public class Board extends JPanel { private int width;
private static final long serialVersionUID = 1L;
private int width;
private int height; private int height;
private Piece[][] board;//creating an array for the coordinates of the board private Piece[][] board;//creating an array for the coordinates of the board
private Piece chosenPiece=null; private Piece chosenPiece=null;
private int turnNumber=0; private int turnNumber=0;
private boolean isTurnWhite=true; private boolean isTurnWhite=true;
private ArrayList<int[]> highlightedSquares= new ArrayList<>(); // private ArrayList<int[]> highlightedSquares= new ArrayList<>();
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width=colNum; this.width=colNum;
@ -38,7 +34,7 @@ public class Board extends JPanel {
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
board[x][y]= new Piece (x, y, type, isWhite); board[y][x]= new Piece (x, y, type, isWhite);
} }
public void populateBoard() { public void populateBoard() {
@ -55,7 +51,6 @@ public class Board extends JPanel {
board[x][0]= new Piece(x, 0, backRowPieces[x], false); board[x][0]= new Piece(x, 0, backRowPieces[x], false);
board[x][7]= new Piece(x, 7, backRowPieces[x], true); board[x][7]= new Piece(x, 7, backRowPieces[x], true);
} }
} }
@ -84,7 +79,6 @@ public class Board extends JPanel {
} }
return builder.toString(); return builder.toString();
} }
@ -101,72 +95,52 @@ public class Board extends JPanel {
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
// Get the clicked piece from the board Piece clickedPiece=board[x][y];
Piece clickedPiece = getPiece(x, y); if(chosenPiece==null) {
//when no piece is selected
if (clickedPiece != null) { if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) {
// Initialize the MovePiece with the clicked piece and the current board chosenPiece=clickedPiece;
MovePiece movePiece = new MovePiece(clickedPiece, this); }
}
// Call to add the possible moves else { //if a piece is already selected
movePiece.addPossibleMoves(); if(isSelected(x,y)) {
} chosenPiece=null; //unselect the chosen piece to revert to normal state
}
else { //move selected piece to new position using MovePiece logics
MovePiece movement= new MovePiece(chosenPiece, this);
boolean movementSuccess=false;
if(chosenPiece.getType()==PieceType.Pawn) {
movementSuccess=movement.movePawn(x,y);
} else if (chosenPiece.getType()==PieceType.Rook) {
movementSuccess=movement.moveRook(x, y);
}else if (chosenPiece.getType()==PieceType.Bishop) {
movementSuccess=movement.moveBishop(x, y);
}else if (chosenPiece.getType()==PieceType.King) {
movementSuccess=movement.moveKing(x, y);
}else if (chosenPiece.getType()==PieceType.Queen) {
movementSuccess=movement.moveQueen(x, y);
}
if(movementSuccess) {
chosenPiece=null;
turnNumber++;
isTurnWhite=! isTurnWhite;
} else { //invalid move
}
}
} }
public void addHighlight(int x, int y) {
highlightedSquares.add(new int[]{x, y});
repaint();
}
public Piece getPiece(int x, int y) {
if (x >= 0 && x < 8 && y >= 0 && y < 8) {
return board[x][y]; // Assuming `board` is your 2D array of Pieces
}
return null;
}
public ArrayList<int[]> getHighlightedSquares() {
return highlightedSquares;
}
public void refreshHighlights() {
super.repaint(); // Just call the original JPanel repaint
}
public void clearHighlights() {
highlightedSquares.clear();
repaint(); // This refreshes the board after clearing highlights
}
public void repaint() {
super.repaint(); // This calls the original JPanel repaint, not the same method again
}
public void revalidate() {
// TODO Auto-generated method stub
} }
public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates
return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y; return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y;
} }
/*
public Piece getPiece(int x, int y) { //returns figure and gives coordinates public Piece getPiece(int x, int y) { //returns figure and gives coordinates
if (x >= 0 && x < width && y >= 0 && y < height) { //checks if coordinates are inside the board if (x >= 0 && x < width && y >= 0 && y < height) { //checks if coordinates are inside the board
return board[x][y]; //returns the piece which is currently on this coordinates return board[x][y]; //returns the piece which is currently on this coordinates
} }
return null; return null;
} }
*/
public void movePiece(int oldX, int oldY, int newX, int newY) { public void movePiece(int oldX, int oldY, int newX, int newY) {
if (board[oldX][oldY] != null) { //checks if there is a piece at that position if (board[oldX][oldY] != null) { //checks if there is a piece at that position
@ -177,7 +151,6 @@ public class Board extends JPanel {
// Update its internal coordinates // Update its internal coordinates
piece.setX(newX); piece.setX(newX);
piece.setY(newY); piece.setY(newY);
this.repaint();
} }
} }
/* saving-loading feature :*/ /* saving-loading feature :*/
@ -193,16 +166,12 @@ public class Board extends JPanel {
} }
/* The following methods require more work ! */ /* The following methods require more work ! */
public boolean isHighlighted(int x, int y) { public boolean isHighlighted(int x, int y) {
for (int[] square : highlightedSquares) { //return highlightedSquares.contains(Point(x,y));
if (square[0] == x && square[1] == y) { return true; }
return true;
}
}
return false;
}
public void undoLastMove() { public void undoLastMove() {
//TODO //TODO
@ -218,5 +187,4 @@ public class Board extends JPanel {
} }
} }

View File

@ -1,4 +1,4 @@
package backend; /*package backend;
public class HighlightManager { public class HighlightManager {
@ -153,7 +153,7 @@ public class HighlightManager {
} }
*/

View File

@ -4,14 +4,14 @@ public class MovePiece {
private Piece piece; private Piece piece;
private Board board; private Board board;
private HighlightManager highlightManager; // private HighlightManager highlightManager;
public MovePiece(Piece piece, Board board) { public MovePiece(Piece piece, Board board) {
this.piece = piece; this.piece = piece;
this.board = board; this.board = board;
this.highlightManager = new HighlightManager(board); // this.highlightManager = new HighlightManager(board);
} }
//Pawn movement logic //Pawn movement logic
@ -160,17 +160,17 @@ public class MovePiece {
if ((stepX <= 1 && stepY <= 1) && !(stepX == 0 && stepY == 0)) { //moving 1 space in any direction if ((stepX <= 1 && stepY <= 1) && !(stepX == 0 && stepY == 0)) { //moving 1 space in any direction
Piece targetPiece = board.getPiece(x, y); Piece targetPiece = board.getPiece(x, y);
if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move is space is empty or capture if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move is space is empty o
board.movePiece(currentX, currentY, x, y); board.movePiece(currentX, currentY, x, y);
return true; return true;
} }
} }
return false; return false;
} }
}
/* public void addPossibleMoves() {
public void addPossibleMoves() {
switch (piece.getType()) { switch (piece.getType()) {
case Pawn:highlightManager.highlightPawnMoves(piece); case Pawn:highlightManager.highlightPawnMoves(piece);
case Rook:highlightManager.highlightRookMoves(piece); case Rook:highlightManager.highlightRookMoves(piece);
@ -183,7 +183,7 @@ public class MovePiece {
} }
} }
*/

View File

@ -1,9 +0,0 @@
public class random_file_to_delete {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}