|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectpictionary.PictionaryGame
public class PictionaryGame
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.
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 |
---|
public static final int NOT_ENOUGH_PLAYERS_TO_START
public static final int GAME_STARTED
public static final int NOT_ENOUGH_PLAYERS_TO_CONTINUE
public static final int TURN_END
public static final int ROUND_FINISHED
public static final int pauseBetweenActions
public final int drawTimeLimit
private int status
GAME_STARTED
private java.util.List<PictionaryPlayer> pictionaryPlayers
private PictionaryPlayer currentPlayer
private java.lang.String category
private java.util.ArrayList<java.lang.String> words
private java.util.ArrayList<java.lang.String> unusedWords
private java.lang.String currentWord
private java.util.ArrayList<PictionaryPlayer> newPlayers
private java.util.Random randomGenerator
private int turnsPlayed
private java.util.Timer timer
private volatile int skipEndDrawTasks
private boolean correctGuess
Constructor Detail |
---|
public PictionaryGame(java.lang.String category, java.util.ArrayList<java.lang.String> words, int drawTimeLimit)
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 |
---|
public int getStatus()
GAME_STARTED
.private void startNewGame()
nextAction()
etc.
private void tellPlayers(int event)
private void tellPlayers(int event, PictionaryPlayer exclude)
public java.lang.String getCategory()
public void resetPlayers()
PictionaryPlayer.WAITING_FOR_TURN
.
private void nextRandomWord()
public void nextAction()
private void scheduleNextTask(int seconds)
private void scheduleEndDrawTask()
PictionaryGame.EndDrawTask.run()
are met, the method will be called after
drawTimeLimit seconds.
private void turnFinished()
TURN_END
and lets the next
player draw.
private void roundFinished()
ROUND_FINISHED
and reports the
scores to all the connected players.
public void join(PictionaryPlayer player)
player
- The player wanting to join.public void playerLeft(PictionaryPlayer player)
player
- The player to be removed from the game.public boolean playerExists(PictionaryPlayer player)
player
- The player to look for.
public void guessWord(PictionaryPlayer guessingPlayer, java.lang.String word)
guessingPlayer
- The guessing player.word
- The word that the player guesses.public java.util.List<java.lang.String> getPlayerNames()
List
of String containing the names of all
the players.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |