Started working on chessrules class for castling. Not done at all not

good at all, needs to be for all pieces not just castling.
Changed in board class lines 32, 39, 212 from private to defaylt cos
needed to be accessible by the new chessrules class. Added piecemove
counter in piece class
This commit is contained in:
Juliette 2025-05-21 21:01:56 +02:00
parent 2480117d6c
commit fb2ab062a8
4 changed files with 28 additions and 76 deletions

View File

@ -1,9 +0,0 @@
BR,BN,BB,BQ,BK,BB,BN,BR
BP,BP,BP,BP,BP,BP,BP,BP
,,,,,,,
,,,,,,,
,,,WP,,,,
,,,,,,,
WP,WP,WP,,WP,WP,WP,WP
WR,WN,WB,WQ,WK,WB,WN,WR
B

View File

@ -10,10 +10,10 @@ public class Board {
private PieceType type;
private ArrayList<Piece> pieces;
private ArrayList<Position> highlight = new ArrayList<>();
private int selectedX = -1;
private int selectedY = -1;
private int turnNumber = 0;
private boolean isWhiteTurn = true;
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;
@ -24,19 +24,19 @@ public class Board {
case Bishop: symbol = 'B'; break;
case Queen: symbol = 'Q'; break;
case King: symbol = 'K'; break;
default: symbol = '?';
default: symbol = '?'; // Should never happen
}
return p.isWhite() ? symbol : Character.toLowerCase(symbol);
}
private Piece getPieceAt(int x, int y) {
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;
}
private static class Position {
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;
@ -48,8 +48,8 @@ public class Board {
this.colNum = colNum;
this.lineNum = lineNum;
this.pieces = new ArrayList<>();
this.turnNumber = 0;
this.isWhiteTurn = true;
this.turnNumber = 0; // Initialize turn number
this.isWhiteTurn = true; // White starts first
}
public int getWidth() {
@ -101,7 +101,7 @@ public class Board {
}
public void cleanBoard() {
pieces.clear();
pieces.clear(); // Remove all pieces from the board
}
public String toString() {
@ -180,63 +180,12 @@ public class Board {
}
public String[] toFileRep() {
String[] fileRep = new String[lineNum + 1];
for (int y = 0; y < lineNum; y++) {
StringBuilder rowBuilder = new StringBuilder();
for (int x = 0; x < colNum; x++) {
Piece p = getPieceAt(x, y);
if (p != null) {
char colorChar = p.isWhite() ? 'W' : 'B';
char typeChar = p.getType().getSummary().charAt(0);
rowBuilder.append(colorChar).append(typeChar);
}
if (x < colNum - 1) rowBuilder.append(",");
}
fileRep[y] = rowBuilder.toString();
}
fileRep[lineNum] = isWhiteTurn ? "W" : "B";
return fileRep;
//TODO
return null;
}
public Board(String[] fileRepresentation) {
if (fileRepresentation == null || fileRepresentation.length < 1) {
throw new IllegalArgumentException("Invalid file format");
}
int totalRows = fileRepresentation.length - 1;
this.lineNum = totalRows;
String[] firstRow = fileRepresentation[0].split(",", -1);
this.colNum = firstRow.length;
this.pieces = new ArrayList<>();
for (int y = 0; y < lineNum; y++) {
String[] squares = fileRepresentation[y].split(",", -1);
if (squares.length != colNum) {
throw new IllegalArgumentException("Row " + y + " has invalid column count");
}
for (int x = 0; x < colNum; x++) {
String square = squares[x].trim();
if (!square.isEmpty()) {
if (square.length() != 2) {
throw new IllegalArgumentException("Invalid piece format: " + square);
}
boolean isWhite = square.charAt(0) == 'W';
char typeChar = square.charAt(1);
PieceType type = PieceType.fromSummary(typeChar);
pieces.add(new Piece(isWhite, type, x, y));
}
}
}
String turnLine = fileRepresentation[fileRepresentation.length - 1].trim();
if (turnLine.isEmpty()) {
throw new IllegalArgumentException("Turn line is missing");
}
this.isWhiteTurn = turnLine.charAt(0) == 'W';
this.turnNumber = 0;
public Board(String[] array) {
//TODO
}
public boolean isHighlighted(int x, int y) {
@ -260,7 +209,7 @@ public class Board {
//TODO
}
private ArrayList<Position> findMoves(Piece p) {
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();
@ -370,4 +319,6 @@ public class Board {
ny += dy;
}
}
}
}

View File

@ -2,4 +2,5 @@ package backend;
public class Move {
}

View File

@ -8,6 +8,7 @@ public class Piece {
private PieceType type;
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
private int moveNumber;
//
public Piece() {
@ -44,4 +45,12 @@ public class Piece {
return color;
}
public int getmoveNumber() {
return moveNumber;
}
public void moveNumberAdd () {
moveNumber++;
}
}