CSCI142: Computer Science 2
Lab 7 - Connect 4

Problem: Networked Connect Four

Connect Four

Introduction

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.
MAKE_MOVE MOVE # 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 (see below).

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:

  1. The first client connects by:
    1. Opening a socket connection to the server.
    2. The server sends a CONNECT request.
  2. The second client connects by:
    1. Opening a socket connection to the server.
    2. The server sends a CONNECT request.
  3. A game is started. Each round of play follows these basic steps:
    1. The server sends a MAKE_MOVE request to player one.
    2. The player responds with a MOVE response.
    3. The server makes the move on its copy of the game.
    4. The server sends a MOVE_MADE request to player one.
    5. The server sends a MOVE_MADE request to player two.
    6. The server checks to see if player one has one. If so:
      • The server sends a GAME_WON request to player one.
      • The server sends a GAME_LOST request to player two.
      • The server ends the game and closes the connections.
    7. If player one has not won the game, the round continues in the same way for player two.
  4. If at any point either client causes an error (e.g. connection interrupted or an invalid response to a request) an ERROR request is sent to both players and the game is ended.

Input Specification

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.

Output Specification

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).

Starter Code

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:

Project Structure

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.

Program Requirements

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:


Grading

The grade breakdown for this lab is as follows:

Submission

You should your src directory into a file named lab7.zip. and submit it to the dropbox on MyCourses by the due date.