Alpha-Beta move generator can look 2 plays (4 plies) ahead on a 4x4 board and blocks every possible attempt by the player to connect 3.
It should be possible to play on larger boards when the computers 'move' is changed from playing a tile to picking the player's next available color.
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
package model;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import player.AlphaBetaComPlayer;
|
||||
import player.HumanPlayer;
|
||||
import player.Player;
|
||||
import view.BoardPanel;
|
||||
import view.MessagePanel;
|
||||
import view.ScorePanel;
|
||||
import model.Board.TileColor;
|
||||
|
||||
public class Referee implements Runnable {
|
||||
@@ -10,26 +16,28 @@ public class Referee implements Runnable {
|
||||
public static final String GAME_OVER = "Game over!";
|
||||
public static final String PLAYER_TURN = "Waiting for the player's move.";
|
||||
|
||||
public static final Logger LOGGER = Logger.getLogger(Referee.class
|
||||
.getName());
|
||||
|
||||
private final Board board;
|
||||
private final HumanPlayer humanPlayer = new HumanPlayer();
|
||||
private final Player cc;
|
||||
private final Player computerPlayer;
|
||||
|
||||
private boolean playerTurn;
|
||||
private BoardPanel boardPanel;
|
||||
private int score = 0;
|
||||
private MessagePanel messagePanel;
|
||||
private ScorePanel scorePanel;
|
||||
|
||||
public Referee() {
|
||||
board = new Board();
|
||||
cc = new RandomComPlayer();
|
||||
playerTurn = true;
|
||||
computerPlayer = new AlphaBetaComPlayer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
int plies = 0;
|
||||
while (!isOver()) {
|
||||
if (playerTurn) {
|
||||
scorePanel.updateScore(getPlayerScore());
|
||||
if (board.isPlayerTurn()) {
|
||||
boolean wasInterrupted = false;
|
||||
while (!humanPlayer.isReady()) {
|
||||
try {
|
||||
@@ -39,7 +47,8 @@ public class Referee implements Runnable {
|
||||
}
|
||||
}
|
||||
if (wasInterrupted) {
|
||||
System.out.println("Interrupted while waiting for human to move!");
|
||||
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) {
|
||||
@@ -49,79 +58,65 @@ public class Referee implements Runnable {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Move mv = cc.getMove(board);
|
||||
Move mv = computerPlayer.getMove(board);
|
||||
playToken(mv);
|
||||
}
|
||||
|
||||
|
||||
messagePanel.updateMessage(getMessage());
|
||||
boardPanel.updateIcons();
|
||||
System.out.println("plies: " + plies++);
|
||||
LOGGER.info("plies: " + plies++);
|
||||
try {
|
||||
Thread.sleep(1000L);
|
||||
} catch (InterruptedException ie) {
|
||||
System.out.println("Interrupted while waiting for human view ply!");
|
||||
System.out
|
||||
.println("Interrupted while waiting for human view ply!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Player getComputerPlayer() {
|
||||
return cc;
|
||||
return computerPlayer;
|
||||
}
|
||||
|
||||
public HumanPlayer getHumanPlayer() {
|
||||
return humanPlayer;
|
||||
}
|
||||
|
||||
|
||||
public String getMessage() {
|
||||
if (isOver()) {
|
||||
return GAME_OVER;
|
||||
} else if (isPlayersTurn()) {
|
||||
} else if (board.isPlayerTurn()) {
|
||||
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;
|
||||
public int getPlayerScore() {
|
||||
return board.getTurn();
|
||||
}
|
||||
|
||||
private boolean isPlayersTurn() {
|
||||
return playerTurn;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
|
||||
public TileColor getTile(int r, int c) {
|
||||
return board.getTile(r, c);
|
||||
}
|
||||
|
||||
public boolean isOver() {
|
||||
return board.isTerminalState();
|
||||
}
|
||||
|
||||
public void playToken(Move move) {
|
||||
|
||||
board.setTile(move.getCell(), move.getColor());
|
||||
|
||||
if (playerTurn) {
|
||||
score++;
|
||||
}
|
||||
|
||||
playerTurn = !playerTurn;
|
||||
board.playTile(move.getCell(), move.getColor());
|
||||
}
|
||||
|
||||
public void setBoardPanel(BoardPanel boardPanel) {
|
||||
this.boardPanel = boardPanel;
|
||||
}
|
||||
|
||||
|
||||
public void setMessagePanel(MessagePanel messagePanel) {
|
||||
this.messagePanel = messagePanel;
|
||||
}
|
||||
}
|
||||
|
||||
public void setScorePanel(ScorePanel scorePanel) {
|
||||
this.scorePanel = scorePanel;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user