Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class HumanTTTPlayer extends Player {
private boolean isTurn;// store if it is my turn
public HumanTTTPlayer(BoardGame work, BoardGame.GameInputController inputCtl) {
super(work, inputCtl);
}
// called when it's my turn
@Override
public void onTurn() {
isTurn = true;
}
public boolean isTurn() {
return isTurn;
}
public void deliverTurn(int row, int col) {
if (isTurn) {
inputController().placeMove(new TTTBoardGame.Move(inputController().playerRotator.getCurrentId(), // our player id
row, col));
isTurn = false;
}
}
}
public class TTTBoard extends Board {
// must override this method otherwise the cellData array will contain
// all null values.
@Override
protected void allocateCellData() {
super.allocateCellData();// allocate the array for our pieces called cellData
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
cellData[r][c] = new Board.Cell();// Yup!
}
}
}
public TTTBoard() {
super(3, 3);
allocateCellData();
}
@Override
public boolean handle(Move move) {
// A move contains a source row, source column, destination row and
// destination column. BUT TIC-TAC-TOE only requires one set of coordinates
// as it doesn't "move" pieces like in chess.
if (!(move instanceof TTTBoardGame.Move))
return false;// it should be over custom move object
final TTTBoardGame.Move tttmove = (TTTBoard.Move) move;
// Just for our case, we use the destination row & column for our move.
// Just ignore source-row & source-column.
if (getCell(move.getDstRow(), move.getDstCol().getHolder() == null) {
getCell(move.getDstRow(), move.getDstCol()).setHolder(new Piece(ttt.moverPlayer, ttt.moverPlayer));
// O's and X's are identical pieces themselves
return true;
}
return false;
}
// Our cell (or squares on the board) will hold only one piece
// at a time.
public static class Cell implements com.silcos.board.Cell {
private Piece holder;
@Override
public Piece getHolder() {
return holder;
}
@Override
public void setHolder(Piece o) {
holder = o;
}
}
}
public class TTTBoardGame extends BoardGame {
public static class Move extends com.silcos.board.Move {
public final int moverPlayer;
public Move(int moverPlayer, int row, int col) {
super(true, 0, 0, row, col);// we're not using srcRow, srcCol
this.moverPlayer = moverPlayer;
}
}
public TTTBoardGame() {
// Tell that two player will take turns in a circular fashion.
super(new CircularPlayerRotator.Factory(), 2);
mBoard = new TTTBoard();
}
@Override
public TTTBoard board() {
return (TTTBoard) mBoard;
}
public static TTTBoardGame newTwoHumanPlayerGame(TTTBoardGame tttgame, BoardEventListener hostListener) {
final TTTHumanPlayer Oplayer = new TTTHumanPlayer(tttgame, tttgame.mController);
final TTTHumanPlayer Xplayer = new TTTHumanPlayer(tttgame, tttgame.mController);
tttgame.addBoardEventListener(hostListener);// this is required so that the activity can initialize the board
tttgame.mPlayer = new Player[]{ Oplayer, Xplayer };
tttgame.renewInputController();// we registered new player, so yah!
// Oplayer.initAfterGame(); Xplayer.initAfterGame(); we don't use this method in TTT
tttgame.mPlayerStats[0] = tttgame.new BoardGame.PlayerStats(Oplayer);
tttgame.mPlayerStats[1] = tttgame.new BoardGame.PlayerStats(Xplayer);
tttgame.dispatchPlayerWiringEvent();// initialization
}
public static BoardGame.GameInputController extractCtl(TTTBoardGame game) {
return game.mController;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.