add pieces into board
This commit is contained in:
parent
7bc1707ad6
commit
fe0bcf52c6
|
|
@ -5,10 +5,18 @@ import java.util.ArrayList;
|
|||
public class Board {
|
||||
private int width;
|
||||
private int height;
|
||||
private ArrayList<Piece> pieces;
|
||||
private int turnNumber = 0;
|
||||
private boolean isWhite = true;
|
||||
private int[] selected = null; // for userTouch
|
||||
private ArrayList<int[]> highlighted = new ArrayList<>();
|
||||
private ArrayList<Move> moveHistory = new ArrayList<>();
|
||||
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.width = colNum;
|
||||
this.height = lineNum;
|
||||
this.pieces = new ArrayList<>();
|
||||
//TODO
|
||||
}
|
||||
|
||||
|
|
@ -24,33 +32,89 @@ public class Board {
|
|||
|
||||
public int getTurnNumber() {
|
||||
//TODO
|
||||
return 0;
|
||||
return turnNumber;
|
||||
}
|
||||
|
||||
public boolean isTurnWhite() {
|
||||
//TODO
|
||||
return false;
|
||||
return isWhite;
|
||||
}
|
||||
|
||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||
Piece piece = new Piece(isWhite, type, x, y);
|
||||
pieces.add(piece);
|
||||
//TODO
|
||||
}
|
||||
|
||||
public void populateBoard() {
|
||||
cleanBoard(); // clear any existing pieces
|
||||
|
||||
// White pieces
|
||||
for (int i = 0; i < 8; i++) {
|
||||
setPiece(true, PieceType.Pawn, i, 1);
|
||||
}
|
||||
setPiece(true, PieceType.Rook, 0, 0);
|
||||
setPiece(true, PieceType.Knight, 1, 0);
|
||||
setPiece(true, PieceType.Bishop, 2, 0);
|
||||
setPiece(true, PieceType.Queen, 3, 0);
|
||||
setPiece(true, PieceType.King, 4, 0);
|
||||
setPiece(true, PieceType.Bishop, 5, 0);
|
||||
setPiece(true, PieceType.Knight, 6, 0);
|
||||
setPiece(true, PieceType.Rook, 7, 0);
|
||||
|
||||
// Black pieces
|
||||
for (int i = 0; i < 8; i++) {
|
||||
setPiece(false, PieceType.Pawn, i, 6);
|
||||
}
|
||||
setPiece(false, PieceType.Rook, 0, 7);
|
||||
setPiece(false, PieceType.Knight, 1, 7);
|
||||
setPiece(false, PieceType.Bishop, 2, 7);
|
||||
setPiece(false, PieceType.Queen, 3, 7);
|
||||
setPiece(false, PieceType.King, 4, 7);
|
||||
setPiece(false, PieceType.Bishop, 5, 7);
|
||||
setPiece(false, PieceType.Knight, 6, 7);
|
||||
setPiece(false, PieceType.Rook, 7, 7);
|
||||
//TODO
|
||||
}
|
||||
|
||||
public void cleanBoard() {
|
||||
pieces.clear();
|
||||
//TODO
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String[][] grid = new String[height][width];
|
||||
|
||||
// Fill grid with dots (empty)
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
grid[y][x] = ".";
|
||||
}
|
||||
}
|
||||
|
||||
// Place each piece in its position
|
||||
for (Piece p : pieces) {
|
||||
String symbol = p.getType().toString().substring(0, 1); // e.g., "P" for Pawn
|
||||
if (!p.isWhite()) {
|
||||
symbol = symbol.toLowerCase(); // lowercase for black
|
||||
}
|
||||
grid[p.getY()][p.getX()] = symbol;
|
||||
}
|
||||
|
||||
// Build the string representation
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
sb.append(grid[y][x]).append(" ");
|
||||
}
|
||||
sb.append("\n");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
//TODO
|
||||
return "";
|
||||
}
|
||||
|
||||
public ArrayList<Piece> getPieces() {
|
||||
ArrayList<Piece> pieces = new ArrayList<>();
|
||||
//TODO
|
||||
|
||||
return pieces;
|
||||
|
|
@ -63,7 +127,7 @@ public class Board {
|
|||
|
||||
public boolean isSelected(int x, int y) {
|
||||
//TODO
|
||||
return false;
|
||||
return selected != null && selected[0] == x && selected[1] == y;
|
||||
}
|
||||
|
||||
/* saving-loading feature :*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue