Back to UNIX page

POSIX Threads

Tic Tac Toe Game Server and Client


Write a tic tac toe server. Server's purpose is to pair players, provide synchronization during a game and check winning conditions. Server is a multi-threaded application - each game is handled by separate thread. Server waits for clients to connect on TCP port given as a command line parameter. If there are two clients connected to a server the game between them begins in a separate thread. There can be maximum number MAX_PLAYERS = 10 of players connected to the server at the same time. No new connections should be accepted if this number is exceeded.

Game handling thread randomly chooses one of two players to begin the game and sends him information about it.
Then in the loop thread waits for player's answer which contains move he want to play.
If move is improper (e.g. field is already occupied or field number is improper), player that made it loses the game immediately. Both players are informed about this situation, game finishes and players wait for another game to begin.
If move causes player to win the game or game becomes a draw, both players are informed about this situation, game finishes and players wait for another game to begin.
Otherwise updated board is send to another player ...

If client disconnects during the game, his opponent wins the game.

On SIGINT (C-c) server application: shouldn't accept any new players, should not start any new games, should terminate connection to any players waiting for a game. After all games are finished it should terminate.


Use telnet for client (e.g. telnet localhost 2000). All client screen output must be generated and sent by server. Reading from client can be organized on 1 character basis i.e. server reads character by character until new line. Move is defined by a number corresponding to the board position:


In case proper command line parameters are not supplied server should print usage information and exit.

Tic Tac Toe rules:
Tic-tac-toe is a two person game. Players named O and X take turns marking the spaces in a 3x3 grid board. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game. The game is a draw when there are no spaces left on the board.

Task will be graded by Ɓukasz Wojciechowski. Copy your solutions to /home2/samba/wojciechowskil/unix/ .

Extra attention should be payed to:
Back to UNIX page