PROGRAM DESCRIPTION:

In this minor, you will write two complete C programs that will allow two players to play the game of network tic-tac-toe. It will include two programs, a server and a client. The server will allow two clients to connect and then will begin the game. The client programs accept input from the player and transmits the command to the server which will execute the commands and send a reply back to the client programs.

The client and server programs are to communicate via the Internet (network) using TCP sockets. Your server should be able to handle commands from either client in any order. Your clients should be able to handle responses from the server or the player. (hint: use select)

The game is for two players on a 3x3 grid. The player who moves first uses X marks. The second player uses O marks. Each player takes turns placing their mark (X/O) on an empty spot on the grid. The game ends when all spots have a mark or either player has 3 marks in a row.

REQUIREMENTS:

  • Your code should be well documented in terms of comments.
  • Your server should be named "minor4server.c", without the quotes.
  • Your client should be named "minor4client.c", without the quotes.

General:

  • The server should accept connections from two clients then begin the game.
  • The following commands should be implemented:
  • M< R>< C> - Player places a mark on row R column C.
  • Rows are the letters A, B or C
  • Columns are numbers 1, 2 or 3
  • R - Resign
  • ? - Display Help

Server:

  • Your server should be named "minor4server.c", without the quotes.
  • Should take a port number as a command line parameter.
  • Display commands received from clients.
  • Display responses sent back to the clients.
  • After each valid move the board should be sent to both clients and a message to the other player that it is their turn.
  • The board should have legends for rows an columns (see sample games below).
  • The server should prevent players from going out of turn.
  • When the game ends. The server should send the board and notify each player of the results. (i.e. who won, or tie, resigned, See sample games below).

Client:

  • Your client should be named "minor4client.c", without the quotes.
  • Should take a hostname and a port number as command line parameters
  • Display a message "Connecting to Server"
  • Be able to process commands from the user and messages from the server in any order.
  • The server can send messages at any time that that should be displayed when received.
  • The player can enter commands at any time which should be sent to the server when entered.

SAMPLE OUTPUT

Example Game 1 - Server

$ ./minor4server 1100
Network Server Starting
Waiting on Clients
XX>You are player X. Waiting on player O to connect
OO>You are player O
X> board
1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | |
3
X>Your turn
OO>Invalid command
OO>It's not your turn
XX>Invalid Move
Move should be M with no spaces
Example: MA1 or MB3
X
O>You win, X resigned
Game Ended

Example Game 1 - First Client

$ ./minor4client cse03.cse.unt.edu 1100
You are player X. Waiting on player O to connect
board
1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | |
Your turn
MA5
Invalid Move
Move should be M< R>< C> with no spaces
Example: MA1 or MB3
R

Example Game 1 - Second Client

$ ./minor4client cse03.cse.unt.edu 1100
You are player O
board
1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | |
X
Invalid command
MA5
It's not your turn
You win, X resigned

Example Game 2 - Server

$ ./minor4server 1100
Network Server Starting
Waiting on Clients
XX>You are player X. Waiting on player O to connect
OO>You are player O
X> board 1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | |
X>Your turn
X< MA1 X>
board
1 2 3
+-+-+-
A|X| |
+-+-+-
B| | |
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A|X| |
+-+-+-
B| | |
+-+-+-
C| | |
O>Your turn
O< MB1 X>
board
1 2 3
+-+-+-
A|X| |
+-+-+-
B|O| |
5
+-+-+-
C| | |

Example Game 2 - Server (continued)

O>
board
1 2 3
+-+-+-
A|X| |
+-+-+-
B|O| |
+-+-+-
C| | |
X>Your turn
X
board
1 2 3
+-+-+-
A|X|X|
+-+-+-
B|O| |
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A|X|X|
+-+-+-
B|O| |
+-+-+-
C| | |
O>Your turn
O
O>That spot is already taken
O< MB2 X>
board
1 2 3
+-+-+-
A|X|X|
+-+-+-
B|O|O|
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A|X|X|
+-+-+-
B|O|O|
+-+-+-
C| | |

Example Game 2 - Server (continued)

X>Your turn
X< MA3 X>
board
1 2 3
+-+-+-
A|X|X|X
+-+-+-
B|O|O|
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A|X|X|X
+-+-+-
B|O|O|
+-+-+-
C| | |
X>You win
O>You lose
Game Ended

Example Game 2 - First Client

./minor4client cse03.cse.unt.edu 1100
You are player X. Waiting on player O to connect
Board
1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | |
Your turn
MA1
board
1 2 3
+-+-+-
A|X| |
+-+-+-
B| | |
+-+-+-
C| | |

Example Game 2 - First Client (continued)

board
1 2 3
+-+-+-
A|X| |
+-+-+-
B|O| |
+-+-+-
C| | |
Your turn
MA2
board
1 2 3
+-+-+-
A|X|X|
+-+-+-
B|O| |
+-+-+-
C| | |
board
1 2 3
+-+-+- A|X|X|
+-+-+-
B|O|O|
+-+-+-
C| | |
Your turn
MA3
board
1 2 3
+-+-+-
A|X|X|X
+-+-+-
B|O|O|
+-+-+-
C| | |
You win

Example Game 2 - Second Client

./minor4client cse03.cse.unt.edu 1100
You are player O
board 1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | |
board
1 2 3
+-+-+-
A|X| |
+-+-+-
B| | |
+-+-+-
C| | |
Your turn
MB1
board
1 2 3
+-+-+-
A|X| |
+-+-+-
B|O| |
+-+-+-
C| | |
board
1 2 3
+-+-+-
A|X|X|
+-+-+-
B|O| |
+-+-+-
C| | |
Your turn
MA2
That spot is already taken
MB2
board
1 2 3
+-+-+-
A|X|X|
+-+-+-
B|O|O|
+-+-+-
C| | |
board
1 2 3
+-+-+-
A|X|X|X
+-+-+-
B|O|O|
+-+-+-
C| | |
You lose

Example Game 3 - Server

$ ./minor4server 1100
Network Server Starting
Waiting on Clients
XX>You are player X. Waiting on player O to connect
OO>You are player O
X> board 1 2 3
+-+-+-
A| | |
9
+-+-+-
B| | |
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | | X>Your
turn
OO>
?-Display this help
R-Resign
Move< R>< C>-Move where < R> is a row A, B or C and is a column 1,2 or 3
Example Moves: MA1 MC3 MB1
X< MA2 X>
board
1 2 3
+-+-+-
A| |X|
+-+-+-
B| | |
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A| |X|
+-+-+-
B| | |
+-+-+-
C| | |
O>Your turn

Example Game 3 - Server (continued)

OX>
board
1 2 3
+-+-+-
A|O|X|
+-+-+-
B| | |
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A|O|X|
+-+-+-
B| | |
+-+-+-
C| | |
X>Your turn
X< MB1 X>
board
1 2 3
+-+-+-
A|O|X|
+-+-+-
B|X| |
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A|O|X|
+-+-+-
B|X| |
+-+-+-
C| | |
O>Your turn
OX>
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-

Example Game 3 - Server (continued)

B|X| |
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X| |
+-+-+-
C| | |
X>Your turn
X< MB3 X>
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X| |X
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X| |X
+-+-+-
C| | |
O>Your turn
OX>
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C| | |
O>
board
1 2 3
+-+-+-

Example Game 3 - Server (continued)

A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C| | |
X>Your turn
X< MC1 X>
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X| |
O>
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X| |
O>Your turn

Example Game 3 - Server (continued)

X>
board
1 2 3
+-+-+A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X|O|
O>
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X|O|
X>Your turn
X< MC3 X>
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X|O|X
O>
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X|O|X
X>Tie game
O>Tie game
Game Ended

Example Game 3 - First Client

./minor4client cse03.cse.unt.edu 1100
You are player X. Waiting on player O to connect
board
1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | |
Your turn
MA2
board
1 2 3
+-+-+-
A| |X|
+-+-+-
B| | |
+-+-+-
C| | |
board
1 2 3
+-+-+-
A|O|X|
+-+-+-
B| | |
+-+-+-
C| | |
Your turn
MB1
board
1 2 3
+-+-+-
A|O|X|
+-+-+-
B|X| |
+-+-+-
C| | |
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X| |
+-+-+-
C| | |
Your turn

Example Game 3 - First Client (continued)

MB3
Board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X| |X
+-+-+-
C| | |
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C| | |
Your turn
MC1
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X| |
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X|O|
Your turn
MC3
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X|O|X
Tie game

Example Game 3 - Second Client

./minor4client cse03.cse.unt.edu 1100
You are player O
board
1 2 3
+-+-+-
A| | |
+-+-+-
B| | |
+-+-+-
C| | | ?
?-Display this help
R-Resign
Move< R>< C>-Move where < R> is a row A, B or C and < C> is a column 1,2 or 3
Example Moves: MA1 MC3 MB1
board
1 2 3
+-+-+-
A| |X|
+-+-+-
B| | |
+-+-+-
C| | |
Your turn
MA1
board
1 2 3
+-+-+-
A|O|X|
+-+-+-
B| | |
+-+-+-
C| | |
board
1 2 3
+-+-+-
A|O|X|
+-+-+-
B|X| |
+-+-+-
C| | |
Your turn

Example Game 3 - Second Client (continued)

MA3
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X| |
+-+-+-
C| | |
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X| |X
+-+-+-
C| | |
Your turn
MB2
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C| | |
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X| |
Your turn

Example Game 3 - Second Client (continued)

MC2
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X|O|
board
1 2 3
+-+-+-
A|O|X|O
+-+-+-
B|X|O|X
+-+-+-
C|X|O|X
Tie game
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.