pictionary
Class PictionaryGame

java.lang.Object
  extended by pictionary.PictionaryGame

public class PictionaryGame
extends java.lang.Object

Implementation of a game of Pictionary.
This class contains a collection of PictionaryPlayer, methods for interacting with the players and methods and variables to keep track of and advance the game state.

Author:
Kristoffer Nordkvist

Nested Class Summary
private  class PictionaryGame.EndDrawTask
          A simple class, overriding TimerTask.run(), used when scheduling the game's next action, created when a player starts drawing.
private  class PictionaryGame.NextActionTask
          A simple class, overriding TimerTask.run(), used when scheduling the game's next action.
 
Field Summary
private  java.lang.String category
          Some text describing the current word category, for example "Animals".
private  boolean correctGuess
          Indicates whether a player has made a correct guess.
private  PictionaryPlayer currentPlayer
          The current player; the person who is drawing, is about to draw or was drawing.
private  java.lang.String currentWord
          The current word being drawn.
 int drawTimeLimit
          The time, in seconds, a player can draw before his time is up.
static int GAME_STARTED
          The game has started, and is playing.
private  java.util.ArrayList<PictionaryPlayer> newPlayers
          The collection holding all the players waiting to join.
static int NOT_ENOUGH_PLAYERS_TO_CONTINUE
          The game cannot continue because someone left in the middle of it.
static int NOT_ENOUGH_PLAYERS_TO_START
          There needs to be more players before a new game can start.
static int pauseBetweenActions
          The standard pause in seconds.
private  java.util.List<PictionaryPlayer> pictionaryPlayers
          The collection of players currently playing (drawing or guessing).
private  java.util.Random randomGenerator
          Used for getting a new random word.
static int ROUND_FINISHED
          All players have drawn, a new round is about to start.
private  int skipEndDrawTasks
          Used for avoiding the canceling of an extra draw round from the timer; sometimes a draw round is canceled prematurely by a correct word guess from a player, then we increment this counter.
private  int status
          The game's current status.
private  java.util.Timer timer
          The timer used to create pauses between turns and rounds.
static int TURN_END
          Someone recently finished drawing, the turn has ended.
private  int turnsPlayed
          How many turns have been played.
private  java.util.ArrayList<java.lang.String> unusedWords
          The list of unused words.
private  java.util.ArrayList<java.lang.String> words
          The list of words used in this Pictionary game.
 
Constructor Summary
PictionaryGame(java.lang.String category, java.util.ArrayList<java.lang.String> words, int drawTimeLimit)
          The constructor.
 
Method Summary
 java.lang.String getCategory()
          Gets the current category.
 java.util.List<java.lang.String> getPlayerNames()
          Gets a collection of all the player names.
 int getStatus()
          Gets the current game status.
 void guessWord(PictionaryPlayer guessingPlayer, java.lang.String word)
          This method is called when a player wants to make a guess.
 void join(PictionaryPlayer player)
          This method is called by the hosting server when it gets a join request from a client.
 void nextAction()
          Advances the game to a new state depending on the game's current state.
private  void nextRandomWord()
          Sets the current word to a new random word and removes that word from unusedWords.
 boolean playerExists(PictionaryPlayer player)
          This method is used to check whether a player is in the game or not.
 void playerLeft(PictionaryPlayer player)
          Called by the hosting server when it gets a disconnect request, calling this method informs the game that a player wants to leave.
 void resetPlayers()
          Resets all players' scores and sets their status to PictionaryPlayer.WAITING_FOR_TURN.
private  void roundFinished()
          Sets status to ROUND_FINISHED and reports the scores to all the connected players.
private  void scheduleEndDrawTask()
          Schedules the next game state update if the conditions within PictionaryGame.EndDrawTask.run() are met, the method will be called after drawTimeLimit seconds.
private  void scheduleNextTask(int seconds)
          Schedules the next game state update, the method will be called after the specified number of seconds.
private  void startNewGame()
          Starts a new game of Pictionary: Resets the players' scores, lets waiting players join in, tells everyone that the game has started, calls nextAction() etc.
private  void tellPlayers(int event)
          Alerts the players that an event has occurred.
private  void tellPlayers(int event, PictionaryPlayer exclude)
          Alerts all players, except the excluded one, that an event has occurred.
private  void turnFinished()
          Sets the status to TURN_END and lets the next player draw.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOT_ENOUGH_PLAYERS_TO_START

public static final int NOT_ENOUGH_PLAYERS_TO_START
There needs to be more players before a new game can start.

See Also:
Constant Field Values

GAME_STARTED

public static final int GAME_STARTED
The game has started, and is playing.

See Also:
Constant Field Values

NOT_ENOUGH_PLAYERS_TO_CONTINUE

public static final int NOT_ENOUGH_PLAYERS_TO_CONTINUE
The game cannot continue because someone left in the middle of it.

See Also:
Constant Field Values

TURN_END

public static final int TURN_END
Someone recently finished drawing, the turn has ended.

See Also:
Constant Field Values

ROUND_FINISHED

public static final int ROUND_FINISHED
All players have drawn, a new round is about to start.

See Also:
Constant Field Values

pauseBetweenActions

public static final int pauseBetweenActions
The standard pause in seconds. Used when scheduling the next action using a timer.

See Also:
Constant Field Values

drawTimeLimit

public final int drawTimeLimit
The time, in seconds, a player can draw before his time is up.


status

private int status
The game's current status.
Equals one of the static state descriptions, for example GAME_STARTED


pictionaryPlayers

private java.util.List<PictionaryPlayer> pictionaryPlayers
The collection of players currently playing (drawing or guessing).


currentPlayer

private PictionaryPlayer currentPlayer
The current player; the person who is drawing, is about to draw or was drawing.


category

private java.lang.String category
Some text describing the current word category, for example "Animals".


words

private java.util.ArrayList<java.lang.String> words
The list of words used in this Pictionary game.


unusedWords

private java.util.ArrayList<java.lang.String> unusedWords
The list of unused words.


currentWord

private java.lang.String currentWord
The current word being drawn.


newPlayers

private java.util.ArrayList<PictionaryPlayer> newPlayers
The collection holding all the players waiting to join.


randomGenerator

private java.util.Random randomGenerator
Used for getting a new random word.


turnsPlayed

private int turnsPlayed
How many turns have been played.


timer

private java.util.Timer timer
The timer used to create pauses between turns and rounds.


skipEndDrawTasks

private volatile int skipEndDrawTasks
Used for avoiding the canceling of an extra draw round from the timer; sometimes a draw round is canceled prematurely by a correct word guess from a player, then we increment this counter.


correctGuess

private boolean correctGuess
Indicates whether a player has made a correct guess.

Constructor Detail

PictionaryGame

public PictionaryGame(java.lang.String category,
                      java.util.ArrayList<java.lang.String> words,
                      int drawTimeLimit)
The constructor.

Parameters:
category - Some text describing the word category, for example "Animals".
words - The words that players will attempt to draw and guess.
drawTimeLimit - How many seconds a player can draw before the time is up and the turn ends.
Method Detail

getStatus

public int getStatus()
Gets the current game status.

Returns:
A number equal to one of the static state descriptors in this class, for example GAME_STARTED.

startNewGame

private void startNewGame()
Starts a new game of Pictionary: Resets the players' scores, lets waiting players join in, tells everyone that the game has started, calls nextAction() etc.


tellPlayers

private void tellPlayers(int event)
Alerts the players that an event has occurred.


tellPlayers

private void tellPlayers(int event,
                         PictionaryPlayer exclude)
Alerts all players, except the excluded one, that an event has occurred.


getCategory

public java.lang.String getCategory()
Gets the current category.

Returns:
A string describing the current category, for example "Animals".

resetPlayers

public void resetPlayers()
Resets all players' scores and sets their status to PictionaryPlayer.WAITING_FOR_TURN.


nextRandomWord

private void nextRandomWord()
Sets the current word to a new random word and removes that word from unusedWords.


nextAction

public void nextAction()
Advances the game to a new state depending on the game's current state.
Gets the next random word when a new turn is about to start, tells each player when its their turn to start or stop drawing/guessing and informs them of different events that occurs - for example when a player makes a correct guess - and schedules timers that trigger the next action when the time is up.


scheduleNextTask

private void scheduleNextTask(int seconds)
Schedules the next game state update, the method will be called after the specified number of seconds.


scheduleEndDrawTask

private void scheduleEndDrawTask()
Schedules the next game state update if the conditions within PictionaryGame.EndDrawTask.run() are met, the method will be called after drawTimeLimit seconds.


turnFinished

private void turnFinished()
Sets the status to TURN_END and lets the next player draw.


roundFinished

private void roundFinished()
Sets status to ROUND_FINISHED and reports the scores to all the connected players.


join

public void join(PictionaryPlayer player)
This method is called by the hosting server when it gets a join request from a client.
It either lets the new player start playing at once or makes her wait until the current round has finished, depending on the game's state.

Parameters:
player - The player wanting to join.

playerLeft

public void playerLeft(PictionaryPlayer player)
Called by the hosting server when it gets a disconnect request, calling this method informs the game that a player wants to leave.
The game then removes the player, takes appropriate action to ensure that the game continues and informs all the players that someone left the game.

Parameters:
player - The player to be removed from the game.

playerExists

public boolean playerExists(PictionaryPlayer player)
This method is used to check whether a player is in the game or not.

Parameters:
player - The player to look for.
Returns:
True if the player is found, else false.

guessWord

public void guessWord(PictionaryPlayer guessingPlayer,
                      java.lang.String word)
This method is called when a player wants to make a guess.

Parameters:
guessingPlayer - The guessing player.
word - The word that the player guesses.

getPlayerNames

public java.util.List<java.lang.String> getPlayerNames()
Gets a collection of all the player names.

Returns:
A List of String containing the names of all the players.