save and load part
This commit is contained in:
parent
f2fdeac92e
commit
af6752b197
|
|
@ -116,6 +116,8 @@ public class Board {
|
||||||
|
|
||||||
return pieces;
|
return pieces;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void userTouch(int x, int y) {
|
public void userTouch(int x, int y) {
|
||||||
if (x < 0 || x >= width || y < 0 || y >= height) return;
|
if (x < 0 || x >= width || y < 0 || y >= height) return;
|
||||||
|
|
@ -164,14 +166,58 @@ public class Board {
|
||||||
/* saving-loading feature :*/
|
/* saving-loading feature :*/
|
||||||
|
|
||||||
public String[] toFileRep() {
|
public String[] toFileRep() {
|
||||||
//TODO
|
String[] lines = new String[height + 1]; // one line per row + 1 for turn
|
||||||
return null;
|
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
Piece piece = board[x][y];
|
||||||
|
if (piece == null) {
|
||||||
|
sb.append("--");
|
||||||
|
} else {
|
||||||
|
sb.append(piece.isWhite() ? "W" : "B");
|
||||||
|
sb.append(piece.getType().getSummary());
|
||||||
|
}
|
||||||
|
if (x < width - 1) sb.append(",");
|
||||||
|
}
|
||||||
|
lines[y] = sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Last line stores the turn number
|
||||||
|
lines[height] = Integer.toString(turn);
|
||||||
|
|
||||||
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Board(String[] array) {
|
//public Board(String[] array) {
|
||||||
//TODO
|
//TODO
|
||||||
|
|
||||||
|
//}
|
||||||
|
|
||||||
|
public Board(String[] fileRepresentation) {
|
||||||
|
this.height = fileRepresentation.length - 1;
|
||||||
|
this.width = fileRepresentation[0].split(",").length;
|
||||||
|
this.board = new Piece[width][height];
|
||||||
|
this.highlightedSquares = new boolean[width][height];
|
||||||
|
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
String[] tokens = fileRepresentation[y].split(",");
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
String token = tokens[x];
|
||||||
|
if (!token.equals("--")) {
|
||||||
|
boolean isWhite = token.charAt(0) == 'W';
|
||||||
|
char pieceChar = token.charAt(1);
|
||||||
|
PieceType type = PieceType.fromSummary(pieceChar);
|
||||||
|
board[x][y] = new Piece(isWhite, type, x, y);
|
||||||
|
} else {
|
||||||
|
board[x][y] = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.turn = Integer.parseInt(fileRepresentation[height]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void calculateValidMoves(Piece piece) {
|
private void calculateValidMoves(Piece piece) {
|
||||||
clearHighlights();
|
clearHighlights();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue