Revampled MainFrame layout to improve appearance/usability when the number of tiles is very large or small.
Fixed instantiation where a new MouseListener was used to each tile - now only one is used for all Tiles. Fixed issue where the MouseListener was being added to each tile twice.
This commit is contained in:
@@ -1,42 +1,23 @@
|
||||
package model;
|
||||
|
||||
import view.BoardPanel;
|
||||
import view.MessagePanel;
|
||||
import model.Board.TileColor;
|
||||
|
||||
public class Referee {
|
||||
public class Referee implements Runnable {
|
||||
|
||||
public enum Message {
|
||||
COM_TURN {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Waiting for the computer's move.";
|
||||
}
|
||||
},
|
||||
GAME_OVER {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Game over!";
|
||||
}
|
||||
},
|
||||
NONE {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "";
|
||||
}
|
||||
},
|
||||
PLAYER_TURN {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Waiting for the player's move.";
|
||||
}
|
||||
}
|
||||
}
|
||||
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();
|
||||
@@ -44,50 +25,63 @@ public class Referee {
|
||||
playerTurn = true;
|
||||
}
|
||||
|
||||
public void doSomething() {
|
||||
if (playerTurn && humanPlayer.isReady()) {
|
||||
Move mv = humanPlayer.getMove(board);
|
||||
if (board.getTile(mv.getCell().r, mv.getCell().c) == TileColor.NONE) {
|
||||
playToken(humanPlayer.getMove(board));
|
||||
@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 {
|
||||
humanPlayer.denyMove();
|
||||
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!");
|
||||
}
|
||||
} else if (!playerTurn) {
|
||||
Move mv = cc.getMove(board);
|
||||
playToken(mv);
|
||||
}
|
||||
}
|
||||
|
||||
public Player getCom() {
|
||||
public Player getComputerPlayer() {
|
||||
return cc;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
if (isOver()) {
|
||||
return Message.GAME_OVER.toString();
|
||||
}
|
||||
|
||||
else if (isPlayersTurn()) {
|
||||
return Message.PLAYER_TURN.toString();
|
||||
}
|
||||
|
||||
else {
|
||||
return Message.COM_TURN.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public HumanPlayer getHumanPlayer() {
|
||||
return humanPlayer;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
|
||||
public String getMessage() {
|
||||
if (isOver()) {
|
||||
return GAME_OVER;
|
||||
} else if (isPlayersTurn()) {
|
||||
return PLAYER_TURN;
|
||||
} else {
|
||||
return COM_TURN;
|
||||
}
|
||||
}
|
||||
|
||||
public TileColor getTile(int r, int c) {
|
||||
return board.getTile(r, c);
|
||||
}
|
||||
|
||||
|
||||
public boolean isOver() {
|
||||
for (int r = 0; r < Board.NUM_ROWS; r++) {
|
||||
for (int c = 0; c < Board.NUM_COLS; c++) {
|
||||
@@ -99,27 +93,35 @@ public class Referee {
|
||||
|
||||
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) {
|
||||
incrementScore();
|
||||
score++;
|
||||
}
|
||||
|
||||
incrementTurn();
|
||||
}
|
||||
|
||||
private void incrementScore() {
|
||||
score++;
|
||||
}
|
||||
|
||||
private void incrementTurn() {
|
||||
playerTurn = !playerTurn;
|
||||
}
|
||||
|
||||
private boolean isPlayersTurn() {
|
||||
return playerTurn;
|
||||
public void setBoardPanel(BoardPanel boardPanel) {
|
||||
this.boardPanel = boardPanel;
|
||||
}
|
||||
|
||||
public void setMessagePanel(MessagePanel messagePanel) {
|
||||
this.messagePanel = messagePanel;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user