In this assignment, you will be creating a client and a server program to play a simple number guessing game (Are You Psychic!)). Client programs can connect to the server to play the game. Clients send their guess to the server. The server responds by sending the current game status back to the client.

The game requires user to guess a three digit number (000 to 999).

Here is the process to play the game:

  • The player (at the client) is prompted to enter the player's name and is sent to the server.
  • The three digit number is selected randomly by the server.
  • The player enters a guess.
  • The client makes sure the guess is valid (in the range 000 to 999) and sends the guess to the server.
  • The server first checks if the player has successfully guessed the number. If so, go to step 8.
  • If the guess was not correct, the server responds to the client with either "Too high" or "Too low."
    • Example: Assume the mystery number is 207. The guess 381 would result in a response of "Too high"
    • Example: Assume the mystery number is 207. The guess 180 would result in a response of "Too low"
    • Example: Assume the mystery number is 207. The guess 207 would result in a response of "Correct guess!"
  • Repeat steps 3-6 until the game is won.
  • When the game is won, the server will respond with a victory message that indicates the number of turns it took to guess the mystery number.
  • In addition, the server will send a leader board indicating the name of the top three players along with the number of turns it took to guess the mystery number.

Are You Psychic! Client

Here is a more detailed description of the client:

  • Starting the client will have two required command line arguments:
    • IP address of the server
    • port of the server process
  • Connect with the server. Exit with an appropriate error message if a connection could not be established.
  • Ask the user for their name.
  • Send the name to the server.
  • Ask the user for a guess. A guess consists of an integer.
  • Check if the guess is valid (the number is in the range from 0 to 999). If the guess is invalid, print an error message and go back to step 5.
  • Send the guess to the server.
  • Receive the result of the guess from the server.
  • If the guess was unsuccessful, print the result of the guess to the screen and go back to step 5.

Note: The remaining steps are only carried out if the guess was successful.

  • Receive an additional victory message from the server.
  • Print the victory message to the screen.
  • Receive leader board information received from the server.
  • Print leader board information to the screen.
  • Close the connection with the server.

Are You Psychic! Server

Here is a more detailed description of the server:

  • Starting the server will have one required command line argument: port number (on which the server listens)
  • Listen for a client.
  • When a client connects, create a new thread to process that client (see below).
  • The server will run forever until aborted (Ctrl-C).

The following steps correspond how to process each client:

  • Generate a random number (between 0 to 999) and print the number on the screen (while this is not necessary for actual gameplay, it helps debugging and grading).
  • The first communication step is to receive the player's name from the client.
  • Receive a guess of number from the client.
  • If the guess is not successful, send the result of the guess "Too high" or Too low (as described previously) and go back to step 3.

Note: The remaining steps are only carried out if the guess was successful.

  • Send the result of the guess ("Correct guess!").
  • Send a victory message indicating the number of turns it took.
  • Update the leader board if the player made the top three.
  • Send the current leader board to the client.
  • Close the connection with the client.

Leader Board

A few notes about the leader board:

  • When the server is started, the leader board initially starts empty.
  • If the leader board contains fewer than three entries, only display the current number of entries.
  • Ties are broken by whoever completed the game first as determined by when the leader board is updated at the end of the game.
    • For example, let's say the following people played in the following order from earliest to latest with number of turns in parentheses: Aadya (19), Bryn (15), Mark (19), Neal (15). Then the leader board would be:
1. Bryn 15
2. Neal 15
3. Aadya 19
  • Do not make this more complicated than necessary. This rule actually makes tie handling very simple.
  • The output of the client should display each person on the leader board on its own line with each line displaying the user name and the number of turns it took (as shown in the example illustrating ties above.)
  • Did you realize that the data structure storing the leader board is shared among the different client threads? Your program should protect against any issues that may arise from sharing the leader board. (Use appropriate synchronization primitives such as locks or semaphores.)

Additional Requirements and Assumptions

  • You may assume the user's name will not be longer than 100 characters.
  • The guess sent from the client to the server must be sent as an integer. The result returned from the server must be sent as a string.
  • All integers must be sent in network order.
  • Since the server runs forever, it must be robust. In particular:
    • Any error that occurs when processing a client causes only that thread to abort. These types of errors should not cause the server as a whole to crash.
    • The server should be free of leaks - dynamically allocated resources need to be reclaimed.
  • If a socket function that creates / uses the listening socket fails, abort the server with an appropriate error message.
  • You may assume that the client only connects to a program running your server and vice versa.
  • Your implementation should support multiple concurrent clients/players at the server

Sample Playing of the Game

For this sample playing, assume that the mystery number is 691.

Welcome to Number Guessing Game!

Enter your name: Aditya
Turn: 1
Enter a guess: 500
Result of guess: Too low
Turn: 2
Enter a guess: 750
Result of guess: Too high
Turn: 3
Enter a guess: 625
Result of guess: Too low
Turn: 4
Enter a guess: 688
Result of guess: Too low
Turn: 5
Enter a guess: 719
Result of guess: Too high
Turn: 6
Enter a guess: 704
Result of guess: Too high
Turn: 7
Enter a guess: 696
Result of guess: Too high
Turn: 8
Enter a guess: 692
Result of guess: Too high
Turn: 9
Enter a guess: 690
Result of guess: Too low
Turn: 10
Enter a guess: 691
Result of guess: Correct guess!

Congratulations! It took 10 turns to guess the number!

Leader board:
1. Maya 3
2. Aadya 5
3. Aditya 10
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.