325 lines
10 KiB
Java
325 lines
10 KiB
Java
package backend;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
public class Board {
|
|
|
|
private int colNum;
|
|
private int lineNum;
|
|
public String board[][];
|
|
private PieceType type;
|
|
private ArrayList<Piece> pieces;
|
|
private ArrayList<Position> highlight = new ArrayList<>();
|
|
private int selectedX = -1; // Tracks selected column (-1 = no selection)
|
|
private int selectedY = -1; // Tracks selected row (-1 = no selection)
|
|
private int turnNumber = 0; // Starts at 0 (White's turn)
|
|
private boolean isWhiteTurn = true; // Track current player's turn
|
|
|
|
private char getPieceSymbol(Piece p) {
|
|
char symbol;
|
|
switch (p.getType()) {
|
|
case Pawn: symbol = 'P'; break;
|
|
case Rook: symbol = 'R'; break;
|
|
case Knight: symbol = 'N'; break;
|
|
case Bishop: symbol = 'B'; break;
|
|
case Queen: symbol = 'Q'; break;
|
|
case King: symbol = 'K'; break;
|
|
default: symbol = '?'; // Should never happen
|
|
}
|
|
return p.isWhite() ? symbol : Character.toLowerCase(symbol);
|
|
}
|
|
|
|
Piece getPieceAt(int x, int y) { //changed visibility again like line 32
|
|
for (Piece p : pieces) {
|
|
if (p.getX() == x && p.getY() == y) return p;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
static class Position { //was private initally but had to remove cos otherwise i couldn't acces it in chessrules (aka changed it to a package
|
|
int x, y;
|
|
Position(int x, int y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
}
|
|
|
|
public Board(int colNum, int lineNum) {
|
|
this.colNum = colNum;
|
|
this.lineNum = lineNum;
|
|
this.pieces = new ArrayList<>();
|
|
this.turnNumber = 0; // Initialize turn number
|
|
this.isWhiteTurn = true; // White starts first
|
|
}
|
|
|
|
public int getWidth() {
|
|
return lineNum;
|
|
}
|
|
|
|
public int getHeight() {
|
|
return colNum;
|
|
}
|
|
|
|
public int getTurnNumber() {
|
|
return turnNumber;
|
|
}
|
|
|
|
public boolean isTurnWhite() {
|
|
return isWhiteTurn;
|
|
}
|
|
|
|
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
|
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
|
|
pieces.add(new Piece(isWhite, type, x, y));
|
|
}
|
|
|
|
public void populateBoard() {
|
|
for (int x = 0; x < 8; x++) {
|
|
setPiece(false, PieceType.Pawn, x, 1);
|
|
setPiece(true, PieceType.Pawn, x, 6);
|
|
}
|
|
setPiece(false, PieceType.Rook, 0, 0);
|
|
setPiece(false, PieceType.Rook, 7, 0);
|
|
setPiece(true, PieceType.Rook, 0, 7);
|
|
setPiece(true, PieceType.Rook, 7, 7);
|
|
|
|
setPiece(false, PieceType.Knight, 1, 0);
|
|
setPiece(false, PieceType.Knight, 6, 0);
|
|
setPiece(true, PieceType.Knight, 1, 7);
|
|
setPiece(true, PieceType.Knight, 6, 7);
|
|
|
|
setPiece(false, PieceType.Bishop, 2, 0);
|
|
setPiece(false, PieceType.Bishop, 5, 0);
|
|
setPiece(true, PieceType.Bishop, 2, 7);
|
|
setPiece(true, PieceType.Bishop, 5, 7);
|
|
|
|
setPiece(false, PieceType.Queen, 3, 0);
|
|
setPiece(true, PieceType.Queen, 3, 7);
|
|
|
|
setPiece(false, PieceType.King, 4, 0);
|
|
setPiece(true, PieceType.King, 4, 7);
|
|
}
|
|
|
|
public void cleanBoard() {
|
|
pieces.clear(); // Remove all pieces from the board
|
|
}
|
|
|
|
public String toString() {
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
char[][] grid = new char[lineNum][colNum];
|
|
for (int y = 0; y < lineNum; y++) {
|
|
for (int x = 0; x < colNum; x++) {
|
|
grid[y][x] = '.';
|
|
}
|
|
}
|
|
|
|
for (Piece p : pieces) {
|
|
int x = p.getX();
|
|
int y = p.getY();
|
|
if (x >= 0 && x < colNum && y >= 0 && y < lineNum) {
|
|
grid[y][x] = getPieceSymbol(p);
|
|
}
|
|
}
|
|
|
|
sb.append(" ");
|
|
for (int x = 0; x < colNum; x++) {
|
|
sb.append((char) ('a' + x)).append(' ');
|
|
}
|
|
sb.append('\n');
|
|
|
|
for (int y = 0; y < lineNum; y++) {
|
|
sb.append(lineNum - y).append(' ');
|
|
for (int x = 0; x < colNum; x++) {
|
|
sb.append(grid[y][x]).append(' ');
|
|
}
|
|
sb.append('\n');
|
|
}
|
|
|
|
return sb.toString();
|
|
}
|
|
|
|
public ArrayList<Piece> getPieces() {
|
|
return pieces;
|
|
}
|
|
|
|
public void userTouch(int x, int y) {
|
|
if (selectedX != -1 && x >= 0 && x < colNum && y >= 0 && y < lineNum && isHighlighted(x, y)) {
|
|
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
|
if (selectedPiece != null) {
|
|
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
|
|
selectedPiece.setX(x);
|
|
selectedPiece.setY(y);
|
|
|
|
turnNumber++;
|
|
isWhiteTurn = !isWhiteTurn;
|
|
}
|
|
|
|
selectedX = -1;
|
|
selectedY = -1;
|
|
highlight.clear();
|
|
return;
|
|
}
|
|
|
|
Piece p = getPieceAt(x, y);
|
|
if (p != null && p.isWhite() == isWhiteTurn) {
|
|
selectedX = x;
|
|
selectedY = y;
|
|
|
|
highlight.clear();
|
|
highlight.addAll(findMoves(p));
|
|
} else {
|
|
selectedX = -1;
|
|
selectedY = -1;
|
|
highlight.clear();
|
|
}
|
|
}
|
|
|
|
public boolean isSelected(int x, int y) {
|
|
return (x == selectedX && y == selectedY);
|
|
}
|
|
|
|
public String[] toFileRep() {
|
|
//TODO
|
|
return null;
|
|
}
|
|
|
|
public Board(String[] array) {
|
|
//TODO
|
|
}
|
|
|
|
public boolean isHighlighted(int x, int y) {
|
|
for (Position pos : highlight) {
|
|
if (pos.x == x && pos.y == y) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void undoLastMove() {
|
|
//TODO
|
|
}
|
|
|
|
public Board(Board board) {
|
|
//TODO
|
|
}
|
|
|
|
public void playMove(Move move) {
|
|
//TODO
|
|
}
|
|
|
|
ArrayList<Position> findMoves(Piece p) { //was private at first too but idem issue as for position
|
|
ArrayList<Position> moves = new ArrayList<>();
|
|
|
|
int x = p.getX();
|
|
int y = p.getY();
|
|
boolean isWhite = p.isWhite();
|
|
|
|
if (p.getType() == PieceType.Pawn) {
|
|
int direction = isWhite ? -1 : 1;
|
|
int startRow = isWhite ? 6 : 1;
|
|
|
|
if (x >= 0 && x < colNum && y + direction >= 0 && y + direction < lineNum &&
|
|
getPieceAt(x, y + direction) == null) {
|
|
moves.add(new Position(x, y + direction));
|
|
if (y == startRow && y + 2 * direction >= 0 && y + 2 * direction < lineNum &&
|
|
getPieceAt(x, y + 2 * direction) == null) {
|
|
moves.add(new Position(x, y + 2 * direction));
|
|
}
|
|
}
|
|
|
|
for (int dx = -1; dx <= 1; dx += 2) {
|
|
int nx = x + dx;
|
|
int ny = y + direction;
|
|
if (nx >= 0 && nx < colNum && ny >= 0 && ny < lineNum) {
|
|
Piece target = getPieceAt(nx, ny);
|
|
if (target != null && target.isWhite() != isWhite) {
|
|
moves.add(new Position(nx, ny));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (p.getType() == PieceType.Knight) {
|
|
int[][] jumps = {
|
|
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
|
|
{1, -2}, {1, 2}, {2, -1}, {2, 1}
|
|
};
|
|
|
|
for (int[] j : jumps) {
|
|
int nx = x + j[0];
|
|
int ny = y + j[1];
|
|
if (nx >= 0 && nx < colNum && ny >= 0 && ny < lineNum) {
|
|
Piece target = getPieceAt(nx, ny);
|
|
if (target == null || target.isWhite() != isWhite) {
|
|
moves.add(new Position(nx, ny));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (p.getType() == PieceType.Rook) {
|
|
extend(moves, x, y, isWhite, 1, 0);
|
|
extend(moves, x, y, isWhite, -1, 0);
|
|
extend(moves, x, y, isWhite, 0, 1);
|
|
extend(moves, x, y, isWhite, 0, -1);
|
|
}
|
|
|
|
if (p.getType() == PieceType.Bishop) {
|
|
extend(moves, x, y, isWhite, 1, 1);
|
|
extend(moves, x, y, isWhite, -1, 1);
|
|
extend(moves, x, y, isWhite, 1, -1);
|
|
extend(moves, x, y, isWhite, -1, -1);
|
|
}
|
|
|
|
if (p.getType() == PieceType.Queen) {
|
|
extend(moves, x, y, isWhite, 1, 0);
|
|
extend(moves, x, y, isWhite, -1, 0);
|
|
extend(moves, x, y, isWhite, 0, 1);
|
|
extend(moves, x, y, isWhite, 0, -1);
|
|
extend(moves, x, y, isWhite, 1, 1);
|
|
extend(moves, x, y, isWhite, -1, 1);
|
|
extend(moves, x, y, isWhite, 1, -1);
|
|
extend(moves, x, y, isWhite, -1, -1);
|
|
}
|
|
|
|
if (p.getType() == PieceType.King) {
|
|
for (int dx = -1; dx <= 1; dx++) {
|
|
for (int dy = -1; dy <= 1; dy++) {
|
|
int nx = x + dx;
|
|
int ny = y + dy;
|
|
if (nx >= 0 && nx < colNum && ny >= 0 && ny < lineNum) {
|
|
Piece target = getPieceAt(nx, ny);
|
|
if (target == null || target.isWhite() != isWhite) {
|
|
moves.add(new Position(nx, ny));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return moves;
|
|
}
|
|
|
|
private void extend(ArrayList<Position> moves, int x, int y, boolean isWhite, int dx, int dy) {
|
|
int nx = x + dx;
|
|
int ny = y + dy;
|
|
while (nx >= 0 && nx < colNum && ny >= 0 && ny < lineNum) {
|
|
Piece target = getPieceAt(nx, ny);
|
|
if (target == null) {
|
|
moves.add(new Position(nx, ny));
|
|
} else if (target.isWhite() != isWhite) {
|
|
moves.add(new Position(nx, ny));
|
|
break;
|
|
} else {
|
|
break;
|
|
}
|
|
nx += dx;
|
|
ny += dy;
|
|
}
|
|
}
|
|
|
|
|
|
}
|