CMPS109 Programming Assignment 5

I have not had time to debug this protocol. I would not be at all surprised if there are critical gaps or errors. I would not expect any substantial changes. You should expect some minor corrects until 1 week before the due date. (Last updated 2-26-04).

Objective

The objective of this assignment is to become familiar with distributed programming using Java’s threads and socket API.

Gomoku

 

For this assignment you will implement a two person game, Gomoku, to be played by two people on two computers

Gomoku , also written Go-Moku is a Japanese board game traditionally played with Go pieces and board (black and white stones on a 19x19 line board). The stones are placed on the intersections of the board, with Black playing first. The winner is the first player to get an unbroken row of five stones, either orthogonally or diagonally.” (Taken directly from http://en.wikipedia.org/wiki/Gomoku which you can consult for more details.)

Your program should follow just the basic rules (called free-style in the referenced link above).

 

Gomoku Server

 

Your submission will have two parts, a server (GomokuServer.java) for matching clients and the client program (Gomoku.java) that actually plays the game. The server will listen on a specified port. When a client connects to the server, if there are no other clients waiting, this client is set aside until a second client connects. We will call the first client to arrive Player1. When a second client arrives the server will respond to each of the clients. All message exchanges are in the form of white space delimited strings. For example, in step 3 of the server protocol below, the integers could be on one line separated by spaces or tabs, or they could be on separate lines (separated by the newline character). However, a complete message will always be terminated by at least one newline (so that methods such as BufferedReader.readline() can be used if desired).

 

The server protocol:

  1. Wait for the first player connection. The default port should be 5555.
  2. Send the response “server” to the first client (Player1).
  3. Receive two integers (in ascii) from Player1. The first is the port number that Player2 will use to contact Player1. The second is a random number that will be used by Player2 to identify them as the legitimate Player2.
  4. Wait for the second player connection.
  5. Send the response “client” to Player2.
  6. Send the message “player1IP portNumber randomNumber” to Player2. Player1IP is the ip address of Player1. The portNumber and randomNumber are the integers sent by Player1.

 

The player protocol with the server:

  1. Connect to the server.
  2. Send the message “gomoku”.
  3. If the response is “client” then read the Player1 IP address, port number, and game id (random number from Player1).
    If the response is instead “server” then
    1. Begin listening on some port PortNumber.
    2. Send PortNumber and a random number to the server.

 

Gomoku client

 

The client is very similar to the program for program4, once the initial handshake with the server is completed.

 

The Player1 protocol:

  1. Receive an integer that must match the random number that was sent to the server. If the correct number is sent, reply with “ready”, otherwise close the connection and restart the connect to server protocol.
  2. Receive the board size (on a line by itself).
  3. Receive a row and column number. This is the first move (Player 2 is black).
  4. Record the move locally.
  5. Make a move for white and send the row and column for the move to Player2.
  6. Repeat steps 3-5 until a win is detected. If Player2 makes a winning move respond with “you win\n”. If Player1 makes a winning move expect “you win\n” instead of a row column from Player2.
  7. If Player1 sent “you win” then the next message from Player2 should be either “continue\n” or “end\n” with the obvious meanings of continue with a new game (go to step 3) or end the match.
    If Player1 received “you win” then send either “continue\n” or “end\n”. If continuing, go to step 3 of the Player2 protocol (the loser gets to go first on the next game).

 

The Player2 protocol:

  1. Send the game id (random number from server) to Player1, then receive “ready” from Player1.
  2. Send the (user selected) board size (on a line by itself).
  3. Send a row and column number. This is the first move (Player 2 is black).
  4. Record the move locally.
  5. Receive a move for white and record it locally.
  6. Repeat steps 3-5 until a win is detected. If Player1 makes a winning move respond with “you win\n”. If Player2 makes a winning move expect “you win” instead of a row column from Player1.
  7. If Player2 sent “you win” then the next message from Player1 should be either “continue\n” or “end\n” with the obvious meanings of continue with a new game or end the match. To continue go to step 3.
    If Player2 received “you win” then send either “continue” or “end”. To continue go to step 3 of the Player1 protocol taking on the role of white (the loser gets to go first on the next game).

 

The polite way to end a game prematurely is for the ending player to send –1 for both the row and the column.

 

A Gomoku Playing Board (essentially the same as for assignment 4)

 

·         Your program should display the board as a rectangular grid of lines with black and white circles placed at the intersections to represent the pieces.

·         Black always moves first.

·         You should use some type of highlighting to distinguish the most recently played stone.

·         Your program should detect when the game has been won and highlight the winning row of stones.

·         Your program should allow for a sequence of games, displaying the number of wins by each player somewhere on the display.

·         Your program should include a menu item that allows the user to select a board size between 9x9 and 19x19. You can either allow for a specific set of choices (make sure there are at least three – 9x9, 15x15, and 19x19) or you can let them chose any board size in the range 9 to 19. The board size is ultimately determined by Player2 (the second to arrive at the server).

·         It is up to you if they are allowed to change the board size in the middle of a match. They should not be allowed to change the board size in the middle of a game.

·         If the board fills without a winner, declare it a draw ending the game with no score for either player.

·         Please put your main method in a class called Gomoku.

·         Your program should require one command line argument, the hostname of the server, and a second optional argument, the port number on the server. If the second argument is omitted the default port is 5555.

·         Your program should include Disconnect and Connect menu items under the File menu. The Disconnect should confirm that the user really wants to disconnect and then break the connection resetting the board an match to the initial state of no games and no pieces played. The connect should pop-up a dialog asking for the server hostname and port number, and then proceed as if these had been given at the command line.

 

Extra Credit

 

You can use these if you didn’t do them for program 4.

A nice extra credit option would be to allow your program to be used either by two people, or by one person with the computer playing the other color.

Extra credit will also be given to a fully working program that creates a visually pleasing board (i.e. use images for the stones instead of simple black and white filled circles).