105 lines
3.4 KiB
Java
105 lines
3.4 KiB
Java
package dev.kske.chess.game;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import dev.kske.chess.board.Board;
|
|
import dev.kske.chess.board.GameState;
|
|
import dev.kske.chess.board.Move;
|
|
import dev.kske.chess.board.Piece.Color;
|
|
import dev.kske.chess.game.ai.AIPlayer;
|
|
import dev.kske.chess.ui.BoardComponent;
|
|
import dev.kske.chess.ui.BoardPane;
|
|
import dev.kske.chess.ui.OverlayComponent;
|
|
|
|
/**
|
|
* Project: <strong>Chess</strong><br>
|
|
* File: <strong>Game.java</strong><br>
|
|
* Created: <strong>06.07.2019</strong><br>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class Game {
|
|
|
|
private Map<Color, Player> players;
|
|
private Board board;
|
|
private OverlayComponent overlayComponent;
|
|
private BoardComponent boardComponent;
|
|
|
|
public Game(Map<Color, Player> players, BoardPane boardPane) {
|
|
this.players = players;
|
|
this.overlayComponent = boardPane.getOverlayComponent();
|
|
this.boardComponent = boardPane.getBoardComponent();
|
|
this.board = new Board();
|
|
boardComponent.setBoard(board);
|
|
|
|
// Initialize the game variable in each player
|
|
players.values().forEach(player -> player.setGame(this));
|
|
}
|
|
|
|
public static Game createNatural(BoardPane boardPane) {
|
|
Map<Color, Player> players = new HashMap<>();
|
|
OverlayComponent overlay = boardPane.getOverlayComponent();
|
|
|
|
players.put(Color.WHITE, new NaturalPlayer(Color.WHITE, overlay));
|
|
players.put(Color.BLACK, new NaturalPlayer(Color.BLACK, overlay));
|
|
return new Game(players, boardPane);
|
|
}
|
|
|
|
public static Game createNaturalVsAI(BoardPane boardPane, int maxDepth, int alphaBeta) {
|
|
Map<Color, Player> players = new HashMap<>();
|
|
OverlayComponent overlay = boardPane.getOverlayComponent();
|
|
|
|
players.put(Color.WHITE, new NaturalPlayer(Color.WHITE, overlay));
|
|
players.put(Color.BLACK, new AIPlayer(Color.BLACK, maxDepth, alphaBeta));
|
|
return new Game(players, boardPane);
|
|
}
|
|
|
|
public static Game createAIVsAI(BoardPane boardPane, int maxDepthW, int maxDepthB, int alphaBetaW, int alphaBetaB) {
|
|
Map<Color, Player> players = new HashMap<>();
|
|
|
|
players.put(Color.WHITE, new AIPlayer(Color.WHITE, maxDepthW, alphaBetaW));
|
|
players.put(Color.BLACK, new AIPlayer(Color.BLACK, maxDepthB, alphaBetaB));
|
|
return new Game(players, boardPane);
|
|
}
|
|
|
|
public void onMove(Player player, Move move) {
|
|
if (board.getPos(move).getColor() == player.color && board.attemptMove(move)) {
|
|
System.out.printf("%s: %s%n", player.color, move);
|
|
GameState eventType = board.getGameEventType(board.getDest(move).getColor().opposite());
|
|
switch (eventType) {
|
|
case CHECKMATE:
|
|
case STALEMATE:
|
|
System.out.printf("%s in %s!%n", player.color.opposite(), eventType);
|
|
break;
|
|
case CHECK:
|
|
System.out.printf("%s in check!%n", player.color.opposite());
|
|
default:
|
|
boardComponent.repaint();
|
|
players.get(player.color.opposite()).requestMove();
|
|
}
|
|
overlayComponent.displayArrow(move);
|
|
} else player.requestMove();
|
|
}
|
|
|
|
public void start() {
|
|
players.get(Color.WHITE).requestMove();
|
|
}
|
|
|
|
public void reset() {
|
|
players.forEach((k, v) -> v.cancelMove());
|
|
board.initializeDefaultPositions();
|
|
boardComponent.repaint();
|
|
overlayComponent.clearDots();
|
|
overlayComponent.clearArrow();
|
|
}
|
|
|
|
/**
|
|
* Removed all connections between the game and the ui.
|
|
*/
|
|
public void disconnect() {
|
|
players.values().forEach(Player::disconnect);
|
|
}
|
|
|
|
public Board getBoard() { return board; }
|
|
}
|