fourwin
Class BrickConnection

java.lang.Object
  extended by fourwin.BrickConnection

public class BrickConnection
extends Object

Establishes a connection with a remote computer using bluetooth or USB. The connection is used to send messages to the computer including moves and status data and to receive commands from the computer.

To connect a pc the waitForConnection() method has to be called. The robot waits then for the remote computer to initiate the connection. Waiting blocks the application until the connection is established.

Remark that bluetooth has to be turned on and the brick has to be visible for other bluetooth devices.

Integer values are used to transmit data. So a code is introduced to allocate an integer to its meaning. See contants defined in that class.

Author:
Seba

Field Summary
private  int CodeOffset
           
(package private)  NXTConnector connection
          Connection handle
private  int DUMMY
           
private  int GAME_START
           
(package private)  DataInputStream inStream
          Stream for receiving data
private  boolean isConnected
          Saves if a connection is already established
private  int KIlevel
           
private  int MOVE_PLAYER_ONE
           
private  int MOVE_PLAYER_TWO
           
private  int NEW_BEGINNER
           
private  int NEW_DIFFICULTY
           
(package private)  DataOutputStream outStream
          Stream for outputting data
private  int PCS_TURN
           
private  FourPlayer player
           
private  int RESET
           
private  boolean useBluetooth
          Determeines whether to use bluetooth.
 
Constructor Summary
BrickConnection(boolean useBluetooth)
          Returns an object which represents one connection to a remote PC.
 
Method Summary
 boolean Connect()
          Waits for the remote PC to initiate the connection.
 void Disconnect()
          Uses the leJOS close() methods to disconnect from 4RowBot.
 boolean isConnected()
           
 void processInput(PlayingField board, FourGUI gui)
          This method is used to receive messages from the brick.
 void SendDummy()
          Can be used to send a dummy message which is always ignored.
 void SendGameReset(FourGUI gui)
          Sends the game reset message to the brick.
 void SendGameStart()
          Is used to send the game-start message to the brick.
 void SendMove(byte column)
          Sends a move command to the Brick.
 void SendNewBeginner(int beginnerCode)
          Is used to notify the brick about a new beginner.
 void SendNewDifficulty(int newDifficulty)
          Is used to notify the brick about a new difficulty.
 void setAIlevel(int level)
          Is used to set the new AI level if the difficulty changes.
 void useBluetooth(boolean useBluetooth)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

useBluetooth

private boolean useBluetooth
Determeines whether to use bluetooth. If set to false USB is used.


isConnected

private boolean isConnected
Saves if a connection is already established


connection

NXTConnector connection
Connection handle


outStream

DataOutputStream outStream
Stream for outputting data


inStream

DataInputStream inStream
Stream for receiving data


CodeOffset

private final int CodeOffset
See Also:
Constant Field Values

RESET

private final int RESET
See Also:
Constant Field Values

MOVE_PLAYER_ONE

private final int MOVE_PLAYER_ONE
See Also:
Constant Field Values

MOVE_PLAYER_TWO

private final int MOVE_PLAYER_TWO
See Also:
Constant Field Values

GAME_START

private final int GAME_START
See Also:
Constant Field Values

NEW_DIFFICULTY

private final int NEW_DIFFICULTY
See Also:
Constant Field Values

NEW_BEGINNER

private final int NEW_BEGINNER
See Also:
Constant Field Values

PCS_TURN

private final int PCS_TURN
See Also:
Constant Field Values

DUMMY

private final int DUMMY
See Also:
Constant Field Values

KIlevel

private int KIlevel

player

private FourPlayer player
Constructor Detail

BrickConnection

public BrickConnection(boolean useBluetooth)
Returns an object which represents one connection to a remote PC.

Parameters:
useBluetooth - Determines whether to use bluetooth (true) or USB (false)
Method Detail

isConnected

public boolean isConnected()

useBluetooth

public void useBluetooth(boolean useBluetooth)
Parameters:
useBluetooth - Determines whether to use bluetooth (true) or USB (false)

Connect

public boolean Connect()
Waits for the remote PC to initiate the connection. The method doesn't return until the connection is established.

Returns:
True if the connection is established successfully

Disconnect

public void Disconnect()
Uses the leJOS close() methods to disconnect from 4RowBot.


SendMove

public void SendMove(byte column)
Sends a move command to the Brick. Should be used to answer a PCS_TURN message.

Parameters:
column - column in which 4RowBot should insert its chip.

SendGameReset

public void SendGameReset(FourGUI gui)
Sends the game reset message to the brick.

Parameters:
gui - GUI-object. Is used to call the repaint method after the playing field has been emptied.

SendDummy

public void SendDummy()
Can be used to send a dummy message which is always ignored.


SendGameStart

public void SendGameStart()
Is used to send the game-start message to the brick.


SendNewDifficulty

public void SendNewDifficulty(int newDifficulty)
Is used to notify the brick about a new difficulty. The numerical value for the new difficulty is attached to the message and must be provided as an argument.

Parameters:
newDifficulty - Numerical value for the new Difficulty

SendNewBeginner

public void SendNewBeginner(int beginnerCode)
Is used to notify the brick about a new beginner. The code for the new beginner is attached to the message and must be provided as an argument.

Parameters:
beginnerCode - Code of the new beginner.

processInput

public void processInput(PlayingField board,
                         FourGUI gui)
This method is used to receive messages from the brick. It should be called periodically. It needs the current gui object to be able to set new settings which are changed at the brick. to visualize a game, the PlayingField object which is connected to the gui must be provided as argument.

Parameters:
board - To the gui attached playing field object
gui - GUI-object, needed to change settings

setAIlevel

public void setAIlevel(int level)
Is used to set the new AI level if the difficulty changes. The new AI level is not used until a new round starts.

Parameters:
level - new level for the artificial intelligence