Write a simple server program that will play tic-tac-toe with a client(s) using
ID: 3548546 • Letter: W
Question
Write a simple server program that will play tic-tac-toe with a client(s) using TCP sockets. You will program the server side only. Your server will play with a given client repeatedly until the server wins the 10th game. Your server must be able to play with any number of clients but only one at a time (ie. single-threaded is fine). You are given the following types, variables, and functions to use:
Board_t B; //the board, do not define, assume this is given
// current tic-tac-toe board is in one of the following states // 1) error (due to an illegal client move)
// 2) cwin: (client has won)
// 3) swin:(server has won)
// 4) playing: (in the middle of play)
typedef enum {playing, error, cwin, swin} board_status_t;
// Applies a move to the board and returns board_status_t board_status apply_move (Board_t*, move_t);
The play always begins when the client sends his/her first move to the server. A move (move_t) contains a X or O and the board position (1 .. 9). The server applies the move and checks the board status to determine if the game is: a) still being played, b) if someone has won, or c) if the board is in error (due to the client move; a server move cannot produce an error). The server then sends back a response move (move_t). The response move will be the next move by the server if the game is still being played, or a special sentinel move indicating that the client has either won (a move position of -1), or the server has just won (a move position is -2), or the game is in error and will be terminated (a move position of -3).
Things to do:
You must define move_t such that it can be sent directly between clients and servers. Program the server including all socket setups and application protocol logic. You need not implement the functions given above, just use them. Do not worry about who is X or O.
Explanation / Answer
The code runs as follows:
package netgame.tictactoe;
import java.io.IOException;
import netgame.common.Hub;
/**
* A "Hub" for the network TicTacToe game. There is only one Hub
* for a game, and both network players connect to the same Hub.
* Official information about the state of the game is maintained
* on the Hub. When the state changes, the Hub sends the new
* state to both players, ensuring that both players see the
* same state.
*/
public class TicTacToeGameHub extends Hub {
private TicTacToeGameState state; // Records the state of the game.
/**
* Create a hub, listening on the specified port. Note that this
* method calls setAutoreset(true), which will cause the output stream
* to each client to be reset before sending each message. This is
* essential since the same state object will be transmitted over and
* over, with changes between each transmission.
* @param port the port number on which the hub will listen.
* @throws IOException if a listener cannot be opened on the specified port.
*/
public TicTacToeGameHub(int port) throws IOException {
super(port);
state = new TicTacToeGameState();
setAutoreset(true);
}
/**
* Responds when a message is received from a client. In this case,
* the message is applied to the game state, by calling state.applyMessage().
* Then the possibly changed state is transmitted to all connected players.
*/
protected void messageReceived(int playerID, Object message) {
state.applyMessage(playerID, message);
sendToAll(state);
}
/**
* This method is called when a player connects. If that player
* is the second player, then the server
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.