Part3_1
This commit is contained in:
parent
4d4fee3bac
commit
96593f2352
|
|
@ -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
|
||||
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
|
||||
|
|
|
|||
|
|
@ -4,19 +4,77 @@ import backend.Piece;
|
|||
import backend.PieceType;
|
||||
import windowInterface.MyInterface;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
// testing :
|
||||
Board testBoard = new Board(8, 8);
|
||||
testBoard.populateBoard();
|
||||
System.out.println(testBoard.toString());
|
||||
|
||||
// Test saving board to file
|
||||
testSavingBoard(testBoard, "default.board");
|
||||
|
||||
// Test loading board from file
|
||||
Board loadedBoard = testLoadingBoard("default.board");
|
||||
if (loadedBoard != null) {
|
||||
System.out.println("Loaded board successfully:");
|
||||
System.out.println(loadedBoard.toString());
|
||||
}
|
||||
|
||||
// Create a custom board position and save it
|
||||
Board customBoard = new Board(8, 8);
|
||||
// Add some pieces in custom positions
|
||||
customBoard.setPiece(true, PieceType.King, 4, 7);
|
||||
customBoard.setPiece(false, PieceType.King, 4, 0);
|
||||
customBoard.setPiece(true, PieceType.Queen, 3, 7);
|
||||
customBoard.setPiece(false, PieceType.Queen, 3, 0);
|
||||
customBoard.setPiece(true, PieceType.Rook, 0, 7);
|
||||
customBoard.setPiece(true, PieceType.Rook, 7, 7);
|
||||
customBoard.setPiece(false, PieceType.Rook, 0, 0);
|
||||
customBoard.setPiece(false, PieceType.Rook, 7, 0);
|
||||
// Save the custom board
|
||||
testSavingBoard(customBoard, "custom.board");
|
||||
|
||||
// launches graphical interface :
|
||||
MyInterface mjf = new MyInterface();
|
||||
mjf.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void testSavingBoard(Board board, String filename) {
|
||||
String[] boardRep = board.toFileRep();
|
||||
try {
|
||||
PrintWriter writer = new PrintWriter(filename);
|
||||
for (String line : boardRep) {
|
||||
writer.println(line);
|
||||
}
|
||||
writer.close();
|
||||
System.out.println("Board saved successfully to " + filename);
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("Error saving the board: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static Board testLoadingBoard(String filename) {
|
||||
try {
|
||||
ArrayList<String> lines = new ArrayList<>();
|
||||
Scanner scanner = new Scanner(new File(filename));
|
||||
while (scanner.hasNextLine()) {
|
||||
lines.add(scanner.nextLine());
|
||||
}
|
||||
scanner.close();
|
||||
|
||||
String[] boardRep = lines.toArray(new String[0]);
|
||||
Board loadedBoard = new Board(boardRep);
|
||||
return loadedBoard;
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("Error loading the board from " + filename + ": " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -213,13 +213,79 @@ public class Board {
|
|||
/* saving-loading feature :*/
|
||||
|
||||
public String[] toFileRep() {
|
||||
//TODO
|
||||
return null;
|
||||
String[] fileRep = new String[height + 1];
|
||||
for (int y = 0; y < height; y++) {
|
||||
StringBuilder row = new StringBuilder();
|
||||
for (int x = 0; x < width; x++) {
|
||||
Piece piece = getPieceAt(x, y);
|
||||
if (piece == null) {
|
||||
row.append("--");
|
||||
} else {
|
||||
char colorChar = piece.isWhite() ? 'W' : 'B';
|
||||
char typeChar;
|
||||
switch (piece.getType()) {
|
||||
case Pawn: typeChar = 'P'; break;
|
||||
case Rook: typeChar = 'R'; break;
|
||||
case Knight: typeChar = 'N'; break;
|
||||
case Bishop: typeChar = 'B'; break;
|
||||
case Queen: typeChar = 'Q'; break;
|
||||
case King: typeChar = 'K'; break;
|
||||
default: typeChar = '?';
|
||||
}
|
||||
row.append(colorChar).append(typeChar);
|
||||
}
|
||||
if (x < width - 1) {
|
||||
row.append(",");
|
||||
}
|
||||
}
|
||||
fileRep[y] = row.toString();
|
||||
}
|
||||
fileRep[height] = isTurnWhite ? "W" : "B";
|
||||
return fileRep;
|
||||
}
|
||||
|
||||
public Board(String[] array) {
|
||||
//TODO
|
||||
|
||||
|
||||
public Board(String[] fileRep) {
|
||||
this.width = 8;
|
||||
this.height = 8;
|
||||
if (fileRep != null && fileRep.length > 0) {
|
||||
this.height = fileRep.length - 1;
|
||||
for (int y = 0; y < height; y++) {
|
||||
if (y < fileRep.length) {
|
||||
String[] squares = fileRep[y].split(",");
|
||||
if (y == 0) {
|
||||
this.width = squares.length;
|
||||
}
|
||||
for (int x = 0; x < width && x < squares.length; x++) {
|
||||
String squareInfo = squares[x];
|
||||
if (squareInfo.length() < 2 || squareInfo.equals("--")) {
|
||||
continue;
|
||||
}
|
||||
boolean isWhite = squareInfo.charAt(0) == 'W';
|
||||
PieceType type = null;
|
||||
switch (squareInfo.charAt(1)) {
|
||||
case 'P': type = PieceType.Pawn; break;
|
||||
case 'R': type = PieceType.Rook; break;
|
||||
case 'N': type = PieceType.Knight; break;
|
||||
case 'B': type = PieceType.Bishop; break;
|
||||
case 'Q': type = PieceType.Queen; break;
|
||||
case 'K': type = PieceType.King; break;
|
||||
}
|
||||
if (type != null) {
|
||||
setPiece(isWhite, type, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fileRep.length > height && fileRep[height] != null && fileRep[height].length() > 0) {
|
||||
this.isTurnWhite = fileRep[height].charAt(0) == 'W';
|
||||
} else {
|
||||
this.isTurnWhite = true; // Default to white's turn
|
||||
}
|
||||
}
|
||||
this.turnNumber = 0;
|
||||
this.selectedX = -1;
|
||||
this.selectedY = -1;
|
||||
this.highlightedPositions = new ArrayList<>();
|
||||
}
|
||||
|
||||
/* The following methods require more work ! */
|
||||
|
|
|
|||
Loading…
Reference in New Issue