diff --git a/app/src/main/java/chess/App.java b/app/src/main/java/chess/App.java index cfabc1a..dede586 100644 --- a/app/src/main/java/chess/App.java +++ b/app/src/main/java/chess/App.java @@ -8,7 +8,6 @@ import chess.controller.commands.NewGameCommand; import chess.model.ChessBoard; import chess.model.Game; import chess.view.consolerender.Console; -import chess.view.simplerender.Window; public class App { public static void main(String[] args) { diff --git a/app/src/main/java/chess/controller/commands/GetAllowedMovesCommand.java b/app/src/main/java/chess/controller/commands/GetAllowedMovesPieceCommand.java similarity index 88% rename from app/src/main/java/chess/controller/commands/GetAllowedMovesCommand.java rename to app/src/main/java/chess/controller/commands/GetAllowedMovesPieceCommand.java index efc66a4..a040214 100644 --- a/app/src/main/java/chess/controller/commands/GetAllowedMovesCommand.java +++ b/app/src/main/java/chess/controller/commands/GetAllowedMovesPieceCommand.java @@ -10,12 +10,12 @@ import chess.model.Coordinate; import chess.model.Game; import chess.model.Piece; -public class GetAllowedMovesCommand extends Command { +public class GetAllowedMovesPieceCommand extends Command { private final Coordinate start; private List destinations; - public GetAllowedMovesCommand(Coordinate start) { + public GetAllowedMovesPieceCommand(Coordinate start) { this.start = start; this.destinations = new ArrayList<>(); } diff --git a/app/src/main/java/chess/controller/commands/GetPlayerMovesCommand.java b/app/src/main/java/chess/controller/commands/GetPlayerMovesCommand.java new file mode 100644 index 0000000..35f0cae --- /dev/null +++ b/app/src/main/java/chess/controller/commands/GetPlayerMovesCommand.java @@ -0,0 +1,24 @@ +package chess.controller.commands; + +import java.util.List; + +import chess.controller.Command; +import chess.controller.OutputSystem; +import chess.model.Game; +import chess.model.Move; + +public class GetPlayerMovesCommand extends Command { + + private List moves; + + @Override + public CommandResult execute(Game game, OutputSystem outputSystem) { + this.moves = game.getBoard().getAllowedMoves(game.getPlayerTurn()); + return CommandResult.NotMoved; + } + + public List getMoves() { + return moves; + } + +} diff --git a/app/src/main/java/chess/model/ChessBoard.java b/app/src/main/java/chess/model/ChessBoard.java index 3967c44..1490dc6 100644 --- a/app/src/main/java/chess/model/ChessBoard.java +++ b/app/src/main/java/chess/model/ChessBoard.java @@ -137,21 +137,54 @@ public class ChessBoard { } public boolean hasAllowedMoves(Color player) { - for (int i = 0; i < Coordinate.VALUE_MAX; i++) { - for (int j = 0; j < Coordinate.VALUE_MAX; j++) { - Coordinate attackCoords = new Coordinate(i, j); - Piece attackPiece = pieceAt(attackCoords); - if (attackPiece == null) + // for (int i = 0; i < Coordinate.VALUE_MAX; i++) { + // for (int j = 0; j < Coordinate.VALUE_MAX; j++) { + // Coordinate attackCoords = new Coordinate(i, j); + // Piece attackPiece = pieceAt(attackCoords); + // if (attackPiece == null) + // continue; + + // if (attackPiece.getColor() != player) + // continue; + + // if (!getAllowedMoves(attackCoords).isEmpty()) + // return true; + // } + // } + return !getAllowedMoves(player).isEmpty(); + } + + public List getAllowedMoves(Color player) { + List result = new ArrayList<>(); + + for (int x = 0; x < Coordinate.VALUE_MAX; x++) { + for (int y = 0; y < Coordinate.VALUE_MAX; y++) { + + Coordinate start = new Coordinate(x, y); + + Piece piece = pieceAt(start); + if (piece == null || piece.getColor() != player) continue; - if (attackPiece.getColor() != player) - continue; + for (int i = 0; i < Coordinate.VALUE_MAX; i++) { + for (int j = 0; j < Coordinate.VALUE_MAX; j++) { + Coordinate destination = new Coordinate(i, j); + Move move = new Move(start, destination); - if (!getAllowedMoves(attackCoords).isEmpty()) - return true; + PiecePathChecker piecePathChecker = new PiecePathChecker(this, + move); + if (!piecePathChecker.isValid()) + continue; + + applyMove(move); + if (!isKingInCheck(player)) + result.add(move); + undoLastMove(); + } + } } } - return false; + return result; } public List getAllowedMoves(Coordinate pieceCoords) { @@ -256,7 +289,6 @@ public class ChessBoard { return null; } - public Move getLastMove() { return this.lastMove; } diff --git a/app/src/main/java/chess/view/simplerender/Window.java b/app/src/main/java/chess/view/simplerender/Window.java index 3aaa352..a6aedf3 100644 --- a/app/src/main/java/chess/view/simplerender/Window.java +++ b/app/src/main/java/chess/view/simplerender/Window.java @@ -7,6 +7,7 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.IOException; import java.util.List; +import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; @@ -20,8 +21,9 @@ import chess.controller.Command.CommandResult; import chess.controller.CommandExecutor; import chess.controller.OutputSystem; import chess.controller.commands.CastlingCommand; -import chess.controller.commands.GetAllowedMovesCommand; +import chess.controller.commands.GetAllowedMovesPieceCommand; import chess.controller.commands.GetPieceAtCommand; +import chess.controller.commands.GetPlayerMovesCommand; import chess.controller.commands.MoveCommand; import chess.controller.commands.PromoteCommand; import chess.controller.commands.PromoteCommand.PromoteType; @@ -139,7 +141,7 @@ public class Window extends JFrame implements OutputSystem { } private boolean previewMoves(int x, int y) { - GetAllowedMovesCommand movesCommand = new GetAllowedMovesCommand(new Coordinate(x, y)); + GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(new Coordinate(x, y)); if (sendCommand(movesCommand) == CommandResult.NotAllowed) return false; @@ -196,6 +198,15 @@ public class Window extends JFrame implements OutputSystem { @Override public void playerTurn(chess.model.Color color) { this.displayText.setText("Current turn: " + color); + + // dumb IA + if (color == chess.model.Color.Black) { + GetPlayerMovesCommand cmd = new GetPlayerMovesCommand(); + sendCommand(cmd); + List moves = cmd.getMoves(); + int random = new Random().nextInt(moves.size()); + sendCommand(new MoveCommand(moves.get(random))); + } } @Override