Compare commits
2 Commits
e4561f42ab
...
811d9a28d5
| Author | SHA1 | Date |
|---|---|---|
|
|
811d9a28d5 | |
|
|
0c88880e81 |
|
|
@ -1,19 +1,15 @@
|
|||
package backend;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
|
||||
public class Board extends JPanel {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private int width;
|
||||
public class Board {
|
||||
private int width;
|
||||
private int height;
|
||||
private Piece[][] board;//creating an array for the coordinates of the board
|
||||
private Piece chosenPiece=null;
|
||||
private int turnNumber=0;
|
||||
private boolean isTurnWhite=true;
|
||||
private ArrayList<int[]> highlightedSquares= new ArrayList<>();
|
||||
// private ArrayList<int[]> highlightedSquares= new ArrayList<>();
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.width=colNum;
|
||||
|
|
@ -38,7 +34,7 @@ public class Board extends JPanel {
|
|||
}
|
||||
|
||||
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() {
|
||||
|
|
@ -56,7 +52,6 @@ public class Board extends JPanel {
|
|||
board[x][7]= new Piece(x, 7, backRowPieces[x], true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -84,7 +79,6 @@ public class Board extends JPanel {
|
|||
}
|
||||
|
||||
return builder.toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -101,72 +95,52 @@ public class Board extends JPanel {
|
|||
}
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
// Get the clicked piece from the board
|
||||
Piece clickedPiece = getPiece(x, y);
|
||||
|
||||
if (clickedPiece != null) {
|
||||
// Initialize the MovePiece with the clicked piece and the current board
|
||||
MovePiece movePiece = new MovePiece(clickedPiece, this);
|
||||
|
||||
// Call to add the possible moves
|
||||
movePiece.addPossibleMoves();
|
||||
}
|
||||
Piece clickedPiece=board[x][y];
|
||||
if(chosenPiece==null) {
|
||||
//when no piece is selected
|
||||
if(clickedPiece!=null && clickedPiece.isWhite()==isTurnWhite) {
|
||||
chosenPiece=clickedPiece;
|
||||
}
|
||||
}
|
||||
else { //if a piece is already selected
|
||||
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
|
||||
return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
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
|
||||
return board[x][y]; //returns the piece which is currently on this coordinates
|
||||
}
|
||||
return null;
|
||||
}
|
||||
*/
|
||||
|
||||
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
|
||||
|
|
@ -177,7 +151,6 @@ public class Board extends JPanel {
|
|||
// Update its internal coordinates
|
||||
piece.setX(newX);
|
||||
piece.setY(newY);
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
/* saving-loading feature :*/
|
||||
|
|
@ -195,13 +168,9 @@ public class Board extends JPanel {
|
|||
/* The following methods require more work ! */
|
||||
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
for (int[] square : highlightedSquares) {
|
||||
if (square[0] == x && square[1] == y) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//return highlightedSquares.contains(Point(x,y));
|
||||
return true; }
|
||||
|
||||
|
||||
public void undoLastMove() {
|
||||
//TODO
|
||||
|
|
@ -219,4 +188,3 @@ public class Board extends JPanel {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package backend;
|
||||
/*package backend;
|
||||
|
||||
public class HighlightManager {
|
||||
|
||||
|
|
@ -153,7 +153,7 @@ public class HighlightManager {
|
|||
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -4,14 +4,14 @@ public class MovePiece {
|
|||
|
||||
private Piece piece;
|
||||
private Board board;
|
||||
private HighlightManager highlightManager;
|
||||
// private HighlightManager highlightManager;
|
||||
|
||||
|
||||
|
||||
public MovePiece(Piece piece, Board board) {
|
||||
this.piece = piece;
|
||||
this.board = board;
|
||||
this.highlightManager = new HighlightManager(board);
|
||||
// this.highlightManager = new HighlightManager(board);
|
||||
}
|
||||
|
||||
//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
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void addPossibleMoves() {
|
||||
/* public void addPossibleMoves() {
|
||||
switch (piece.getType()) {
|
||||
case Pawn:highlightManager.highlightPawnMoves(piece);
|
||||
case Rook:highlightManager.highlightRookMoves(piece);
|
||||
|
|
@ -183,7 +183,7 @@ public class MovePiece {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
|
||||
public class random_file_to_delete {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue