Overview

In this project, you will write a C program that reinforces programming fundamentals such as control flow, functions, expressions and random number generation with rand() and srand().

The rand() function is a random number generator in C that returns a value in a specified range from [0, max] where the value returned is based off a seed value that is input into srand() where the seed value determines the pseudo-random numbers generated.

Premise

After years of attempting to understand the laws of physics, scientists have finally been able to create a time travel machine. A number of archeologists learned of such a feat, and have inquired about it so that they can study the past more thoroughly and effectively. Despite not having tested the time travel machine, the scientists agree to let the group of archeologists use it to go back in time to an ancient civilization named Tarhniv. While on their expedition to Tarhniv, the archeologists find a tablet inscription which details a game that the Tarhniv people of that time are playing. Soon enough, the local Tarhniv people find out that a mysterious band of people have invaded their lands and thus the archeologists need to escape by traveling back in time to their present time. With the Tarhniv gaming table inscription now in hand, the archeologists begin to decrypt the game that was played in the past and have tasked you with implementing a version for them to play it with.

The game of Gehntoo

The game of Gehntoo is played with two players. The game is similar to volleyball except that both players do not attempt to throw the ball back over the net. Rather, in Gehntoo, players take turns throwing the ball once per turn over the net, and based on whether their throw went over or under the net, they win or lose points. The player who first reaches a score <=0 points (starting from 75 points), wins the game and can choose to play again if desired.

Restrictions

  • You may only import stdio.h, stdlib.h and string.h
  • You may not use any global variables.

Gehntoo rules

Gehntoo is a two-player game where player one will be the human player (you) and the second player will be the computer (a nave AI).

When running the program, a welcome message is first displayed and player one is asked to enter his name. The name entered must not be longer than 7 characters and must contain only letters or blank spaces. If at least one of these conditions is not met, your program must display an error message and re-prompt to enter the name until it is a valid input.

Next, ask the player for a seed value. Verify that the seed value entered is no longer than 5 characters. You must validate that this seed value only contains numbers. Do not just call sscanf, call the is_valid_seed()function and check the return value to determine whether it is a valid seed. Skipping this step will result in a penalty on your score.

  • If the seed value entered is not valid (e.g. it contains non numbers, or exceeds 5 digits), an error message is displayed and the user is prompted to re-enter the seed.
  • Once a valid seed has been entered, pass the seed to the srand() function once (see the rand() explanation given above).

Ask the player to enter the height of the game net in feet and inches.

  • A valid input for this prompt would be as follows xxft xxin where the user can input two digits or a single digit before ft and two digits or a single digit before in (values range in [1, 99]).
    • Assume the user will enter a valid input. A valid input could be as follows: "3ft 3in", "30ft 30in", "17ft 6in", "3ft 10in", and " 3 FT 99 I N".
    • Once a valid net height is entered, parse it to determine the net height in inches.
    • Afterwards, print out the net height in inches.
    • Your program must verify that the input for the net height is correct, otherwise an error message is displayed and the user is prompted to re-enter the value.

Each player has three chances to play the game in a run. When the game ends, the player is asked if want to play again, if he agrees, then he has one less chance to play again. Once a player has no more chances to play, then the run ends. See section c.i (Playing again or exiting the game) for more information regarding playing the game again.

Both players start with 75 points and take turns throwing the ball trying to get it over the net.

Player one (human player)

Before attempting to throw the ball, the player must make a bet. The bet must not exceed his current total points and must range in [1, 11], inclusive. If this condition is not met, the player is prompted to re-enter a valid bet.

  • If the player threw the ball over the net, the amount of his bet is deducted from his total points.
  • If the player threw the ball under (or the same height as) the net, two points is added to his total points. Two points is added to their total points if they do not meet the criteria to rethrow otherwise:
    • If the player threw the ball less than half the height of the net, then he has the opportunity to throw it again. If he throws it over the net this time, what he bet before is deducted from his total points, otherwise he gets 4 points. Regardless of the result, the player will not have the opportunity to throw the ball again. If they choose not to rethrow, then their bet is added to their total points.
  • Display how high was the throw in inches.
  • Display how many points are being added or deducted from the player.
  • Display the player's total points (i.e., when points are added or deducted from his total points after the throw).

Player two (computer)

Once the player one has thrown, the computer will throw the ball.

  • The computer does not make a bet to be able to throw.
  • If the computer threw the ball over the net, then 9 points are deducted from its total points.
  • If the computer threw the ball under (or the same height as) the net, then 4 points are added to its total points. The computers points is added to their total points if they do not meet the criteria to rethrow otherwise:
    • If the computer threw the ball less than half the height of the net, the ball is thrown again. If its rethrow is still under (or the same height as) the net, then 6 points are added to its total points, otherwise 7 points are deducted from its total points. Regardless of the result, the computer will not have the opportunity to throw the ball again.
  • Display how high was the throw in inches.
  • Display how many points are being added or deducted from the computer.
  • Display the total points the computer has after its throw.

Playing again or exiting the game

The player who first reaches a score <=0 points win the game. When it occurs:

  • If the winner has no more chances to play, the run ends.
  • If the winner has still a chance to play, ask the user (on behalf of the winner) if he would like to play again. The user will enter either 'Y' or 'N'.
    • If the user enters 'Y', then the game starts again using the previous seed value and the previous net height. The player will then have one less chance to play again.
    • If answer is 'N', the run ends.
  • Before the run ends, display the following statistics:
    • How many times the player one won out of all the games played.
    • How many times the computer won out of all the games played.

Required functions

Choose the appropriate parameters for each of the following functions.

int is_valid_seed()

This function checks the input value for the seed and validates that only digits were entered, and that the value ranges in [1, 99999], inclusive. If at least one of these conditions is not met, then the function returns 0 otherwise 1.

int game_logic()

This function implements the game logic as indicated by the rules above.

int throw_ball()

This function throws the ball to a random height using the rand() function. The height generated at random must range in [0, net_height+16].

int is_valid_name()

This function determines if a valid name is entered by the user. A name must not be longer than 7 characters and must contain only letters or blank spaces. If name is valid, return 1 otherwise return 0.

int calculate_net_height()

This function determines the height of the net in inches of the net string that is input by the user. Return the calculated net height in inches.

You may create any additional functions that help you create your game logic.

Sample Output

Welcome to the Gehntoo game
Enter your name Jim
Enter a seed value to use in the range of [1-99999] 143d
Invalid seed, please enter a new seed value in the range [1-99999] 12563
Seed value to use is 12563
Enter the height of the net in feet and inches [xxft xxin] where xx must be [1-99] 3ft 3in
Height of the net: 39 inches
Each player is starting with 75 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 53 inches, the ball is over the net
Deducting 11 points
Jim now has 64 points
Computer's turn
Ball height: 14 inches, ball is below the net, throwing once more
Ball height: 25 inches, the ball is below the net
Adding 6 points
Computer now has 81 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 48 inches, the ball is over the net
Deducting 11 points
Jim now has 53 points
Computer's turn
Ball height: 13 inches, ball is below the net, throwing once more
Ball height: 10 inches, the ball is below the net
Adding 6 points
Computer now has 87 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 5 inches, Throw again? Type Y or N: Y
Ball height: 3 inches, the ball is below the net
Adding 4 points
Jim now has 57 points
Computer's turn
Ball height: 47 inches, the ball is over the net
Deducting 9 points
Computer now has 78 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 18 inches, Throw again? Type Y or N: Y
Ball height: 42 inches, the ball is over the net
Deducting 11 points
Jim now has 46 points
Computer's turn
Ball height: 21 inches, the ball is below the net
Adding 4 points
Computer now has 82 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 12 inches, Throw again? Type Y or N: Y
Ball height: 20 inches, the ball is below the net
Adding 4 points
Jim now has 50 points
Computer's turn
Ball height: 5 inches, ball is below the net, throwing once more
Ball height: 40 inches, the ball is over the net
Deducting 7 points
Computer now has 75 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 19 inches, Throw again? Type Y or N: Y
Ball height: 18 inches, the ball is below the net
Adding 4 points
Jim now has 54 points
Computer's turn
Ball height: 15 inches, ball is below the net, throwing once more
Ball height: 29 inches, the ball is below the net
Adding 6 points
Computer now has 81 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 50 inches, the ball is over the net
Deducting 11 points
Jim now has 43 points
Computer's turn
Ball height: 17 inches, ball is below the net, throwing once more
Ball height: 19 inches, the ball is below the net
Adding 6 points
Computer now has 87 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 51 inches, the ball is over the net
Deducting 11 points
Jim now has 32 points
Computer's turn
Ball height: 7 inches, ball is below the net, throwing once more
Ball height: 14 inches, the ball is below the net
Adding 6 points
Computer now has 93 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 10 inches, Throw again? Type Y or N: Y
Ball height: 10 inches, the ball is below the net
Adding 4 points
Jim now has 36 points
Computer's turn
Ball height: 35 inches, the ball is below the net
Adding 4 points
Computer now has 97 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 7 inches, Throw again? Type Y or N: Y
Ball height: 9 inches, the ball is below the net
Adding 4 points
Jim now has 40 points
Computer's turn
Ball height: 32 inches, the ball is below the net
Adding 4 points
Computer now has 101 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 22 inches, the ball is below the net
Adding 2 points
Jim now has 42 points
Computer's turn
Ball height: 18 inches, ball is below the net, throwing once more
Ball height: 8 inches, the ball is below the net
Adding 6 points
Computer now has 107 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 35 inches, the ball is below the net
Adding 2 points
Jim now has 44 points
Computer's turn
Ball height: 13 inches, ball is below the net, throwing once more
Ball height: 53 inches, the ball is over the net
Deducting 7 points
Computer now has 100 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 22 inches, the ball is below the net
Adding 2 points
Jim now has 46 points
Computer's turn
Ball height: 44 inches, the ball is over the net
Deducting 9 points
Computer now has 91 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 55 inches, the ball is over the net
Deducting 11 points
Jim now has 35 points
Computer's turn
Ball height: 49 inches, the ball is over the net
Deducting 9 points
Computer now has 82 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 9 inches, Throw again? Type Y or N: Y
Ball height: 12 inches, the ball is below the net
Adding 4 points
Jim now has 39 points
Computer's turn
Ball height: 53 inches, the ball is over the net
Deducting 9 points
Computer now has 73 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 14 inches, Throw again? Type Y or N: Y
Ball height: 36 inches, the ball is below the net
Adding 4 points
Jim now has 43 points
Computer's turn
Ball height: 16 inches, ball is below the net, throwing once more
Ball height: 16 inches, the ball is below the net
Adding 6 points
Computer now has 79 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 51 inches, the ball is over the net
Deducting 11 points
Jim now has 32 points
Computer's turn
Ball height: 29 inches, the ball is below the net
Adding 4 points
Computer now has 83 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 10 inches, Throw again? Type Y or N: Y
Ball height: 13 inches, the ball is below the net
Adding 4 points
Jim now has 36 points
Computer's turn
Ball height: 49 inches, the ball is over the net
Deducting 9 points
Computer now has 74 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 46 inches, the ball is over the net
Deducting 11 points
Jim now has 25 points
Computer's turn
Ball height: 4 inches, ball is below the net, throwing once more
Ball height: 7 inches, the ball is below the net
Adding 6 points
Computer now has 80 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 40 inches, the ball is over the net
Deducting 11 points
Jim now has 14 points
Computer's turn
Ball height: 55 inches, the ball is over the net
Deducting 9 points
Computer now has 71 points
Jim's turn. How much would you like to bet towards your throw? 11
bet 11 points
Ball height: 42 inches, the ball is over the net
Deducting 11 points
Jim now has 3 points
Computer's turn
Ball height: 47 inches, the ball is over the net
Deducting 9 points
Computer now has 62 points
Jim's turn. How much would you like to bet towards your throw? 11
your bet was not within the valid range of [1, 11] or you attempted to bet more points than you
currently have. Please enter a valid bet: 3
bet 3 points
Ball height: 48 inches, the ball is over the net
Deducting 3 points
Jim now has 0 points
Jim has won the current game, congratulations!
Would you like to play again Jim? Type Y or N: N
Jim chose not to play again. Thanks for playing!
Printing out game statistics now:
Jim won a total of 1 time(s)
Computer won a total of 0 time(s)
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.