Class Game

java.lang.Object
  |
  +--Game

public class Game
extends java.lang.Object

The Game Class is the central core of the the Sokoban game. The Game class is a link between the user interface, the networking and the map.


Field Summary
protected  SGui gui
          The link to the Sokoban user interface.
 
Constructor Summary
Game(SGui sokobanGUI)
          Constructor - initializes the game class
 
Method Summary
 void changeLevel(java.lang.String level)
          Processes a request to change the current level being played.
protected  void checkChangeLevelVotes(java.lang.String level)
          Checks to see if the change level vote has completed.
protected  void checkRestartVotes()
          Checks to see if the restart level vote has completed.
 void connectionClosed()
          Informs the multiplayer dialog to display a connection closed dialog message.
 void doChangeLevel(java.lang.String level)
          Tells the map class to load the level passed.
 void doRestartMap()
          Perform the restart map operation.
 void endGame()
          Closes any connection that is present and clears the map.
 java.lang.String getCurrentLevel()
          Returns the name of the current level being played
 java.lang.String getFirstLevel()
          Returns the name of the first level in the level list.
 java.lang.String[] getLevelList()
          Returns a String array of the names of the levels in the level list.
 Robot getLocalRobot()
          Returns the local Robot
 Square[][] getMap()
          Returns the current Map.
 java.lang.String[] getMapString()
          Returns the current Map.
 java.lang.String getNextLevel()
          Returns the name of the next level.
 java.lang.String getPrevLevel()
          Returns the name of the previous level.
 Robot[] getRobots()
          Returns the robots currently in the map
static java.lang.String[] getSaveDescriptions()
          Returns a String array containing the save game descriptions.
 int getTime()
          Returns the cumulative time spent on solving the current map (in seconds).
 int getTotalScore()
          Returns the current totalScore
 java.lang.String getUserName()
          Returns the current username
 void instantReplay()
          Sends instantReplay message to the Map class
Precondition: The game is in single player mode.
 boolean isActive()
          Returns whether the game is active or not.
 boolean isClientConnected()
          Indicates whether or not the client is connected to a multiplayer game.
 boolean isHosting()
          Indicates whether or not the client is connected to a multiplayer game.
 boolean isMultiplayer()
          Indicates whether or not the game is a multiplayer game.
 boolean isPaused()
          Returns true if the map is currently paused, false otherwise.
 void levelCompleted()
          This function tells the system that level has been completed and to therefore proceed to next level.
 void loadGame(int loadSlot)
          This function allows the user to load a game from a previously saved game
Precondition:none
Postcondition:The previously played game is loaded and the map is set to the saved state
 void pauseTimer()
          Sends pauseTimer message to the Map class
Precondition: The game is not multiplayer
Postcondition: The timer is paused.
 void postAcceptedChangeLevel(java.lang.String level, int robotNum, boolean accepted)
          Sets a robot's change level flag to the accepted boolean passed.
 void postAcceptedRestart(int robotNum, boolean accepted)
          Sets a robot's restart level flag to the accepted boolean passed.
 void postChat(java.lang.String user, java.lang.String msg)
          This function displays the chat messages sent by the users.
 void postDisconnect(int robotNum)
          Informs other users connected to a server of a client disconnecting and removes the client's robot from the map
 void postMove(int robotNum, Direction dir)
          This function posts the move made by the user on the map.
 void postNewUser(java.lang.String userName, int robotNum)
          Adds a user to the current game.
 void requestChangeLevel(java.lang.String levelName, int nRobot)
          Allows the user to agree or disagree to a change level request.
 void requestRestart(int nRobot)
          Allows the user to agree or disagree to a restart level request.
 void restartMap()
          Sends restartMap message to the Map class
Precondition: None.
 void saveGame(int saveSlot, java.lang.String description)
          This function allows the user to save his progress in the game
Precondition:User is engaged in a game
Postcondition:The game is saved
 void setDebugMode(boolean mode)
          Sets whether the getTime() method reports time as always 1 or if it returns the correct value.
 void setMapObserver(java.util.Observer obs)
          Adds an observer to the current map
 void setUserName(java.lang.String name)
          Sets the current userName to the passed name
 void startClient(java.lang.String ip, int port, MultiplayerDialog multiDiag)
          Initilizes the client to connect to the server.
 void startMap(java.lang.String levelName)
          This function initializes the map, adds the robot(s) to the map and then loads the level to be played.
 void startServer(int port, MultiplayerDialog multiDiag)
          Tells the Server to begin listening for connections.
 void startTimer()
          Sends startTimer message to the Map class
Precondition: The game is not multiplayer
Postcondition: The timer is started.
 void stopMultiplayer()
          Disconnects the client and sends the disconnect message if this game is a client or disconnects all clients connected to the server if this game is a server.
 void undoMove(int robotNum)
          Sends undoMove message to the Map class.
 void updateDisplay()
          Informs the GUI to update the window
preconditons: none.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

gui

protected SGui gui
The link to the Sokoban user interface.
Constructor Detail

Game

public Game(SGui sokobanGUI)
Constructor - initializes the game class
Method Detail

levelCompleted

public void levelCompleted()
This function tells the system that level has been completed and to therefore proceed to next level.
Precondition:All conditions for completing a certain level have been met
Postcondition:The system proceeds to the next level to be played by the user

saveGame

public void saveGame(int saveSlot,
                     java.lang.String description)
This function allows the user to save his progress in the game
Precondition:User is engaged in a game
Postcondition:The game is saved
Parameters:
saveSlot - the saved game slot to save to
description - the description entered by the user describing the current saved game.

loadGame

public void loadGame(int loadSlot)
This function allows the user to load a game from a previously saved game
Precondition:none
Postcondition:The previously played game is loaded and the map is set to the saved state
Parameters:
loadSlot - the saved game slot to be loaded from

startMap

public void startMap(java.lang.String levelName)
This function initializes the map, adds the robot(s) to the map and then loads the level to be played.
Parameters:
levelName - the name of the initial level to play
Precondition:none
Postcondition:The level is loaded and the game is started

changeLevel

public void changeLevel(java.lang.String level)
Processes a request to change the current level being played.
Parameters:
level - the name of the level to change to
preconditions: the game is started
postconditions: the level passed is loaded if single player, or a request to change to the level passed is sent to the other users if multiplayer.

doChangeLevel

public void doChangeLevel(java.lang.String level)
Tells the map class to load the level passed.
Parameters:
level - String value associated with name of the level
Pre-conditions: none
Post-conditions: level is sent to changeLevel within the Map class

requestChangeLevel

public void requestChangeLevel(java.lang.String levelName,
                               int nRobot)
Allows the user to agree or disagree to a change level request. If hosting, the change request is sent to the other users
Parameters:
levelName - the name of the level to change to
nRobot - the robot number requesting the change
precondition: a multiplayer game is in progress
postcondition: the user has agreed or disagreed to the change level request

postAcceptedChangeLevel

public void postAcceptedChangeLevel(java.lang.String level,
                                    int robotNum,
                                    boolean accepted)
Sets a robot's change level flag to the accepted boolean passed.
Parameters:
level - the name of the level to change to
robotNum - the robotnum of the user accepting or declining
accepted - true if the user accepts, false if not
preconditions: a multiplayer game is in progress
postconditions: the user with robotNum has selected to accept or decline the request for a level change

checkChangeLevelVotes

protected void checkChangeLevelVotes(java.lang.String level)
Checks to see if the change level vote has completed.
Parameters:
level - the name of the level to change to
preconditions: a change level vote is in progress
postconditions: if the vote is complete and it was unanimous in favor then the level is changed. otherwise a vote failed message is sent to the chat

postChat

public void postChat(java.lang.String user,
                     java.lang.String msg)
This function displays the chat messages sent by the users.
Parameters:
user - The name of the user sending the message
msg - The message sent by user
Precondition:User is engaged in a multiplayer game
Postcondition:The message is displayed in the chat window for all users to read

postDisconnect

public void postDisconnect(int robotNum)
Informs other users connected to a server of a client disconnecting and removes the client's robot from the map
Parameters:
robotNum - The robot number of the user disconnecting
Precondition:User is engaged in a multiplayer game
Postcondition:The robot number of the user disconnecting is removed from the map.

postMove

public void postMove(int robotNum,
                     Direction dir)
This function posts the move made by the user on the map.
Parameters:
robotNum - The id of the robot being moved
dir - The direction in which the robot moved
Precondition:User is engaged in a multiplayer game
Postcondition:The direction in which the particular robot moved is displayed to all users

undoMove

public void undoMove(int robotNum)
Sends undoMove message to the Map class. Sends the message to the server if the user is hosting and to the client if the user is not.
Parameters:
robotNum - The id of the robot undoing a move
Precondition:User is engaged in a multiplayer game
Postcondition:Returns the map to the state it was in before the most recent move was executed.

postNewUser

public void postNewUser(java.lang.String userName,
                        int robotNum)
Adds a user to the current game.
Parameters:
userName - the name of the new user
robotNum - the robot number of the new user precondition: there is a multiplayer game running postcondition: the new user has been added to the game

getMapString

public java.lang.String[] getMapString()

Returns the current Map.

Returns:
an array of Strings representing the current map

getMap

public Square[][] getMap()

Returns the current Map.

Returns:
a 2-dimensional array of Squares representing the current map

getRobots

public Robot[] getRobots()

Returns the robots currently in the map

Returns:
an array of Robots

pauseTimer

public void pauseTimer()
Sends pauseTimer message to the Map class
Precondition: The game is not multiplayer
Postcondition: The timer is paused.

startTimer

public void startTimer()
Sends startTimer message to the Map class
Precondition: The game is not multiplayer
Postcondition: The timer is started.

isPaused

public boolean isPaused()
Returns true if the map is currently paused, false otherwise.
Returns:
returns whether the map is paused or not

restartMap

public void restartMap()
Sends restartMap message to the Map class
Precondition: None.
Postcondition: The game has been restarted.

doRestartMap

public void doRestartMap()
Perform the restart map operation. preconditions: a map exists and a game is in progress postconditions: the map has been restarted

requestRestart

public void requestRestart(int nRobot)
Allows the user to agree or disagree to a restart level request. If hosting, the restart request is sent to the other users
Parameters:
nRobot - the robot number requesting the change
precondition: a multiplayer game is in progress
postcondition: the user has agreed or disagreed to the restart level request

postAcceptedRestart

public void postAcceptedRestart(int robotNum,
                                boolean accepted)
Sets a robot's restart level flag to the accepted boolean passed.
Parameters:
robotNum - the robotnum of the user accepting or declining
accepted - true if the user accepts, false if not
preconditions: a multiplayer game is in progress
postconditions: the user with robotNum has selected to accept or decline the request for a level restart

checkRestartVotes

protected void checkRestartVotes()
Checks to see if the restart level vote has completed.
preconditions: a restart level vote is in progress
postconditions: if the vote is complete and it was unanimous in favor then the level is restarted. otherwise a vote failed message is sent to the chat

instantReplay

public void instantReplay()
Sends instantReplay message to the Map class
Precondition: The game is in single player mode.
Postcondition: The map returns to the state it was in before the instant replay.

startServer

public void startServer(int port,
                        MultiplayerDialog multiDiag)
Tells the Server to begin listening for connections.
Precondition: None.
Postcondition: The server is listening for connections.

stopMultiplayer

public void stopMultiplayer()
Disconnects the client and sends the disconnect message if this game is a client or disconnects all clients connected to the server if this game is a server.
Precondition: Multiplayer is set to true.
Postcondition: The game is disconnected from all others.

startClient

public void startClient(java.lang.String ip,
                        int port,
                        MultiplayerDialog multiDiag)
Initilizes the client to connect to the server.
Parameters:
ip - The IP address to connect to.
port - The port to use in the connection.
Precondition: The game is not running as a host
Postcondition: The client is running and the game is set to be a multiplayer game.

isMultiplayer

public boolean isMultiplayer()
Indicates whether or not the game is a multiplayer game.

isHosting

public boolean isHosting()
Indicates whether or not the client is connected to a multiplayer game.

isClientConnected

public boolean isClientConnected()
Indicates whether or not the client is connected to a multiplayer game.

getUserName

public java.lang.String getUserName()
Returns the current username
Returns:
userName The current name set as the user

setUserName

public void setUserName(java.lang.String name)
Sets the current userName to the passed name
Parameters:
name - the name of a user

getTotalScore

public int getTotalScore()
Returns the current totalScore
Returns:
totalScore The current cummulative score for the user

getLocalRobot

public Robot getLocalRobot()
Returns the local Robot
Returns:
Returns the Robot associated with the local machine

updateDisplay

public void updateDisplay()
Informs the GUI to update the window
preconditons: none.
postconditions: the ui display has been repainted

connectionClosed

public void connectionClosed()
Informs the multiplayer dialog to display a connection closed dialog message.
preconditions: multiplayer game is in progress.
postconditions: the connection closed dialog is displayed

isActive

public boolean isActive()
Returns whether the game is active or not.
Returns:
true if the game is active, otherwise false

endGame

public void endGame()
Closes any connection that is present and clears the map.
Precondition: The game is running.
Postcondition: The game is terminated.

getTime

public int getTime()
Returns the cumulative time spent on solving the current map (in seconds).
Returns:
Returns the amount of time currently spent solving the active map

setMapObserver

public void setMapObserver(java.util.Observer obs)
Adds an observer to the current map
Parameters:
obs - the Observer to be added to the map

getSaveDescriptions

public static java.lang.String[] getSaveDescriptions()
Returns a String array containing the save game descriptions.
Returns:
String[] continaing the game descriptions.

getLevelList

public java.lang.String[] getLevelList()
Returns a String array of the names of the levels in the level list.
Returns:
a string array with the names of the levels in the level list.

getNextLevel

public java.lang.String getNextLevel()
Returns the name of the next level.
Returns:
the name of the next level or null if the current level is the last level

getPrevLevel

public java.lang.String getPrevLevel()
Returns the name of the previous level.
Returns:
the name of the previous level or null if the current level is the first level

getFirstLevel

public java.lang.String getFirstLevel()
Returns the name of the first level in the level list.
Returns:
the name of the first level in the level list

getCurrentLevel

public java.lang.String getCurrentLevel()
Returns the name of the current level being played
Returns:
the name of the current level

setDebugMode

public void setDebugMode(boolean mode)
Sets whether the getTime() method reports time as always 1 or if it returns the correct value. debug mode makes time = 1.
Parameters:
mode - turns debug mode on or off