Optimised loading function (moved everything in the same constructor)
This commit is contained in:
parent
2417b7b2ba
commit
ca4c22f545
|
|
@ -222,29 +222,39 @@ public class Board {
|
|||
return lines.toArray(new String[0]);
|
||||
}
|
||||
|
||||
public void loadFromFileRep(String[] lines) {
|
||||
cleanBoard();
|
||||
for (String line : lines) {
|
||||
String[] tokens = line.trim().split("\\s+");
|
||||
if (tokens.length != 4) continue;
|
||||
|
||||
PieceType type = PieceType.valueOf(tokens[0]);
|
||||
boolean isWhite = Boolean.parseBoolean(tokens[1]);
|
||||
int x = Integer.parseInt(tokens[2]);
|
||||
int y = Integer.parseInt(tokens[3]);
|
||||
|
||||
setPiece(type, isWhite, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
//loading feature
|
||||
public Board(String[] array) {
|
||||
this.cNum = 8;
|
||||
this.lNum = 8;
|
||||
this.cells = new Piece[cNum][lNum];
|
||||
this.selectedCell = null;
|
||||
loadFromFileRep(array);
|
||||
|
||||
//clean board
|
||||
for (int y = 0; y < 8; y++) {
|
||||
for (int x = 0; x < 8; x++) {
|
||||
cells[x][y] = null;
|
||||
}
|
||||
}
|
||||
|
||||
//load pieces
|
||||
for (String line : array) {
|
||||
String[] tokens = line.trim().split("\\s+");
|
||||
if (tokens.length != 4) continue;
|
||||
|
||||
try {
|
||||
PieceType type = PieceType.valueOf(tokens[0]);
|
||||
boolean isWhite = Boolean.parseBoolean(tokens[1]);
|
||||
int x = Integer.parseInt(tokens[2]);
|
||||
int y = Integer.parseInt(tokens[3]);
|
||||
|
||||
setPiece(type, isWhite, x, y);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error parsing line: " + line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* The following methods require more work ! */
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue