-The rest of the game engine. I screwed up the commit last time.

This commit is contained in:
Marshall
2012-03-31 14:18:08 -04:00
parent 2954041e89
commit 521fac6e08
45 changed files with 1006 additions and 0 deletions

154
src/model/Referee.java Normal file
View File

@@ -0,0 +1,154 @@
package model;
import javax.security.auth.Refreshable;
import model.Board.TileColor;
import model.comController.RandomComController;
import controller.PlayerController;
public class Referee implements Refreshable {
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.";
}
}
}
private final Board board;
private final ComController cc;
private final PlayerController pc = new PlayerController();
private boolean playerTurn;
private boolean refresh = true;
private int score = 0;
public Referee() {
board = new Board();
cc = new RandomComController(board);
playerTurn = true;
}
public void doSomething() {
Move mv;
if (playerTurn && pc.isReady()) {
mv = pc.getMove();
if (board.getTile(mv.getCell().r, mv.getCell().c) == TileColor.NONE) {
playToken(pc.getMove());
refresh = true;
}
else {
pc.denyMove();
}
}
else if (!playerTurn) {
mv = cc.getMove();
playToken(mv);
refresh = true;
}
}
public ComController getCom() {
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 PlayerController getPlayer() {
return pc;
}
public int getScore() {
return score;
}
public TileColor getTile(int r, int c) {
return board.getTile(r, c);
}
@Override
public boolean isCurrent() {
return !refresh;
}
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 void playToken(Move move) {
board.setTile(move.getCell(), move.getColor());
if (playerTurn) {
incrementScore();
}
incrementTurn();
}
@Override
public void refresh() {
refresh = false;
}
private void incrementScore() {
score++;
}
private void incrementTurn() {
playerTurn = !playerTurn;
}
private boolean isPlayersTurn() {
return playerTurn;
}
}