TTTO.Game
Class GameServer

java.lang.Object
  extended by TTTO.Game.GameServer

public class GameServer
extends java.lang.Object

Controls the logic of a TTTGame. Two clients must connect to the server.


Nested Class Summary
 class GameServer.ClientHandler
           
 
Field Summary
private  java.util.ArrayList<GameServer.ClientHandler> allPlayers
           
static java.lang.String DRAW
           
static java.lang.String FREE_SQUARE
           
static java.lang.String GAME_CONNECTED
           
private  java.lang.String[] gameBoard
           
private  java.lang.String gameName
           
private  MasterServerClient msClient
           
static java.lang.String NEW_GAME
           
static java.lang.String PLAYER_LOSES
           
static java.lang.String PLAYER_MOVE
           
static java.lang.String PLAYER_ONE
           
static java.lang.String PLAYER_TWO
           
static java.lang.String PLAYER_WINS
           
private  java.lang.String playerTurn
           
private  java.net.ServerSocket serverSocket
           
 
Constructor Summary
GameServer(java.net.ServerSocket aSocket, MasterServerClient aMasterServerClient, java.lang.String aGameName)
          Creates a GameServer wrapped around a ServerSocket
 
Method Summary
private  java.lang.String checkForWinner()
          Checks if someone won yet.
private  int countPlayerMoves()
          Counts how many player moves been made
private  boolean hasPlayerWon(java.lang.String aPlayerID)
          Checks if the given player wins
static boolean isValidCommand(java.lang.String aCommand)
          Compares the string to all commands and checks if the string is a command
static boolean isValidPlayerID(java.lang.String aPlayerID)
          Compares the string to all player IDs and check if the string is an ID
private  void newGame()
          Waits 2.5 seconds and inits a new game
private  void sendMessageToAllClients(java.lang.String aCommand, java.lang.String[] aData)
          Sends a message to all clients
private  void sendMessageToClient(java.lang.String aTargetClientID, java.lang.String aCommand, java.lang.String[] aData)
          Sends message to a specific client
 void startServer()
          Starts the server
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serverSocket

private java.net.ServerSocket serverSocket

allPlayers

private java.util.ArrayList<GameServer.ClientHandler> allPlayers

msClient

private MasterServerClient msClient

gameName

private java.lang.String gameName

gameBoard

private java.lang.String[] gameBoard

playerTurn

private java.lang.String playerTurn

PLAYER_ONE

public static final java.lang.String PLAYER_ONE
See Also:
Constant Field Values

PLAYER_TWO

public static final java.lang.String PLAYER_TWO
See Also:
Constant Field Values

FREE_SQUARE

public static final java.lang.String FREE_SQUARE
See Also:
Constant Field Values

GAME_CONNECTED

public static final java.lang.String GAME_CONNECTED
See Also:
Constant Field Values

NEW_GAME

public static final java.lang.String NEW_GAME
See Also:
Constant Field Values

PLAYER_MOVE

public static final java.lang.String PLAYER_MOVE
See Also:
Constant Field Values

PLAYER_WINS

public static final java.lang.String PLAYER_WINS
See Also:
Constant Field Values

PLAYER_LOSES

public static final java.lang.String PLAYER_LOSES
See Also:
Constant Field Values

DRAW

public static final java.lang.String DRAW
See Also:
Constant Field Values
Constructor Detail

GameServer

public GameServer(java.net.ServerSocket aSocket,
                  MasterServerClient aMasterServerClient,
                  java.lang.String aGameName)
Creates a GameServer wrapped around a ServerSocket

Parameters:
aSocket - the server socket.
aMasterServerClient - reference to the master server
aGameName - name of the game //TODO: Not used yet
Method Detail

isValidCommand

public static boolean isValidCommand(java.lang.String aCommand)
Compares the string to all commands and checks if the string is a command

Parameters:
aCommand - The string to check
Returns:
true if the string is a command

isValidPlayerID

public static boolean isValidPlayerID(java.lang.String aPlayerID)
Compares the string to all player IDs and check if the string is an ID

Parameters:
aPlayerID - the playerid
Returns:
true if its an ID, false if not

newGame

private void newGame()
Waits 2.5 seconds and inits a new game


startServer

public void startServer()
Starts the server


sendMessageToAllClients

private void sendMessageToAllClients(java.lang.String aCommand,
                                     java.lang.String[] aData)
Sends a message to all clients

Parameters:
aCommand - The GameServer command
aData - the data to send, can be null

sendMessageToClient

private void sendMessageToClient(java.lang.String aTargetClientID,
                                 java.lang.String aCommand,
                                 java.lang.String[] aData)
Sends message to a specific client

Parameters:
aTargetClientID - the ID of the client
aCommand - the GameServer command
aData - the data to send

countPlayerMoves

private int countPlayerMoves()
Counts how many player moves been made

Returns:
0 - 9

checkForWinner

private java.lang.String checkForWinner()
Checks if someone won yet.

Returns:
Null if no one won yet. Else PlayerID of the winner.

hasPlayerWon

private boolean hasPlayerWon(java.lang.String aPlayerID)
Checks if the given player wins

Parameters:
aPlayerID - id of the player to check
Returns:
true if player won, false if he havn't won yet