parent
3e1a770176
commit
a19130bb86
|
@ -1,4 +1,8 @@
|
|||
package de.hs_mannheim.informatik.chess.model;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.ConsoleHandler;
|
||||
|
@ -10,7 +14,10 @@ import java.util.logging.SimpleFormatter;
|
|||
import com.github.bhlangonijr.chesslib.Board;
|
||||
import com.github.bhlangonijr.chesslib.Piece;
|
||||
import com.github.bhlangonijr.chesslib.Square;
|
||||
import com.github.bhlangonijr.chesslib.game.Game;
|
||||
import com.github.bhlangonijr.chesslib.game.GameResult;
|
||||
import com.github.bhlangonijr.chesslib.move.Move;
|
||||
import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
|
||||
|
||||
public class ChessEngine {
|
||||
private Board board;
|
||||
|
@ -187,4 +194,56 @@ public class ChessEngine {
|
|||
logger.info("ChessEngine wurde initialisiert.");
|
||||
}
|
||||
|
||||
public List<Game> loadGamesFromPgn(String path) throws IOException {
|
||||
|
||||
PgnHolder pgnHolder = new PgnHolder(path);
|
||||
try {
|
||||
pgnHolder.loadPgn();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
List<Game> games = pgnHolder.getGames();
|
||||
return games;
|
||||
}
|
||||
|
||||
public void saveAsPgn(Game game, String path, String dateiname) {
|
||||
String event = game.getRound().getEvent().getName();
|
||||
String site = game.getRound().getEvent().getSite();
|
||||
String round = "" + game.getRound().getNumber();
|
||||
String date = game.getRound().getEvent().getStartDate();
|
||||
String wName = game.getWhitePlayer().getName();
|
||||
String bName = game.getBlackPlayer().getName();
|
||||
String result = game.getResult().getDescription();
|
||||
|
||||
StringBuilder header = new StringBuilder();
|
||||
header.append("[Event \"" + event + "\"]\n");
|
||||
header.append("[Site \"" + site + "\"]\n");
|
||||
header.append("[Date \"" + date + "\"]\n");
|
||||
header.append("[Round \"" + round + "\"]\n");
|
||||
header.append("[White \"" + wName + "\"]\n");
|
||||
header.append("[Black \"" + bName + "\"]\n");
|
||||
header.append("[Result \"" + result + "\"]\n");
|
||||
header.append("\n");
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String[] sanArray = game.getHalfMoves().toSanArray();
|
||||
|
||||
for (int i = 0; i < sanArray.length; i++) {
|
||||
if (i % 2 == 0) {
|
||||
sb.append((i / 2 + 1)).append(". ");
|
||||
}
|
||||
sb.append(sanArray[i]).append(" ");
|
||||
}
|
||||
|
||||
sb.append(result); // Endergebnis muss auch am Ende stehen!
|
||||
|
||||
String file = header.toString() + sb.toString();
|
||||
|
||||
try {
|
||||
Files.writeString(Path.of(path, dateiname), file, StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue