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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access