lots of things
This commit is contained in:
7
app/src/main/java/chess/io/Command.java
Normal file
7
app/src/main/java/chess/io/Command.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package chess.io;
|
||||
|
||||
import chess.model.Game;
|
||||
|
||||
public abstract class Command {
|
||||
public abstract CommandResult execute(Game game, OutputSystem outputSystem);
|
||||
}
|
||||
37
app/src/main/java/chess/io/CommandExecutor.java
Normal file
37
app/src/main/java/chess/io/CommandExecutor.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package chess.io;
|
||||
|
||||
import chess.model.Game;
|
||||
|
||||
public class CommandExecutor {
|
||||
|
||||
private final Game game;
|
||||
private final OutputSystem outputSystem;
|
||||
|
||||
public CommandExecutor(Game game, OutputSystem outputSystem) {
|
||||
this.game = game;
|
||||
this.outputSystem = outputSystem;
|
||||
}
|
||||
|
||||
public CommandResult executeCommand(Command command) {
|
||||
CommandResult result = command.execute(this.game, this.outputSystem);
|
||||
if (result == CommandResult.Moved)
|
||||
alternatePlayers();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void alternatePlayers() {
|
||||
this.game.switchPlayerTurn();
|
||||
this.outputSystem.playerTurn(this.game.getPlayerTurn());
|
||||
}
|
||||
|
||||
public Game getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
public OutputSystem getOutputSystem() {
|
||||
return outputSystem;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
5
app/src/main/java/chess/io/CommandResult.java
Normal file
5
app/src/main/java/chess/io/CommandResult.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package chess.io;
|
||||
|
||||
public enum CommandResult {
|
||||
Moved, NotMoved, NotAllowed;
|
||||
}
|
||||
8
app/src/main/java/chess/io/OutputSystem.java
Normal file
8
app/src/main/java/chess/io/OutputSystem.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package chess.io;
|
||||
|
||||
public abstract class OutputSystem implements OutputSystemInterface {
|
||||
|
||||
public OutputSystem() {
|
||||
}
|
||||
|
||||
}
|
||||
18
app/src/main/java/chess/io/OutputSystemInterface.java
Normal file
18
app/src/main/java/chess/io/OutputSystemInterface.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package chess.io;
|
||||
|
||||
import chess.model.Color;
|
||||
|
||||
public interface OutputSystemInterface {
|
||||
|
||||
void playerTurn(Color color);
|
||||
|
||||
void winnerIs(Color color);
|
||||
|
||||
void kingIsInCheck();
|
||||
|
||||
void kingIsInMat();
|
||||
|
||||
void patSituation();
|
||||
|
||||
void hasSurrendered(Color color);
|
||||
}
|
||||
15
app/src/main/java/chess/io/commands/CastlingCommand.java
Normal file
15
app/src/main/java/chess/io/commands/CastlingCommand.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.model.Game;
|
||||
|
||||
public class CastlingCommand extends Command{
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
return CommandResult.NotAllowed;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.model.Game;
|
||||
|
||||
public class GrandCastlingCommand extends Command{
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
return CommandResult.NotAllowed;
|
||||
}
|
||||
|
||||
}
|
||||
63
app/src/main/java/chess/io/commands/MoveCommand.java
Normal file
63
app/src/main/java/chess/io/commands/MoveCommand.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Color;
|
||||
import chess.model.Game;
|
||||
import chess.model.Move;
|
||||
import chess.model.Piece;
|
||||
import chess.model.visitor.PiecePathChecker;
|
||||
|
||||
public class MoveCommand extends Command {
|
||||
private final Move move;
|
||||
|
||||
public MoveCommand(Move move) {
|
||||
this.move = move;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
Piece piece = board.pieceAt(move.getStart());
|
||||
if (piece == null)
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
if (piece.getColor() != game.getPlayerTurn())
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
boolean valid = new PiecePathChecker(board, move).isValid();
|
||||
if (!valid)
|
||||
return CommandResult.NotAllowed;
|
||||
|
||||
board.applyMove(move);
|
||||
|
||||
if (board.isKingInCheck(game.getPlayerTurn())) {
|
||||
board.undoLastMove();
|
||||
return CommandResult.NotAllowed;
|
||||
}
|
||||
|
||||
checkGameStatus(game, outputSystem);
|
||||
|
||||
return CommandResult.Moved;
|
||||
}
|
||||
|
||||
private void checkGameStatus(Game game, OutputSystem outputSystem) {
|
||||
final ChessBoard board = game.getBoard();
|
||||
|
||||
final Color enemy = Color.getEnemy(game.getPlayerTurn());
|
||||
|
||||
if (board.isKingInCheck(enemy)) {
|
||||
if (board.hasAllowedMoves(enemy)) {
|
||||
outputSystem.kingIsInCheck();
|
||||
} else {
|
||||
outputSystem.kingIsInMat();
|
||||
outputSystem.winnerIs(game.getPlayerTurn());
|
||||
}
|
||||
} else if(!board.hasAllowedMoves(enemy)) {
|
||||
outputSystem.patSituation();
|
||||
}
|
||||
}
|
||||
}
|
||||
54
app/src/main/java/chess/io/commands/NewGameCommand.java
Normal file
54
app/src/main/java/chess/io/commands/NewGameCommand.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.model.ChessBoard;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Game;
|
||||
import chess.model.pieces.Bishop;
|
||||
import chess.model.pieces.King;
|
||||
import chess.model.pieces.Knight;
|
||||
import chess.model.pieces.Pawn;
|
||||
import chess.model.pieces.Queen;
|
||||
import chess.model.pieces.Rook;
|
||||
|
||||
public class NewGameCommand extends Command {
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
ChessBoard board = game.getBoard();
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
board.pieceComes(new Pawn(Color.Black), new Coordinate(i, 0));
|
||||
board.pieceComes(new Pawn(Color.White), new Coordinate(i, Coordinate.VALUE_MAX - 1));
|
||||
}
|
||||
|
||||
board.pieceComes(new Rook(Color.Black), new Coordinate(0, 1));
|
||||
board.pieceComes(new Rook(Color.Black), new Coordinate(Coordinate.VALUE_MAX - 1, 1));
|
||||
|
||||
board.pieceComes(new Rook(Color.White), new Coordinate(0, Coordinate.VALUE_MAX - 2));
|
||||
board.pieceComes(new Rook(Color.White), new Coordinate(Coordinate.VALUE_MAX - 1, Coordinate.VALUE_MAX - 2));
|
||||
|
||||
board.pieceComes(new Knight(Color.Black), new Coordinate(1, 1));
|
||||
board.pieceComes(new Knight(Color.Black), new Coordinate(Coordinate.VALUE_MAX - 2, 1));
|
||||
|
||||
board.pieceComes(new Knight(Color.White), new Coordinate(1, Coordinate.VALUE_MAX - 2));
|
||||
board.pieceComes(new Knight(Color.White), new Coordinate(Coordinate.VALUE_MAX - 2, Coordinate.VALUE_MAX - 2));
|
||||
|
||||
board.pieceComes(new Bishop(Color.Black), new Coordinate(2, 1));
|
||||
board.pieceComes(new Bishop(Color.Black), new Coordinate(Coordinate.VALUE_MAX - 3, 1));
|
||||
|
||||
board.pieceComes(new Bishop(Color.White), new Coordinate(2, Coordinate.VALUE_MAX - 2));
|
||||
board.pieceComes(new Bishop(Color.White), new Coordinate(Coordinate.VALUE_MAX - 3, Coordinate.VALUE_MAX - 2));
|
||||
|
||||
board.pieceComes(new Queen(Color.Black), new Coordinate(4, 1));
|
||||
board.pieceComes(new King(Color.Black), new Coordinate(3, 1));
|
||||
|
||||
board.pieceComes(new Queen(Color.White), new Coordinate(4, Coordinate.VALUE_MAX - 2));
|
||||
board.pieceComes(new King(Color.White), new Coordinate(3, Coordinate.VALUE_MAX - 2));
|
||||
|
||||
game.resetPlayerTurn();
|
||||
|
||||
return CommandResult.NotMoved;
|
||||
}
|
||||
}
|
||||
24
app/src/main/java/chess/io/commands/SurrenderCommand.java
Normal file
24
app/src/main/java/chess/io/commands/SurrenderCommand.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package chess.io.commands;
|
||||
|
||||
import chess.io.Command;
|
||||
import chess.io.CommandResult;
|
||||
import chess.io.OutputSystem;
|
||||
import chess.model.Color;
|
||||
import chess.model.Game;
|
||||
|
||||
public class SurrenderCommand extends Command {
|
||||
|
||||
private final Color player;
|
||||
|
||||
public SurrenderCommand(Color player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandResult execute(Game game, OutputSystem outputSystem) {
|
||||
outputSystem.hasSurrendered(player);
|
||||
outputSystem.winnerIs(Color.getEnemy(player));
|
||||
return CommandResult.NotMoved;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user