This commit is contained in:
@@ -72,9 +72,7 @@ public abstract class AI extends GameAdaptator{
|
|||||||
|
|
||||||
protected CommandResult sendCommand(Command command, CommandExecutor commandExecutor) {
|
protected CommandResult sendCommand(Command command, CommandExecutor commandExecutor) {
|
||||||
CommandResult result = commandExecutor.executeCommand(command);
|
CommandResult result = commandExecutor.executeCommand(command);
|
||||||
if(result == CommandResult.NotAllowed){
|
assert result != CommandResult.NotAllowed : "Command not allowed!";
|
||||||
System.out.println("eeeeee");
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,4 @@ public abstract class Piece {
|
|||||||
|
|
||||||
public abstract <T> T accept(PieceVisitor<T> visitor);
|
public abstract <T> T accept(PieceVisitor<T> visitor);
|
||||||
|
|
||||||
@Override
|
|
||||||
public abstract boolean equals(Object other);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,13 +15,4 @@ public class Bishop extends Piece {
|
|||||||
return visitor.visitPiece(this);
|
return visitor.visitPiece(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
return (obj instanceof Bishop && ((Bishop) obj).getColor() == this.getColor());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,13 +14,4 @@ public class King extends Piece {
|
|||||||
public <T> T accept(PieceVisitor<T> visitor) {
|
public <T> T accept(PieceVisitor<T> visitor) {
|
||||||
return visitor.visitPiece(this);
|
return visitor.visitPiece(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
return (obj instanceof King && ((King) obj).getColor() == this.getColor());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,12 +15,4 @@ public class Knight extends Piece {
|
|||||||
return visitor.visitPiece(this);
|
return visitor.visitPiece(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
return (obj instanceof Knight && ((Knight) obj).getColor() == this.getColor());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,12 +19,4 @@ public class Pawn extends Piece {
|
|||||||
return getColor() == Color.White ? 1 : -1;
|
return getColor() == Color.White ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
return (obj instanceof Pawn && ((Pawn) obj).getColor() == this.getColor());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,12 +15,5 @@ public class Queen extends Piece {
|
|||||||
return visitor.visitPiece(this);
|
return visitor.visitPiece(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
return (obj instanceof Queen && ((Queen) obj).getColor() == this.getColor());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,12 +15,4 @@ public class Rook extends Piece {
|
|||||||
return visitor.visitPiece(this);
|
return visitor.visitPiece(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object other) {
|
|
||||||
return (other instanceof Rook && ((Rook) other).getColor() == this.getColor());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
package chess.model.visitor;
|
|
||||||
|
|
||||||
import chess.model.Color;
|
|
||||||
import chess.model.Piece;
|
|
||||||
import chess.model.PieceVisitor;
|
|
||||||
import chess.model.pieces.*;
|
|
||||||
|
|
||||||
public class KingIdentifier implements PieceVisitor<Boolean> {
|
|
||||||
|
|
||||||
private final Color color;
|
|
||||||
|
|
||||||
public KingIdentifier(Color color) {
|
|
||||||
this.color = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isKing(Piece piece) {
|
|
||||||
if (piece == null)
|
|
||||||
return false;
|
|
||||||
return visit(piece);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitPiece(Bishop bishop) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitPiece(King king) {
|
|
||||||
return king.getColor() == color;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitPiece(Knight knight) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitPiece(Pawn pawn) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitPiece(Queen queen) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitPiece(Rook rook) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,22 +1,12 @@
|
|||||||
package chess.pgn;
|
package chess.pgn;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
|
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.view.AssetManager;
|
import common.AssetManager;
|
||||||
|
|
||||||
public class PgnFileSimulator extends PgnSimulator{
|
public class PgnFileSimulator extends PgnSimulator{
|
||||||
|
|
||||||
private static String readResource(String path) {
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(AssetManager.getResource(path)));
|
|
||||||
reader.lines().forEach((line) -> builder.append(line + "\n"));
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public PgnFileSimulator(CommandExecutor commandExecutor, String fileName) {
|
public PgnFileSimulator(CommandExecutor commandExecutor, String fileName) {
|
||||||
super(commandExecutor, readResource(fileName));
|
super(commandExecutor, AssetManager.getResourceAsString(fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,15 +5,16 @@ import java.util.List;
|
|||||||
|
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.PlayerCommand;
|
import chess.controller.PlayerCommand;
|
||||||
import chess.controller.commands.NewGameCommand;
|
|
||||||
import chess.controller.event.GameAdaptator;
|
import chess.controller.event.GameAdaptator;
|
||||||
import chess.model.Game;
|
import common.Signal0;
|
||||||
|
|
||||||
public class PgnSimulator extends GameAdaptator {
|
public class PgnSimulator extends GameAdaptator {
|
||||||
|
|
||||||
private final CommandExecutor commandExecutor;
|
private final CommandExecutor commandExecutor;
|
||||||
private final String pgn;
|
private final String pgn;
|
||||||
|
|
||||||
|
public final Signal0 onComplete = new Signal0();
|
||||||
|
|
||||||
public PgnSimulator(CommandExecutor commandExecutor, String pgn) {
|
public PgnSimulator(CommandExecutor commandExecutor, String pgn) {
|
||||||
this.commandExecutor = commandExecutor;
|
this.commandExecutor = commandExecutor;
|
||||||
this.pgn = pgn;
|
this.pgn = pgn;
|
||||||
@@ -23,6 +24,7 @@ public class PgnSimulator extends GameAdaptator {
|
|||||||
public void onGameStart() {
|
public void onGameStart() {
|
||||||
List<PlayerCommand> cmds = PgnImport.importGame(this.pgn);
|
List<PlayerCommand> cmds = PgnImport.importGame(this.pgn);
|
||||||
this.commandExecutor.executeCommands(cmds);
|
this.commandExecutor.executeCommands(cmds);
|
||||||
|
this.onComplete.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import java.io.InputStream;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
import chess.view.AssetManager;
|
import common.AssetManager;
|
||||||
|
|
||||||
public class AudioFiles {
|
public class AudioFiles {
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import chess.model.pieces.Knight;
|
|||||||
import chess.model.pieces.Pawn;
|
import chess.model.pieces.Pawn;
|
||||||
import chess.model.pieces.Queen;
|
import chess.model.pieces.Queen;
|
||||||
import chess.model.pieces.Rook;
|
import chess.model.pieces.Rook;
|
||||||
import chess.view.AssetManager;
|
import common.AssetManager;
|
||||||
|
|
||||||
public class PieceIcon implements PieceVisitor<String> {
|
public class PieceIcon implements PieceVisitor<String> {
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package chess.view;
|
package common;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
public class AssetManager {
|
public class AssetManager {
|
||||||
|
|
||||||
@@ -22,6 +24,13 @@ public class AssetManager {
|
|||||||
return ClassLoader.getSystemResourceAsStream(name);
|
return ClassLoader.getSystemResourceAsStream(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getResourceAsString(String path) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(AssetManager.getResource(path)));
|
||||||
|
reader.lines().forEach((line) -> builder.append(line + "\n"));
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
private static InputStream getFileInputStream(String path) {
|
private static InputStream getFileInputStream(String path) {
|
||||||
File f = new File(path);
|
File f = new File(path);
|
||||||
if (f.exists()) {
|
if (f.exists()) {
|
||||||
@@ -14,7 +14,9 @@ import chess.controller.event.GameAdaptator;
|
|||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
import chess.pgn.PgnExport;
|
import chess.pgn.PgnExport;
|
||||||
|
import chess.pgn.PgnFileSimulator;
|
||||||
import chess.pgn.PgnImport;
|
import chess.pgn.PgnImport;
|
||||||
|
import common.AssetManager;
|
||||||
|
|
||||||
public class PgnTest {
|
public class PgnTest {
|
||||||
|
|
||||||
@@ -49,7 +51,7 @@ public class PgnTest {
|
|||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void importExport() {
|
private void importExportRandom() {
|
||||||
Game game = getRandomGame();
|
Game game = getRandomGame();
|
||||||
|
|
||||||
String pgnContent = PgnExport.exportGame(game);
|
String pgnContent = PgnExport.exportGame(game);
|
||||||
@@ -69,9 +71,54 @@ public class PgnTest {
|
|||||||
assertEquals(pgnContent, pgnContent2);
|
assertEquals(pgnContent, pgnContent2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test void importExports() {
|
private void importExportFile(String name) {
|
||||||
|
Game game = new Game();
|
||||||
|
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
|
PgnFileSimulator fileSimulator = new PgnFileSimulator(commandExecutor, name);
|
||||||
|
commandExecutor.addListener(fileSimulator);
|
||||||
|
|
||||||
|
final StringBuilder pgnContent = new StringBuilder();
|
||||||
|
|
||||||
|
fileSimulator.onComplete.connect(() -> {
|
||||||
|
pgnContent.append(PgnExport.exportGame(game));
|
||||||
|
|
||||||
|
commandExecutor.close();
|
||||||
|
|
||||||
|
synchronized (game) {
|
||||||
|
game.notifyAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
|
||||||
|
synchronized (game) {
|
||||||
|
try {
|
||||||
|
game.wait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String content1 = pgnContent.toString().trim();
|
||||||
|
String content2 = AssetManager.getResourceAsString(name).replaceAll("\n", " ").trim();
|
||||||
|
|
||||||
|
assertEquals(content1, content2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importExportFiles() {
|
||||||
|
importExportFile("games/CastlingTest.pgn");
|
||||||
|
importExportFile("games/EnPassantTest.pgn");
|
||||||
|
importExportFile("games/FoolCheckmate.pgn");
|
||||||
|
importExportFile("games/PromoteTest.pgn");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importExports() {
|
||||||
for (int i = 0; i < 50; i++) {
|
for (int i = 0; i < 50; i++) {
|
||||||
importExport();
|
importExportRandom();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user