Class NetworkPlayer

java.lang.Object
  extended by Player
      extended by NetworkPlayer

public class NetworkPlayer
extends Player

This class inerits from player. It is involved in the network game. This identifies that a network player is the second player.

Author:

Field Summary
 
Fields inherited from class Player
LOCALPLAYER
 
Constructor Summary
NetworkPlayer(int playerNum, Rules rules, Driver theDriver)
          Constructor that creates a default object of this class
 
Method Summary
 void acceptDraw(Player player)
          When the current player accepts a draw, this method is called in the opposite player to inform them that the draw has been accepted.
 void cleanup()
          Closes the streams & sockets
 void connectToHost()
          This method establishes a connection to the host
 void endInDeclineDraw(Player player)
          Method is invoked if the other player declines a draw.
 void endInDraw(Player player)
          Method that is invoked when the end of game conditions have been met.
 void endOfGame(java.lang.String endMessage)
          Method that is invoked when the end of game conditions have been met.
 int getPlayerType()
          Return the integer constant representing the networkPlayer class
 void offerDraw(Player player)
          A DRAWOFFER message is sent to the remote player.
 java.lang.Integer processCommand(int command)
          Process an incoming command, and take the appropriate action.
 void sendColor()
          This method sends the color of this player to the other computer; will only be executed by the host computer.
 void sendCommand(int type)
          This method sends some command over network to the local system
 void sendMove()
          The move is sent to the remote player
 void sendName()
          This method sends the name of this player to the other computer
 void setHost(java.net.URL host)
          Set the host that we'll connect to if we're a remote system
 void takeColor()
          This method gets the color of this player from the other computer; this method will only be executed by the client computer.
 void takeName()
          This method takes the name of the other player and stores it
 void waitForConnect()
          The host waits for the network player to connect.
 void waitForPlayer()
          Wait for the other player to send us a move or command.
 
Methods inherited from class Player
getColor, getName, getNumber, getType, makeMove, setColor, setName, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NetworkPlayer

public NetworkPlayer(int playerNum,
                     Rules rules,
                     Driver theDriver)
Constructor that creates a default object of this class

Method Detail

setHost

public void setHost(java.net.URL host)
Set the host that we'll connect to if we're a remote system


waitForConnect

public void waitForConnect()
The host waits for the network player to connect. If there is a timeout, we generate an actionEvent telling the GUI.


connectToHost

public void connectToHost()
This method establishes a connection to the host

Parameters:
host - - the host to connect to

takeName

public void takeName()
This method takes the name of the other player and stores it


sendName

public void sendName()
This method sends the name of this player to the other computer


takeColor

public void takeColor()
This method gets the color of this player from the other computer; this method will only be executed by the client computer.


sendColor

public void sendColor()
This method sends the color of this player to the other computer; will only be executed by the host computer.


waitForPlayer

public void waitForPlayer()
Wait for the other player to send us a move or command. If there is a timeout, we generate an actionEvent telling the GUI. We must then send the move along to Rules, or call processCommand.


processCommand

public java.lang.Integer processCommand(int command)
Process an incoming command, and take the appropriate action.


sendCommand

public void sendCommand(int type)
This method sends some command over network to the local system

Parameters:
type - - the type of command that is an integer value

sendMove

public void sendMove()
The move is sent to the remote player

Parameters:
move - - move that was made by the local player

offerDraw

public void offerDraw(Player player)
A DRAWOFFER message is sent to the remote player. On this end, we will wait for a reply and generate an appropriate actionEvent based on it.

Specified by:
offerDraw in class Player

acceptDraw

public void acceptDraw(Player player)
When the current player accepts a draw, this method is called in the opposite player to inform them that the draw has been accepted. We're a networkPlayer, so we send the ACCEPTDRAW command over to the remote player and waitForPlayer.

Specified by:
acceptDraw in class Player

endOfGame

public void endOfGame(java.lang.String endMessage)
Method that is invoked when the end of game conditions have been met. Send the ENDOFGAME command over to the remote player and then send the message endMessage over.

Specified by:
endOfGame in class Player
Parameters:
endMessage - Message indicating the end of the game.

getPlayerType

public int getPlayerType()
Return the integer constant representing the networkPlayer class


cleanup

public void cleanup()
Closes the streams & sockets


endInDeclineDraw

public void endInDeclineDraw(Player player)
Description copied from class: Player
Method is invoked if the other player declines a draw. It displays the dialog box for the decline of draw

Specified by:
endInDeclineDraw in class Player

endInDraw

public void endInDraw(Player player)
Description copied from class: Player
Method that is invoked when the end of game conditions have been met. If they have been, this method is called in both players to notify them of this with a message. Implementation differs for local player and network player.

Specified by:
endInDraw in class Player