package model; import view.BoardPanel; import view.MessagePanel; import model.Board.TileColor; public class Referee implements Runnable { public static final String COM_TURN = "Waiting for the computer's move."; public static final String GAME_OVER = "Game over!"; public static final String PLAYER_TURN = "Waiting for the player's move."; private final Board board; private final HumanPlayer humanPlayer = new HumanPlayer(); private final Player cc; private boolean playerTurn; private BoardPanel boardPanel; private int score = 0; private MessagePanel messagePanel; public Referee() { board = new Board(); cc = new RandomComPlayer(); playerTurn = true; } @Override public void run() { int plies = 0; while (!isOver()) { if (playerTurn) { boolean wasInterrupted = false; while (!humanPlayer.isReady()) { try { Thread.sleep(250L); } catch (InterruptedException ie) { wasInterrupted = true; } } if (wasInterrupted) { System.out.println("Interrupted while waiting for human to move!"); } else { Move mv = humanPlayer.getMove(board); if (board.getTile(mv.getCell().r, mv.getCell().c) == TileColor.NONE) { playToken(humanPlayer.getMove(board)); } else { humanPlayer.denyMove(); } } } else { Move mv = cc.getMove(board); playToken(mv); } messagePanel.updateMessage(getMessage()); boardPanel.updateIcons(); System.out.println("plies: " + plies++); try { Thread.sleep(1000L); } catch (InterruptedException ie) { System.out.println("Interrupted while waiting for human view ply!"); } } } public Player getComputerPlayer() { return cc; } public HumanPlayer getHumanPlayer() { return humanPlayer; } public String getMessage() { if (isOver()) { return GAME_OVER; } else if (isPlayersTurn()) { return PLAYER_TURN; } else { return COM_TURN; } } public boolean isOver() { for (int r = 0; r < Board.NUM_ROWS; r++) { for (int c = 0; c < Board.NUM_COLS; c++) { if (board.getTile(r, c) == TileColor.NONE) { return false; } } } return true; } private boolean isPlayersTurn() { return playerTurn; } public int getScore() { return score; } public TileColor getTile(int r, int c) { return board.getTile(r, c); } public void playToken(Move move) { board.setTile(move.getCell(), move.getColor()); if (playerTurn) { score++; } playerTurn = !playerTurn; } public void setBoardPanel(BoardPanel boardPanel) { this.boardPanel = boardPanel; } public void setMessagePanel(MessagePanel messagePanel) { this.messagePanel = messagePanel; } }