iaaaaaaaaaaaaaa

This commit is contained in:
2025-04-10 12:26:09 +02:00
parent 3226e72d32
commit 17d8333342
5 changed files with 82 additions and 16 deletions

View File

@@ -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) {

View File

@@ -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<Coordinate> destinations;
public GetAllowedMovesCommand(Coordinate start) {
public GetAllowedMovesPieceCommand(Coordinate start) {
this.start = start;
this.destinations = new ArrayList<>();
}

View File

@@ -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<Move> moves;
@Override
public CommandResult execute(Game game, OutputSystem outputSystem) {
this.moves = game.getBoard().getAllowedMoves(game.getPlayerTurn());
return CommandResult.NotMoved;
}
public List<Move> getMoves() {
return moves;
}
}

View File

@@ -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<Move> getAllowedMoves(Color player) {
List<Move> 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<Coordinate> getAllowedMoves(Coordinate pieceCoords) {
@@ -256,7 +289,6 @@ public class ChessBoard {
return null;
}
public Move getLastMove() {
return this.lastMove;
}

View File

@@ -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<Move> moves = cmd.getMoves();
int random = new Random().nextInt(moves.size());
sendCommand(new MoveCommand(moves.get(random)));
}
}
@Override