From df2bf3df9f82f655ca933935d8352927cf6d4e9d Mon Sep 17 00:00:00 2001 From: eliot Date: Wed, 7 May 2025 14:32:53 +0200 Subject: [PATCH] board initialisation (and test) --- src/Main.java | 7 +-- src/backend/Board.java | 104 ++++++++++++++++++++++++++++++++++++----- src/backend/Piece.java | 12 +++++ 3 files changed, 109 insertions(+), 14 deletions(-) diff --git a/src/Main.java b/src/Main.java index e2f0697..15ee119 100644 --- a/src/Main.java +++ b/src/Main.java @@ -10,17 +10,18 @@ public class Main { public static void main(String[] args) { // testing : - Board testBoard = new Board(9, 8); + Board testBoard = new Board(8, 8); testBoard.populateBoard(); testBoard.getHeight(); testBoard.getWidth(); System.out.println(testBoard.toString()); + testBoard.populateBoard(); - Piece testPiece = new Piece(); + /**Piece testPiece = new Piece(true, PieceType.Rook, 2,3); testPiece.getX(); testPiece.getY(); testPiece.getType(); - testPiece.isWhite(); + testPiece.isWhite();*/ // launches graphical interface : diff --git a/src/backend/Board.java b/src/backend/Board.java index 5d4713a..f03967f 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -8,16 +8,23 @@ public class Board { private int colNum; private int lineNum; + public String board[][]; + private PieceType type; + private ArrayList pieces; + public Board(int colNum, int lineNum) { this.colNum = colNum; this.lineNum = lineNum; - String board[][] = new String[lineNum][colNum]; - for (int i = 0; i < lineNum; i++) { - for (int j = 0; j < colNum; j++) { - board[i][j] = null; - } - } + this.pieces = new ArrayList<>(); + + //board = new String[lineNum][colNum]; + // for (int i = 0; i < lineNum; i++) { + // for (int j = 0; j < colNum; j++) { + // board[i][j] = null; + // } + // } + } public int getWidth() { @@ -41,26 +48,101 @@ public class Board { } public void setPiece(boolean isWhite, PieceType type, int x, int y) { + // Remove existing piece at coordinates (x,y) + pieces.removeIf(p -> p.getX() == x && p.getY() == y); + // Add new piece + pieces.add(new Piece(isWhite, type, x, y)); //TODO } + + ; + public void populateBoard() { - //TODO + 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 //TODO } public String toString() { + StringBuilder sb = new StringBuilder(); + + // Header (column labels) + sb.append(" "); + for (int x = 0; x < colNum; x++) { + sb.append((char)('a' + x)).append(" "); + } + sb.append("\n"); + + // Board rows (8 to 1, since chess starts from bottom) + for (int y = 0; y < lineNum; y++) { + sb.append(lineNum - y).append(" "); // Row number (8 to 1) + + for (int x = 0; x < colNum; x++) { + boolean pieceFound = false; + + // Check if a piece exists at (x, y) + for (Piece p : pieces) { + if (p.getX() == x && p.getY() == y) { + char pieceChar = getPieceSymbol(p); + sb.append(pieceChar).append(" "); + pieceFound = true; + break; + } + } + + // If no piece, add a dot (.) + if (!pieceFound) { + sb.append(". "); + } + } + sb.append("\n"); + } //TODO - return ""; + return sb.toString(); + } + // Helper method to convert Piece to symbol (e.g., 'K' for white king) + 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); } public ArrayList getPieces() { - ArrayList pieces = new ArrayList<>(); - //TODO - return pieces; } diff --git a/src/backend/Piece.java b/src/backend/Piece.java index a899a97..a05dd13 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -7,7 +7,19 @@ public class Piece { private int y; private PieceType type; + // + public Piece() { + // Leave empty or initialize default values + } + // + public Piece(boolean isWhite, PieceType type, int x, int y) { + this.color = isWhite; + this.type = type; + this.x = x; + this.y = y; + } + public int getX() { System.out.println("The X value is " + x);