Comparing NetFourByFour and FourByFour
Many classes in NetFourByFour
are similar to those in FourByFour
; this is a consequence of keeping the game logic on the client side.
The Positions
class, which manages the on-screen markers is unchanged from FourByFour
. PickDragBehavior
still handles user picking and dragging, but reports a selected position to NetFourByFour
rather than to Board
. The game data structures in Board
are as before, but the tryMove()
method for processing a move and the reportWinner()
are different. WrapNetFourByFour
is similar to WrapFourByFour
but utilizes the OverlayCanvas
class rather than Canvas3D
.
The NetFourByFour
class is changed since the networking code for the client side is located there. FBFWatcher
is used to monitor messages coming from the server, so it is new.
Game Initialization in the Client
The network initialization done in NetFourByFour
consists of opening a connection to the server and creating a FBFWatcher
thread to wait for a response:
// globals in NetFourByFour
private Socket sock;
private PrintWriter out;
private void makeContact() // in NetFourByFour
{
try {
sock = new Socket(HOST, PORT);
BufferedReader in = new BufferedReader(
new InputStreamReader( sock.getInputStream() ));
out = new PrintWriter( sock.getOutputStream(), true );
new FBFWatcher(this, in).start(); // start watching server
}
catch(Exception e)
{ System.out.println("Cannot contact the NetFourByFour Server");
System.exit(0);
}
} // end of makeContact()
A consideration of Figure ...
Get Killer Game Programming in Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.