loading and saving method done
This commit is contained in:
parent
c555ba8350
commit
6e70865ae6
|
|
@ -14,6 +14,7 @@ public class Board {
|
|||
private ArrayList<int[]> highlightedSquares= new ArrayList<>(); //stores highlighted board positions
|
||||
private Move lastMove; // Tracks the last move; needed for en-passant rule
|
||||
private List<Board> previousStates = new ArrayList<>();
|
||||
|
||||
|
||||
//constructor for the board
|
||||
public Board(int colNum, int lineNum) { //here colNum and lineNum are arguments
|
||||
|
|
@ -221,14 +222,71 @@ public class Board {
|
|||
/* saving-loading feature :*/
|
||||
|
||||
public String[] toFileRep() {
|
||||
//TODO
|
||||
return null;
|
||||
}
|
||||
String[] out = new String[height + 1];
|
||||
for (int y = 0; y < height; y++) {
|
||||
StringBuilder row = new StringBuilder();
|
||||
for (int x = 0; x < width; x++) {
|
||||
Piece p = board[x][y];
|
||||
if (p == null) {
|
||||
row.append("--");
|
||||
} else {
|
||||
row.append(p.isWhite() ? 'W' : 'B');
|
||||
switch (p.getType()) {
|
||||
case King: row.append('K'); break;
|
||||
case Queen: row.append('Q'); break;
|
||||
case Rook: row.append('R'); break;
|
||||
case Bishop: row.append('B'); break;
|
||||
case Knight: row.append('N'); break;
|
||||
case Pawn: row.append('P'); break;
|
||||
}
|
||||
}
|
||||
if (x < width - 1) row.append(',');
|
||||
}
|
||||
out[y] = row.toString();
|
||||
}
|
||||
// whose turn next
|
||||
out[height] = isTurnWhite ? "W" : "B";
|
||||
return out;
|
||||
}
|
||||
|
||||
public Board(String[] array) {
|
||||
//TODO
|
||||
public Board(String[] fileRep) {
|
||||
// derive dimensions
|
||||
this.width = fileRep[0].split(",").length;
|
||||
this.height = fileRep.length - 1;
|
||||
this.board = new Piece[width][height];
|
||||
|
||||
// parse each row
|
||||
for (int y = 0; y < height; y++) {
|
||||
String[] cells = fileRep[y].split(",");
|
||||
for (int x = 0; x < width; x++) {
|
||||
String c = cells[x];
|
||||
if (!c.equals("--")) {
|
||||
boolean white = (c.charAt(0) == 'W');
|
||||
char t = c.charAt(1);
|
||||
PieceType type;
|
||||
switch (t) {
|
||||
case 'K': type = PieceType.King; break;
|
||||
case 'Q': type = PieceType.Queen; break;
|
||||
case 'R': type = PieceType.Rook; break;
|
||||
case 'B': type = PieceType.Bishop; break;
|
||||
case 'N': type = PieceType.Knight; break;
|
||||
case 'P': type = PieceType.Pawn; break;
|
||||
default: throw new IllegalArgumentException("Bad code: "+c);
|
||||
}
|
||||
// create the piece at (x,y)
|
||||
Piece p = new Piece(x, y, type, white);
|
||||
board[x][y] = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// restore turn flag and clear any history/highlights
|
||||
this.isTurnWhite = fileRep[height].equals("W");
|
||||
this.previousStates = new ArrayList<>();
|
||||
this.highlightedSquares= new ArrayList<>();
|
||||
// any other fields you need to init…
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* The following methods require more work ! */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
BR,BN,BB,BQ,BK,BB,BN,BR
|
||||
BP,BP,--,BP,BP,BP,BP,BP
|
||||
--,--,--,--,--,--,--,--
|
||||
--,--,BP,--,--,--,--,--
|
||||
--,--,--,--,--,WP,--,--
|
||||
--,--,--,--,--,--,--,--
|
||||
WP,WP,WP,WP,WP,--,WP,WP
|
||||
WR,WN,WB,WQ,WK,WB,WN,WR
|
||||
W
|
||||
Loading…
Reference in New Issue