The problem

Write a client/server pair that implements a simple game of Tic-Tac-Toe.

The window for the client application will display a pair of status labels and a three by three grid of buttons. (The grid lines in the picture above are just for illustration. You do not need to draw grid lines in your window.) When it is a player's turn they can click one of the buttons for an unoccupied cell to make their move. The server will track the player's moves and determine when a move results in a win.

A player wins the game if they can get three of their symbols in a row, either horizontally, vertically, or down one of the two diagonals.

Client-server protocol

The table below shows the set of requests the client can send to the server and the responses the server will send back.

RequestAdditional DataServer Response
JOIN_GAMENonePLAYER_ONE, PLAYER_TWO, or GAME_FULL
WHOSE_TURNNonePLAYER_ONE, PLAYER_TWO, or GAME_OVER
GAME_BOARDNoneThe current configuration of the game board
MAKE_MOVECell row, cell columnACCEPTED or REJECTED

Since the game can only accomodate two players, once two clients have connected the server will respond to requests to join the game with the status code GAME_FULL. Player one uses the symbol X, and player two uses the symbol O.

When a player sends in their move with a MAKE_MOVE request the server will either accept or reject their move. The server will reject a move if it is not the player's turn, if the cell is already occupied, or if the game is already over. If the server accepts a player's move, the player's turn is over and it is the other player's turn to play.

Implementation hints

You should implement a Game class in the server application that keeps track of the game status.

To represent the game board you should use a two-dimensional array of ints, with 0 standing for an unoccupied cell, 1 for an X, and 2 for an O.

private int board[][] = {{0,0,0},{0,0,0},{0,0,0}};

You should implement a thread in the client that checks in frequently with the server to get the game status. Several times each second that thread should check in with the server to see whose turn it is and what the current configuration of the board is. This thread will update the text in the buttons to match the board state and also update the label that shows whose turn it is.