board initialisation (and test)
This commit is contained in:
parent
dfdf692e0b
commit
df2bf3df9f
|
|
@ -10,17 +10,18 @@ public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// testing :
|
// testing :
|
||||||
Board testBoard = new Board(9, 8);
|
Board testBoard = new Board(8, 8);
|
||||||
testBoard.populateBoard();
|
testBoard.populateBoard();
|
||||||
testBoard.getHeight();
|
testBoard.getHeight();
|
||||||
testBoard.getWidth();
|
testBoard.getWidth();
|
||||||
System.out.println(testBoard.toString());
|
System.out.println(testBoard.toString());
|
||||||
|
testBoard.populateBoard();
|
||||||
|
|
||||||
Piece testPiece = new Piece();
|
/**Piece testPiece = new Piece(true, PieceType.Rook, 2,3);
|
||||||
testPiece.getX();
|
testPiece.getX();
|
||||||
testPiece.getY();
|
testPiece.getY();
|
||||||
testPiece.getType();
|
testPiece.getType();
|
||||||
testPiece.isWhite();
|
testPiece.isWhite();*/
|
||||||
|
|
||||||
|
|
||||||
// launches graphical interface :
|
// launches graphical interface :
|
||||||
|
|
|
||||||
|
|
@ -8,16 +8,23 @@ public class Board {
|
||||||
|
|
||||||
private int colNum;
|
private int colNum;
|
||||||
private int lineNum;
|
private int lineNum;
|
||||||
|
public String board[][];
|
||||||
|
private PieceType type;
|
||||||
|
private ArrayList<Piece> pieces;
|
||||||
|
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
this.colNum = colNum;
|
this.colNum = colNum;
|
||||||
this.lineNum = lineNum;
|
this.lineNum = lineNum;
|
||||||
String board[][] = new String[lineNum][colNum];
|
this.pieces = new ArrayList<>();
|
||||||
for (int i = 0; i < lineNum; i++) {
|
|
||||||
for (int j = 0; j < colNum; j++) {
|
//board = new String[lineNum][colNum];
|
||||||
board[i][j] = null;
|
// for (int i = 0; i < lineNum; i++) {
|
||||||
}
|
// for (int j = 0; j < colNum; j++) {
|
||||||
}
|
// board[i][j] = null;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
|
|
@ -41,26 +48,101 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
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
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
;
|
||||||
|
|
||||||
public void populateBoard() {
|
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() {
|
public void cleanBoard() {
|
||||||
|
pieces.clear(); // Remove all pieces from the board
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
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
|
//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<Piece> getPieces() {
|
public ArrayList<Piece> getPieces() {
|
||||||
ArrayList<Piece> pieces = new ArrayList<>();
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
return pieces;
|
return pieces;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,19 @@ public class Piece {
|
||||||
private int y;
|
private int y;
|
||||||
private PieceType type;
|
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() {
|
public int getX() {
|
||||||
System.out.println("The X value is " + x);
|
System.out.println("The X value is " + x);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue