vooga.examples.networking.tictactoe.states
Class PlayState

java.lang.Object
  extended by vooga.engine.state.GameState
      extended by vooga.examples.networking.tictactoe.states.PlayState

public class PlayState
extends GameState

The main GameState for the TicTacToe game. Handles placing pieces and dealing with the network to get opposing moves.


Field Summary
 
Fields inherited from class vooga.engine.state.GameState
myRenderField, myUpdateField
 
Constructor Summary
PlayState(Game game, LevelManager levelManager, ClientConnection connection)
          Constructor for the PlayState.
 
Method Summary
 void addPiece()
          Called by Control whenever the user clicks in a square.
 boolean checkMessage()
          If there is a message to send then call the method that corresponds with that message and then set the message to null.
 void initControls()
          Set up Control object that handles adding a piece when the user clicks in a square.
 void initialize()
          Load the first and only level, set up the Control object for clicking to add pieces, and create all the different GameStates and Events
 void initLevel()
          Sets up the gameWon, wait, youLost, theyQuit, and tie GameStates as well as the GameWon, GameLost, and GameTied events.
 void interpretMessage(java.lang.String data)
          If what we're parsing is an Integer and therefore an instance of Move, then deserialize the String into a Move object and place the piece on the board.
 void lost()
          Called when the player loses.
 void placeOpposingPiece()
          Called in interpretMessage if the data its parsing is an integer so it represents a Move.
 void quit()
          Called when the other player quits.
 void setMessage(java.lang.String message)
          Sets the message.
 void theirTurn()
          Called when it is the opponent's turn.
 void tied()
          Called when the player ties.
 void update(long elapsedTime)
          First checks the events to make sure there won't be any message changes.
 void won()
          Called when the player wins.
 void yourTurn()
          Called when the it is the player's turn.
 
Methods inherited from class vooga.engine.state.GameState
activate, addGroup, addPlayField, addRenderPlayField, addRenderState, addState, addUpdatePlayField, addUpdateState, deactivate, equals, getGroup, getRenderField, getUpdateField, isActive, removeEverything, render
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PlayState

public PlayState(Game game,
                 LevelManager levelManager,
                 ClientConnection connection)
Constructor for the PlayState.

Parameters:
game - Game for the GameStateManager and other convenience methods
levelManager - the LevelManager for making the other PlayStates
connection - the ClientConnection through which all communication with the server is done
Method Detail

initialize

public void initialize()
Load the first and only level, set up the Control object for clicking to add pieces, and create all the different GameStates and Events

Specified by:
initialize in class GameState

initControls

public void initControls()
Set up Control object that handles adding a piece when the user clicks in a square.


initLevel

public void initLevel()
Sets up the gameWon, wait, youLost, theyQuit, and tie GameStates as well as the GameWon, GameLost, and GameTied events.


addPiece

public void addPiece()
Called by Control whenever the user clicks in a square. Decides whether or not to add it based on if there is a piece already in that square. If it's empty, it adds the piece, sets myTurn to false, and sends the Move with the row and column of where the piece was placed to the socket with the line connection.sendData(new Move(row, col));


placeOpposingPiece

public void placeOpposingPiece()
Called in interpretMessage if the data its parsing is an integer so it represents a Move. It gets the row and the column from the Move and adds it to the board.


setMessage

public void setMessage(java.lang.String message)
Sets the message. The message is initialized to anything sent through the socket that is not a Serializeable object so just a String. For our purposes it is used to determine state (i.e. the String won corresponds with the won() method which changes the Game to the gameWonState.)

Parameters:
message - to set the message instance to

interpretMessage

public void interpretMessage(java.lang.String data)
If what we're parsing is an Integer and therefore an instance of Move, then deserialize the String into a Move object and place the piece on the board. Otherwise set the message to the String we received.

Parameters:
data - data received from the socket

checkMessage

public boolean checkMessage()
If there is a message to send then call the method that corresponds with that message and then set the message to null.

Returns:
whether or not it sent a message

update

public void update(long elapsedTime)
First checks the events to make sure there won't be any message changes. Then call checkMessage to follow the message protocol. Then, if there were no messages to change state and it's not myTurn, then get the next piece of data from the socket and call interpretMessage with it.

Overrides:
update in class GameState

won

public void won()
Called when the player wins. Tells the server the game is over and to cease the connection and switches to the gameWonState.


tied

public void tied()
Called when the player ties. Tells the server the game is over and to cease the connection and switches to the tieState.


lost

public void lost()
Called when the player loses. Tells the server the game is over and to cease the connection and switches to the youLostState.


quit

public void quit()
Called when the other player quits. Tells the server the game is over and to cease the connection and switches to the theyQuitState.


theirTurn

public void theirTurn()
Called when it is the opponent's turn. Switches to the theirTurnState until further notice.


yourTurn

public void yourTurn()
Called when the it is the player's turn. Just sets myTurn to true so the player knows they can go.