board to file implemented, board from file does not work
This commit is contained in:
parent
48631a1d96
commit
71ee1b1eb3
|
|
@ -4,8 +4,8 @@ import java.util.ArrayList;
|
|||
import java.util.LinkedList;
|
||||
|
||||
public class Board {
|
||||
private int width;
|
||||
private int height;
|
||||
private int width = 8;
|
||||
private int height = 8;
|
||||
private ArrayList<ArrayList<Piece>> board = new ArrayList<>();
|
||||
private boolean select = false;
|
||||
private int xm;
|
||||
|
|
@ -191,13 +191,15 @@ public class Board {
|
|||
/* saving-loading feature :*/
|
||||
|
||||
public String[] toFileRep() {
|
||||
//TODO
|
||||
return null;
|
||||
FileBoard boardToFile = new FileBoard(board,turnNumber,turnColor);
|
||||
return boardToFile.toFile();
|
||||
}
|
||||
|
||||
public Board(String[] array) {
|
||||
//TODO
|
||||
|
||||
FileBoard boardToFile = new FileBoard(array);
|
||||
this.board = boardToFile.getBoard();
|
||||
this.turnColor = boardToFile.isTurnColor();
|
||||
this.turnNumber = boardToFile.getTurnNumber();
|
||||
}
|
||||
|
||||
/* The following methods require more work ! */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,100 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class FileBoard {
|
||||
private ArrayList<ArrayList<Piece>> board = new ArrayList<>();
|
||||
private int turnNumber;
|
||||
private boolean turnColor;
|
||||
public FileBoard(ArrayList<ArrayList<Piece>> board,int turnNumber, boolean turnColor) {
|
||||
this.board = board;
|
||||
this.turnColor = turnColor;
|
||||
this.turnNumber = turnNumber;
|
||||
}
|
||||
public String[] toFile() {
|
||||
String tC;
|
||||
String toBeWritten = " ";
|
||||
String activeLine = "";
|
||||
String[] boardFile = new String[8];
|
||||
for (int y = 0;y<8;y++) {
|
||||
for (int x = 0;x<8;x++) {
|
||||
if (x!= 7) {
|
||||
if (board.get(y).get(x) != null) {
|
||||
if (board.get(y).get(x).isWhite() == true) {
|
||||
toBeWritten = 'W' + board.get(y).get(x).getType().getSummary() + ",";
|
||||
} else {
|
||||
toBeWritten = 'B' + board.get(y).get(x).getType().getSummary() + ",";
|
||||
}
|
||||
} else {toBeWritten = " ,";}
|
||||
activeLine += toBeWritten;
|
||||
} else {
|
||||
if (board.get(y).get(x) != null) {
|
||||
if (board.get(y).get(x).isWhite() == true) {
|
||||
toBeWritten = 'W' + board.get(y).get(x).getType().getSummary();
|
||||
} else {
|
||||
toBeWritten = 'B' + board.get(y).get(x).getType().getSummary();
|
||||
}
|
||||
} else {toBeWritten = " ";}
|
||||
activeLine += toBeWritten;
|
||||
}
|
||||
}
|
||||
boardFile[y] = activeLine;
|
||||
activeLine = "";
|
||||
}
|
||||
if (turnColor) {
|
||||
tC = "W";
|
||||
} else {tC = "B";}
|
||||
boardFile[8] = turnNumber + tC;
|
||||
return boardFile;
|
||||
}
|
||||
public FileBoard(String[] boardFile) {
|
||||
ArrayList<ArrayList<Piece>> boardF = new ArrayList<>();
|
||||
int rows = 8;
|
||||
int cols = 8;
|
||||
for (int i = 0; i < rows; i++) {
|
||||
ArrayList<Piece> row = new ArrayList<>();
|
||||
for (int j = 0; j < cols; j++) {
|
||||
row.add(null); // Fill with null
|
||||
}
|
||||
boardF.add(row);
|
||||
}
|
||||
String pieceChar;
|
||||
char color;
|
||||
char type;
|
||||
boolean col;
|
||||
for (int x = 0; x<8;x++) {
|
||||
for (int y = 0; y<8;y++) {
|
||||
pieceChar = getCharAt(boardFile,x,y);
|
||||
if (pieceChar.charAt(0) != ' ') {
|
||||
// System.out.println(pieceChar); // Debug
|
||||
color = pieceChar.charAt(0);
|
||||
type = pieceChar.charAt(1);
|
||||
if (color == 'W') {col = true;} else {col = false;}
|
||||
Piece piece = PieceCreation.createPiece(x,y,PieceType.fromSummary(type),col);
|
||||
boardF.get(y).set(x, piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
pieceChar = getCharAt(boardFile,0,7);
|
||||
type = pieceChar.charAt(0);
|
||||
color = pieceChar.charAt(1);
|
||||
if (color == 'B') {col = false;} else {col = true;}
|
||||
this.board = boardF;
|
||||
this.turnColor = col;
|
||||
this.turnNumber = type;
|
||||
|
||||
}
|
||||
public String getCharAt(String[] str,int x,int y) {
|
||||
return str[y].split(",")[x];
|
||||
}
|
||||
public ArrayList<ArrayList<Piece>> getBoard() {
|
||||
return board;
|
||||
}
|
||||
public int getTurnNumber() {
|
||||
return turnNumber;
|
||||
}
|
||||
public boolean isTurnColor() {
|
||||
return turnColor;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue