From 59431ad622695ca5ab83d84c4262428bcafbb0eb Mon Sep 17 00:00:00 2001 From: Romain Murphy Date: Wed, 9 Apr 2025 10:21:18 +0200 Subject: [PATCH] board init --- OOP_2B1_Project/src/Main.java | 17 +++- OOP_2B1_Project/src/backend/Board.java | 85 +++++++++++++++---- OOP_2B1_Project/src/backend/Game.java | 4 +- OOP_2B1_Project/src/backend/Piece.java | 25 ++++-- .../src/windowInterface/JPanelChessBoard.java | 2 +- 5 files changed, 108 insertions(+), 25 deletions(-) diff --git a/OOP_2B1_Project/src/Main.java b/OOP_2B1_Project/src/Main.java index 671bb51..2ff297c 100644 --- a/OOP_2B1_Project/src/Main.java +++ b/OOP_2B1_Project/src/Main.java @@ -1,9 +1,22 @@ +import backend.Board; +import backend.Move; +import backend.Piece; +import backend.PieceType; +import windowInterface.MyInterface; + public class Main { - public static void main(String[] args) { - // TODO Auto-generated method stub + public static void main(String[] args) { + // testing : + Board testBoard = new Board(8, 8); + testBoard.populateBoard(); + System.out.println(testBoard.toString()); + + // launches graphical interface : + MyInterface mjf = new MyInterface(); + mjf.setVisible(true); } } diff --git a/OOP_2B1_Project/src/backend/Board.java b/OOP_2B1_Project/src/backend/Board.java index a5a588e..1bf00ca 100644 --- a/OOP_2B1_Project/src/backend/Board.java +++ b/OOP_2B1_Project/src/backend/Board.java @@ -3,19 +3,31 @@ package backend; import java.util.ArrayList; public class Board { - + public int width; + public int height; + public ArrayList> board = new ArrayList<>(); + private int selectPosition = 0; public Board(int colNum, int lineNum) { - //TODO + this.width = colNum; + this.height = lineNum; + int rows = 8; + int cols = 8; + for (int i = 0; i < rows; i++) { + ArrayList row = new ArrayList<>(); + for (int j = 0; j < cols; j++) { + row.add(null); // Fill with null + } + this.board.add(row); + } + } public int getWidth() { - //TODO - return 0; + return this.width; } public int getHeight() { - //TODO - return 0; + return this.height; } public int getTurnNumber() { @@ -28,32 +40,75 @@ public class Board { return false; } - public void setPiece(boolean isWhite, PieceType type, int x, int y) { - //TODO + public void setPiece(int x, int y, PieceType type, boolean isWhite) { + Piece piece = new Piece(x,y,type,isWhite); + board.get(y).set(x, piece); } public void populateBoard() { - //TODO + for (int x = 0; x < this.width;x++) { + for (int y = 0; y < this.height; y++) { + if (y ==1 || y == 6) { + if (y == 1) { + this.setPiece(x,y,PieceType.Pawn,false); + } + else if (y == 6) {this.setPiece(x,y,PieceType.Pawn,true);} + } + if (y == 0) { + boolean col = false; + if (x == 0 || x == 7) { + this.setPiece(x,y,PieceType.Rook,col); + } else if (x == 1 || x == 6) {this.setPiece(x,y,PieceType.Knight,col);} + else if (x == 2 || x == 5) {this.setPiece(x,y,PieceType.Bishop,col);} + else if (x == 3) {this.setPiece(x,y,PieceType.Queen,col);} + else if (x == 4) {this.setPiece(x,y,PieceType.King,col);} + } else if (y == 7) { + boolean col = true; + if (x == 0 || x == 7) { + this.setPiece(x,y,PieceType.Rook,col); + } else if (x == 1 || x == 6) {this.setPiece(x,y,PieceType.Knight,col);} + else if (x == 2 || x == 5) {this.setPiece(x,y,PieceType.Bishop,col);} + else if (x == 3) {this.setPiece(x,y,PieceType.Queen,col);} + else if (x == 4) {this.setPiece(x,y,PieceType.King,col);} + } + } + } } public void cleanBoard() { //TODO } + @Override public String toString() { - //TODO - return ""; + return "Board [width=" + width + ", height=" + height + ", board=" + board + "]"; } public ArrayList getPieces() { ArrayList pieces = new ArrayList<>(); - //TODO - - return pieces; + for (ArrayList row : board) { + for (Piece piece : row) { + if (piece != null) { + pieces.add(piece); + } + } + } + return pieces; + } + + public Piece getPiece(int x, int y) { + return board.get(x).get(y); } public void userTouch(int x, int y) { - //TODO + Piece pieceToMove = new Piece(); + while (this.selectPosition < 2) { + if (this.selectPosition == 0) { + pieceToMove = this.getPiece(y,x); + } else if (this.selectPosition == 1) { + this.setPiece(x, y, pieceToMove.getType(), pieceToMove.isWhite()); + } + } } diff --git a/OOP_2B1_Project/src/backend/Game.java b/OOP_2B1_Project/src/backend/Game.java index a2b79c9..ce1421f 100644 --- a/OOP_2B1_Project/src/backend/Game.java +++ b/OOP_2B1_Project/src/backend/Game.java @@ -66,8 +66,8 @@ public class Game extends Thread { } - public void setPiece(boolean isWhite, PieceType type, int x, int y) { - board.setPiece(isWhite, type, x, y); + public void setPiece(int x, int y, PieceType type, boolean isWhite) { + board.setPiece(x, y, type, isWhite); } public String[] getFileRepresentation() { diff --git a/OOP_2B1_Project/src/backend/Piece.java b/OOP_2B1_Project/src/backend/Piece.java index c560daf..25ceece 100644 --- a/OOP_2B1_Project/src/backend/Piece.java +++ b/OOP_2B1_Project/src/backend/Piece.java @@ -1,21 +1,36 @@ package backend; public class Piece { - + public int x; + public int y; + public PieceType type; + public boolean isWhite; + public Piece() { + } + public Piece(int x, int y, PieceType type, boolean isWhite) { + this.x = x; + this.y = y; + this.type = type; + this.isWhite = isWhite; + } public int getX() { - return 0; + return this.x; } public int getY() { - return 0; + return this.y; } public PieceType getType() { - return null; + return this.type; } public boolean isWhite() { - return false; + return this.isWhite; + } + @Override + public String toString() { + return "Piece [x=" + x + ", y=" + y + ", type=" + type + ", isWhite=" + isWhite + "]"; } } diff --git a/OOP_2B1_Project/src/windowInterface/JPanelChessBoard.java b/OOP_2B1_Project/src/windowInterface/JPanelChessBoard.java index ad260b1..84e95b2 100644 --- a/OOP_2B1_Project/src/windowInterface/JPanelChessBoard.java +++ b/OOP_2B1_Project/src/windowInterface/JPanelChessBoard.java @@ -61,7 +61,7 @@ public class JPanelChessBoard extends JPanel { int y = (me.getY()*myGame.getHeight())/getHeight(); if(pieceAdderMode) { //TODO - myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y); + myGame.setPiece(x, y, selectedPieceType, selectedPieceIsWhite); pieceAdderMode = false; } else { myGame.clickCoords(x,y);