Network Tick-Tack-Toe
Assigned homework 24 January 2003.
Goal
Write a tick-tack-toe program that can be played across the
network, using the client-server paradigm, in Java (C/C++
also permitted).
Step-by-step
- The Chat
You have been given examples of Java network programming which
should allow you to create a client-server chat program: two
applications which allow you to type back and forth across the
the network. (See Java by Example handout.)
Your first step is to get these working.
- The Protocol
We need to define the messages which will be sent between client
and server. This is a very important step, and we need to be very
accurate. There are four messages.
- The first message and the first message sent upon client-server
connection is a banner from the server to the client.
Something like TTT V0.1 is sufficient.
- Their are three other message types. They are identified by
the first letter of the message:
N, B, or Q.
- The N
and Q messages are just that single
letter. The B
message is of the form B*********
where the nine stars are replaced by either
X, O, or -.
- The client drives the conversation. Each message from the server,
except the banner, is a response to a message from the client. This
simplifies coding. From the client's point of view, the loop is:
write, read, think, write, read, think, and so on.
- Except for the banner, the server reacts to client's messages and
must respond to each message. This simplifies coding. From the server's
point of view, the loop is: read, think, write, read, think, write,
and so on.
- The B message: The board update message is
the most important message. It describes the current board. It is sent
either from server to client or client to server. Example: suppose X is in
the upper left and lower right squares and O is in the middle square.
The message sent will be BX---O---X.
Note well: It is not the responsibility of the protocol that
the game is played in any way correctly. It is only cares that these
messages are syntacticly correct. This protocol is our implementation
of the OSI Session and Presentation layers. The 7-th layer will insure
that new and old board positions agree with the laws of play.
- The Q message:
The quit message ends the game and the communication. Parties
must try not to simply hang up on each other, but should use
a quit message to exit together. Either client or server can
begin the quit process by sending a Q.
However, if the client sends, the server must respond with a
Q, and then exit. If the
server sends first, the client does not respond, but both
client and server exit. This is because the client expects
a response for every message it sends.
Neither client nor server can refuse a quit.
- When the client exits, it exits the client application program.
The server however only exits the connection and it goes back
to the listening socket for the next connection.
- The N message:
The new game message is somewhat optional. It is probably
most needed when the board update message received shows
that the game has been won.
-
Suppose the client makes the winning move. Its
B
message requires a response. The server responds
N
offering a new game. The client can return a
B
message with its first move, or an
N
message offering first move back to the server, or an
Q
to request the server to terminate the connection.
-
Suppose the server makes the winning move. Its
B
message does not require a response, but protocol is
that if the client wants to quit it should send an
Q
message and wait for an
Q
response. Or it can send a
B
message with its first move of a new game, or an
N
message offering first move to the server.
-
How the choice is made between the above options is not
the responsibility of the protocol. The application software
can be simple or sophisticated, negotiating a new first
player or not.
Get this protocol working after the chat is working. It
consists of loops on the server and client code which interpret
the messages and make reasonable responses. In particular, don't
worry too much about correct game boards, rather that the rules
of message interchange are followed.
- Playing the Game
Now add the application layer software which presents the
board to the user pictorially, offers opportunities to move,
resign or quit. Also the application layer can check new and
old boards for correct play and check if the game has been
won. If the game is won it can prompt the user whether it
wants a new game, whether it wants to go first, or whether it
would rather quit.
Do this layer last. It is the least tricky and you can spend
endless hours making the interface
handsome, friendly and flexible.