From 14f4379ebf4c06b9b4f5b24179b1f4a97969d5e4 Mon Sep 17 00:00:00 2001 From: Marleen Peterson Date: Sun, 18 May 2025 15:09:50 +0200 Subject: [PATCH] Added debugging system to print the board state in the console --- src/Main.java | 28 +--------------------------- src/backend/Board.java | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/Main.java b/src/Main.java index d292c39..ea7427f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -9,6 +9,7 @@ public class Main { // testing Board testBoard = new Board(8, 8); testBoard.populateBoard(); + System.out.println("Initial board:"); System.out.println(testBoard.toString()); System.out.println(testBoard.getWidth() + "*" + testBoard.getHeight()); @@ -16,32 +17,5 @@ public class Main { MyInterface mjf = new MyInterface(); mjf.setVisible(true); - //test critical game functions (quick verification) - System.out.println("\nTesting core functionality:"); - - //test piece movement - System.out.println("White pawn at (4,6) moves:"); - Piece pawn = testBoard.getBoardMatrix()[6][4]; - System.out.println(testBoard.getValidMoves(pawn, true)); - - //test turn management - System.out.println("\nCurrent turn: " + (testBoard.isTurnWhite() ? "White" : "Black")); - - //test save/load functionality - String[] savedGame = testBoard.toFileRep(); - System.out.println("\nSaved game turn: " + savedGame[8]); - - //test move execution - System.out.println("\nMaking a sample move (e2-e4):"); - testBoard.userTouch(4, 6); //select pawn - testBoard.userTouch(4, 4); //move pawn - System.out.println("Board after move:"); - System.out.println(testBoard.toString()); - - //test undo functionality - System.out.println("\nUndoing last move:"); - testBoard.undoLastMove(); - System.out.println("Board after undo:"); - System.out.println(testBoard.toString()); } } \ No newline at end of file diff --git a/src/backend/Board.java b/src/backend/Board.java index b877c8e..7ca6fac 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -61,6 +61,12 @@ public class Board { //places a piece on the board at the specified location public void setPiece(boolean isWhite, PieceType type, int x, int y) { board[y][x] = new Piece(x, y, type, isWhite); + + if (debugMode) { //for debugging moves shown in console + System.out.println("A piece was added:"); + System.out.println(this.toString()); + System.out.println("Next turn: " + (isTurnWhite() ? "White" : "Black")); + } } public void populateBoard() { @@ -129,6 +135,8 @@ public class Board { } return sb.toString(); } + + private boolean debugMode = true; //showing debugging info public void userTouch(int x, int y) { if (selectedPiece == null) { @@ -156,6 +164,12 @@ public class Board { selectedPiece = null; highlightedPositions.clear(); } + + if (debugMode) { //for debugging moves shown in console + System.out.println("Move executed:"); + System.out.println(this.toString()); + System.out.println("Next turn: " + (isTurnWhite() ? "White" : "Black")); + } } public boolean isSelected(int x, int y) { //if the specified position is the currently selected piece's position @@ -412,6 +426,12 @@ public class Board { board[cy][cx] = new Piece(cx, cy, last.capturedPiece.getType(), last.capturedPiece.isWhite()); } } + + if (debugMode) { //for debugging moves shown in console + System.out.println("Undo the last move executed:"); + System.out.println(this.toString()); + System.out.println("Next turn: " + (isTurnWhite() ? "White" : "Black")); + } }