From 4c533827188ee48a590240e11adcd4cba54a464d Mon Sep 17 00:00:00 2001 From: VALENTINE GIRAL Date: Wed, 23 Apr 2025 12:24:56 +0200 Subject: [PATCH] no error in methods but pieces do not appear on the board yet --- src/Main.java | 5 ++- src/backend/Board.java | 63 ++++++++++++++++++++++++++++++-------- src/backend/Piece.java | 14 +++++++-- src/backend/PieceType.java | 4 +++ 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/Main.java b/src/Main.java index 870cae7..7d0a4fa 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,7 +4,6 @@ import backend.Piece; import backend.PieceType; import windowInterface.MyInterface; - public class Main { @@ -17,6 +16,10 @@ public class Main { // launches graphical interface : MyInterface mjf = new MyInterface(); mjf.setVisible(true); + + Piece test = new Piece(true, PieceType.Pawn, 1, 1); + System.out.println(test.isWhite()); } + } diff --git a/src/backend/Board.java b/src/backend/Board.java index 8a3cde3..c6f8ea3 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -5,10 +5,12 @@ import java.util.ArrayList; public class Board { private int line; private int col; + private Piece[][] board; // 2D array to hold pieces public Board(int colNum, int lineNum) { this.col=colNum; this.line=lineNum; + this.board =new Piece [colNum][lineNum];// Create the 2D board array } public int getWidth() { @@ -30,39 +32,76 @@ public class Board { } public void setPiece(boolean isWhite, PieceType type, int x, int y) { - //TODO + //TODO datastructure treeset + // Create a new Piece object with the provided attributes + Piece piece =new Piece (isWhite, type,x,y); + + // Place the piece on the board at the specified position + if (x >= 0 && x < col && y >= 0 && y < line) { + board[y][x] = piece; + } + + else { + System.out.println("Invalid position"); + } } public void populateBoard() { - //TODO + PieceType[] backRow = { + PieceType.Rook, PieceType.Knight, PieceType.Bishop, + PieceType.Queen, PieceType.King, PieceType.Bishop, + PieceType.Knight, PieceType.Rook + }; + + // place black pieces (false = black) + for (int x=0;x<8;x++) { + // Row 0: black major pieces + setPiece(false,backRow[x],x,0); + // Row 1: black pawns + setPiece(false, PieceType.Pawn,x,1); + } + + // Place white pieces (true = white) + for (int x = 0; x < 8; x++) { + // Row 6: white pawns + setPiece(true, PieceType.Pawn, x, 6); + // Row 7: white major pieces + setPiece(true, backRow[x], x, 7); + } } - + public void cleanBoard() { - //TODO + // Iterate over each row and column and set the board position to null + for (int y = 0; y < line; y++) { + for (int x = 0; x < col; x++) { + board[y][x] = null; // Remove the piece + } + } + System.out.println("Board cleaned. All pieces removed."); } - + public String toString() { //TODO return ""; } - + public ArrayList getPieces() { ArrayList pieces = new ArrayList<>(); //TODO - + return pieces; } public void userTouch(int x, int y) { //TODO - + } public boolean isSelected(int x, int y) { //TODO return false; } - + /* saving-loading feature :*/ public String[] toFileRep() { @@ -74,7 +113,7 @@ public class Board { //TODO } - + /* The following methods require more work ! */ public boolean isHighlighted(int x, int y) { @@ -84,14 +123,14 @@ public class Board { public void undoLastMove() { //TODO - + } public Board(Board board) { //TODO } - + public void playMove(Move move) { //TODO diff --git a/src/backend/Piece.java b/src/backend/Piece.java index 9adfdb5..0c891c7 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -3,6 +3,8 @@ package backend; public class Piece { private int X; private int Y; + private PieceType type; + private boolean color; public int getX() { return this.X; @@ -13,11 +15,19 @@ public class Piece { } public PieceType getType() { - return null; + return this.type ; } public boolean isWhite() { - return false; + return color; + } + + // constructor + public Piece (boolean colorP, PieceType typeP, int xP, int yP) { + this.color=colorP; + this.type=typeP; + this.X=xP; + this.Y=yP; } } diff --git a/src/backend/PieceType.java b/src/backend/PieceType.java index baceab1..2f84427 100644 --- a/src/backend/PieceType.java +++ b/src/backend/PieceType.java @@ -25,4 +25,8 @@ public enum PieceType { return PieceType.Queen; } + // PieceType type = PieceType.Pawn; + // int n = type.ordinal(); + // gives the value of the enum list index corresponding to called piece + }