For this lab you will be writing a networked version of the classic game "Connect Four" that will allow two players to play over a network. Connect Four is similar to Tic-Tac-Toe in that players alternate taking turns filling a two-dimensional board with pieces. Each player's goal is to create a sequence of at least 4 of their own pieces in a horizontal, vertical, or diagonal line. Players must also sometimes make moves to block their opponents from creating a sequence.
The major differences between Connect Four and Tic-Tac-Toe are the size of the board (7x6) and that players are limited to "dropping" their pieces (usually checkers) into a column on the board rather than playing in any open spot. The piece falls until it reaches the bottom of the column, or lands on another piece.
Your solution will ultimately involve two main classes:
In order for the game to work, you will need to implement a protocol: a simple language with which the client and server can communicate. You will be using TCP/IP sockets to send information back and forth between the server and the client.
The protocol is described here. All requests are sent from the server to the client. All responses are sent from the client to the server in response to a request. The client never initiates communication with the server (apart from establishing the initial socket connection). All messages between the server and client are a single line of text terminated with a newline ("\n").
|Request (from server to client)||Response (from client to server)||Description|
|CONNECT||No response||Called as a "handshake" between the server and the client immediately after the client establishes a socket connection with the server.|
Called when it is the client's turn to make a move. The client
responds with the number of the column in which the player would
like to move. The client should not update its game state until
it receives verification from the server that the move was made
At this point the client should prompt the real player to enter a move.
|MOVE_MADE #||No response||
Called when the client should update its game state to reflect that
a move has been made including the column number in which the move
was made. A MOVE_MADE request from the server is the only
time that a client should update the local game state.
At this point the column in which the move was made and the current game board should be displayed to the real player.
|GAME_WON||No response||Called when the client has won the game. This ends the game.|
|GAME_LOST||No response||Called when the other player has won the game and the client has lost. This ends the game.|
|GAME_TIED||No response||Called when the game was tied. This ends the game.|
|ERROR||No response||Called when either client has caused an error on the server (e.g. an interrupted connection or an invalid response to a request). No response is expected from the client. This ends the game (the connection is terminated).|
A Connect Four game should work as follows:
Your Java program should have two classes with executable main method.
The user should only ever have to input column numbers for moves. No other user input is required.
The specifics of the output are up to you, but you should follow the guidelines below:
[ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][O][ ][ ] [ ][ ][ ][ ][X][ ][ ] [ ][X][ ][ ][O][ ][ ] [O][O][ ][ ][X][O][ ] [X][X][O][X][O][X][X]
Example Output (player one).
If you did not get the GitHub link to the starter code from your instructor please contact them immediately. Your project structure must minimally look like the following:
Do not change the following classes:
The main program for your client is in connectfour.client.ConnectFourClient. It takes the hostname and port as arguments.
The main program for your server is in connectfour.server.ConnectFourServer. It takes the port as an argument.
You will complete this portion of the lab on your own. Unlike previous labs we are not providing you with a UML diagram or Javadocs. To receive full credit you should follow these design suggestions:
You should your src directory into a file named lab7.zip. and submit it to the dropbox on MyCourses by the due date.