package model;
import GameIO.*;
import java.awt.Point;
import java.util.*;
import model.board.*;
/**
* A concrete model of a game. For descriptions of the methods, see the IModel,
* IModelAdmin, and IModelCombo interface documentation.
* Except for the setPlayers() and getPlayers() methods, see below.
*/
public final class GameModel implements IModelCombo {
/**
* An abstract factory to create APlayers. Used privately by GameModel.
*/
interface IMakePlayer {
/**
* Instantiates an APlayer object given the player's "player number".
* Player number 0 plays first, player number 1 plays second.
* @param playerNo The player number for the player to be instantiated.
* @return An APlayer object
*/
public APlayer create(int playerNo);
}
/**
* For player management.
*/
private TurnControl turnControl;
/**
* Adapter to talk to the view to display/clear a game piece ("token") or
* a String message.
*/
private ICommand iCommand;
/**
* Adapter to talk to the view to announce winner, draw, or reset.
*/
private IViewAdmin viewAdmin;
/**
* Adapter to talk to the view to tell that the human player needs to try a
* move.
*/
private ITurnAdmin turnAdmin;
/**
* The invariant, encapsulated rules and behaviors of a game.
*/
private IBoardModel boardModel;
/**
* Initializes the board model to Tic-Tac-Toe.
* @param nRows The number of rows in the board.
* @param nCols The number of columns in the board.
*/
public GameModel(int nRows, int nCols) {
boardModel = new TicTacToeBoard(nRows,nCols, this);
// boardModel = new OthelloBoard(nRows, nCols);
}
public GameModel(IBoardModel boardModel) {
this.boardModel = boardModel;
}
/**
* The facade that does "everything"!
* Use anonymous inner class to have access to everything in the outer object.
*/
private IRequestor requestor ; // = ??? ANONYMOUS CLASS FOR STUDENTS TO FILL OUT .
/**
* @param command
*/
public void setCommand(ICommand command) {
iCommand = command;
}
public void reset() {
System.out.println("Resetting");
boardModel.reset();
boardModel.redrawAll(iCommand);
if(turnControl != null) {
turnControl.setHalt();
}
}
/**
* Assumes that the players are IMakePlayer factory objects.
* @param player0 an IMakePlayer factory
* @param player1 an IMakePlayer factory
*/
public void setPlayers(Object player0, Object player1) {
// Last in/first play
turnControl = new TurnControl(((IMakePlayer) player1).create(1));
turnControl.addPlayer(((IMakePlayer) player0).create(0));
turnControl.run();
}
public void setViewAdmin(IViewAdmin viewAdmin, ITurnAdmin turnAdmin) {
this.viewAdmin = viewAdmin;
this.turnAdmin = turnAdmin;
}
public IBoardModel getBoardModel() {
return boardModel;
}
/**
* Returns a Vector filled with IMakePlayer factory objects.
* @return
*/
public Vector getPlayers() {
Vector v = new Vector();
v.addElement(new IMakePlayer() {
public APlayer create(int playerNo) {
return new HumanPlayer(requestor, playerNo, turnAdmin);
}
public String toString() {
return "Human player";
}
});
// ADD COMPUTER PLAYER WITH RANDOM MOVE STRATEGY IN MILESTONE 1.
// ADD COMPUTER PLAYER WITH OTHER NEXT MOVE STRATEGY IN MILESTONE 2.
return v;
}
public void exit() {
reset();
}
}