Loading/Saving system (Board and Main classes touched)
This commit is contained in:
parent
c03ff7d03e
commit
612ee4ab97
|
|
@ -1,9 +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
|
||||
|
|
|
|||
|
|
@ -3,30 +3,42 @@ import backend.Move;
|
|||
import backend.Piece;
|
||||
import backend.PieceType;
|
||||
import windowInterface.MyInterface;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Testing
|
||||
Board testBoard = new Board(8, 8);
|
||||
testBoard.populateBoard();
|
||||
System.out.println("Initial board:");
|
||||
System.out.println(testBoard.toString());
|
||||
|
||||
public static void main(String[] args) {
|
||||
// testing :
|
||||
Board testBoard = new Board(8, 8);
|
||||
testBoard.populateBoard();
|
||||
testBoard.getHeight();
|
||||
testBoard.getWidth();
|
||||
System.out.println(testBoard.toString());
|
||||
testBoard.populateBoard();
|
||||
|
||||
/**Piece testPiece = new Piece(true, PieceType.Rook, 2,3);
|
||||
testPiece.getX();
|
||||
testPiece.getY();
|
||||
testPiece.getType();
|
||||
testPiece.isWhite();*/
|
||||
|
||||
saveBoard(testBoard, "save.board");
|
||||
Board loadedBoard = loadBoard("save.board");
|
||||
System.out.println("\nLoaded board:\n" + loadedBoard);
|
||||
|
||||
// launches graphical interface :
|
||||
MyInterface mjf = new MyInterface();
|
||||
mjf.setVisible(true);
|
||||
}
|
||||
MyInterface mjf = new MyInterface();
|
||||
mjf.setVisible(true);
|
||||
}
|
||||
|
||||
private static void saveBoard(Board board, String filename) {
|
||||
try (FileWriter fw = new FileWriter(filename)) {
|
||||
for (String line : board.toFileRep()) {
|
||||
fw.write(line + "\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Board loadBoard(String filename) {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
|
||||
return new Board(br.lines().toArray(String[]::new));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new Board(8, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -10,10 +10,10 @@ public class Board {
|
|||
private PieceType type;
|
||||
private ArrayList<Piece> pieces;
|
||||
private ArrayList<Position> highlight = new ArrayList<>();
|
||||
private int selectedX = -1; // Tracks selected column (-1 = no selection)
|
||||
private int selectedY = -1; // Tracks selected row (-1 = no selection)
|
||||
private int turnNumber = 0; // Starts at 0 (White's turn)
|
||||
private boolean isWhiteTurn = true; // Track current player's turn
|
||||
private int selectedX = -1;
|
||||
private int selectedY = -1;
|
||||
private int turnNumber = 0;
|
||||
private boolean isWhiteTurn = true;
|
||||
|
||||
private char getPieceSymbol(Piece p) {
|
||||
char symbol;
|
||||
|
|
@ -24,7 +24,7 @@ public class Board {
|
|||
case Bishop: symbol = 'B'; break;
|
||||
case Queen: symbol = 'Q'; break;
|
||||
case King: symbol = 'K'; break;
|
||||
default: symbol = '?'; // Should never happen
|
||||
default: symbol = '?';
|
||||
}
|
||||
return p.isWhite() ? symbol : Character.toLowerCase(symbol);
|
||||
}
|
||||
|
|
@ -48,8 +48,8 @@ public class Board {
|
|||
this.colNum = colNum;
|
||||
this.lineNum = lineNum;
|
||||
this.pieces = new ArrayList<>();
|
||||
this.turnNumber = 0; // Initialize turn number
|
||||
this.isWhiteTurn = true; // White starts first
|
||||
this.turnNumber = 0;
|
||||
this.isWhiteTurn = true;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
|
|
@ -101,7 +101,7 @@ public class Board {
|
|||
}
|
||||
|
||||
public void cleanBoard() {
|
||||
pieces.clear(); // Remove all pieces from the board
|
||||
pieces.clear();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
|
@ -180,12 +180,63 @@ public class Board {
|
|||
}
|
||||
|
||||
public String[] toFileRep() {
|
||||
//TODO
|
||||
return null;
|
||||
String[] fileRep = new String[lineNum + 1];
|
||||
|
||||
for (int y = 0; y < lineNum; y++) {
|
||||
StringBuilder rowBuilder = new StringBuilder();
|
||||
for (int x = 0; x < colNum; x++) {
|
||||
Piece p = getPieceAt(x, y);
|
||||
if (p != null) {
|
||||
char colorChar = p.isWhite() ? 'W' : 'B';
|
||||
char typeChar = p.getType().getSummary().charAt(0);
|
||||
rowBuilder.append(colorChar).append(typeChar);
|
||||
}
|
||||
if (x < colNum - 1) rowBuilder.append(",");
|
||||
}
|
||||
fileRep[y] = rowBuilder.toString();
|
||||
}
|
||||
|
||||
fileRep[lineNum] = isWhiteTurn ? "W" : "B";
|
||||
return fileRep;
|
||||
}
|
||||
|
||||
public Board(String[] array) {
|
||||
//TODO
|
||||
public Board(String[] fileRepresentation) {
|
||||
if (fileRepresentation == null || fileRepresentation.length < 1) {
|
||||
throw new IllegalArgumentException("Invalid file format");
|
||||
}
|
||||
|
||||
int totalRows = fileRepresentation.length - 1;
|
||||
this.lineNum = totalRows;
|
||||
String[] firstRow = fileRepresentation[0].split(",", -1);
|
||||
this.colNum = firstRow.length;
|
||||
this.pieces = new ArrayList<>();
|
||||
|
||||
for (int y = 0; y < lineNum; y++) {
|
||||
String[] squares = fileRepresentation[y].split(",", -1);
|
||||
if (squares.length != colNum) {
|
||||
throw new IllegalArgumentException("Row " + y + " has invalid column count");
|
||||
}
|
||||
|
||||
for (int x = 0; x < colNum; x++) {
|
||||
String square = squares[x].trim();
|
||||
if (!square.isEmpty()) {
|
||||
if (square.length() != 2) {
|
||||
throw new IllegalArgumentException("Invalid piece format: " + square);
|
||||
}
|
||||
boolean isWhite = square.charAt(0) == 'W';
|
||||
char typeChar = square.charAt(1);
|
||||
PieceType type = PieceType.fromSummary(typeChar);
|
||||
pieces.add(new Piece(isWhite, type, x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String turnLine = fileRepresentation[fileRepresentation.length - 1].trim();
|
||||
if (turnLine.isEmpty()) {
|
||||
throw new IllegalArgumentException("Turn line is missing");
|
||||
}
|
||||
this.isWhiteTurn = turnLine.charAt(0) == 'W';
|
||||
this.turnNumber = 0;
|
||||
}
|
||||
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue