Changes until setPiece()

This commit is contained in:
Marleen PETERSON 2025-04-15 11:17:22 +02:00
parent 10cd42b2c9
commit 690ee4f1dc
2 changed files with 76 additions and 21 deletions

View File

@ -1,10 +1,6 @@
import backend.Board;
import backend.Move;
import backend.Piece;
import backend.PieceType;
import windowInterface.MyInterface;
public class Main {
@ -13,6 +9,7 @@ public class Main {
Board testBoard = new Board(8, 8);
testBoard.populateBoard();
System.out.println(testBoard.toString());
System.out.println(testBoard.getWidth() + "*" + testBoard.getHeight());
// launches graphical interface :
MyInterface mjf = new MyInterface();

View File

@ -3,52 +3,110 @@ package backend;
import java.util.ArrayList;
public class Board {
private int colNum;
private int lineNum;
private Piece[][] board;
private int turnNumber = 0;
private boolean isWhiteTurn = true;
public Board(int colNum, int lineNum) {
this.colNum = colNum;
this.lineNum = lineNum;
this.board = new Piece[lineNum][colNum];
}
public int getWidth() {
//TODO
return 0;
return colNum;
}
public int getHeight() {
//TODO
return 0;
return lineNum;
}
public int getTurnNumber() {
//TODO
return 0;
return turnNumber;
}
public boolean isTurnWhite() {
//TODO
return false;
return isWhiteTurn;
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
//TODO
board[y][x] = new Piece(x,y,type, isWhite);
}
public void populateBoard() {
//TODO
}
cleanBoard(); // Start with an empty board
// Pawns
for (int x = 0; x < getWidth(); x++) {
setPiece(false, PieceType.Pawn, x, 1); // Black pawns
setPiece(true, PieceType.Pawn, x, 6); // White pawns
}
// Rooks
setPiece(false, PieceType.Rook, 0, 0);
setPiece(false, PieceType.Rook, 7, 0);
setPiece(true, PieceType.Rook, 0, 7);
setPiece(true, PieceType.Rook, 7, 7);
// Knights
setPiece(false, PieceType.Knight, 1, 0);
setPiece(false, PieceType.Knight, 6, 0);
setPiece(true, PieceType.Knight, 1, 7);
setPiece(true, PieceType.Knight, 6, 7);
// Bishops
setPiece(false, PieceType.Bishop, 2, 0);
setPiece(false, PieceType.Bishop, 5, 0);
setPiece(true, PieceType.Bishop, 2, 7);
setPiece(true, PieceType.Bishop, 5, 7);
// Queens
setPiece(false, PieceType.Queen, 3, 0);
setPiece(true, PieceType.Queen, 3, 7);
// Kings
setPiece(false, PieceType.King, 4, 0);
setPiece(true, PieceType.King, 4, 7);
}
public void cleanBoard() {
//TODO
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
board[y][x] = null;
}
}
}
@Override
public String toString() {
//TODO
return "";
StringBuilder sb = new StringBuilder();
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
Piece piece = board[y][x];
if (piece == null) {
sb.append(". ");
} else {
String symbol = piece.getType().getSummary();
sb.append(piece.isWhite() ? symbol.toUpperCase() : symbol.toLowerCase()).append(" ");
}
}
sb.append("\n");
}
return sb.toString();
}
public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>();
//TODO
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
if (board[y][x] != null) {
pieces.add(board[y][x]);
}
}
}
return pieces;
}