C Program

We will be implementing a variation of the dice game Cee-lo. The following description has been adopted from Wikipedia.

Two players (in our case, one human, once CPU) roll three dice each until both have a recongized dice combination. Whoever rolls the better combination wins! A tie results in both player and CPU rerolling their three dice until they both have a recognized combination. Recognized combations are ranked below from best to worst:

  • 4-5-6: The die values are 4, 5, and 6 (order doesn't matter).
  • Trips: The three die values are all the same. If both players roll trips, the higher trips (larger dice value) beats the lower trips.
  • Point: A pair is rolled. The non-matching dice is the "point. A higher point beats a lower point (so 1-1-5 beats 6-3-6).
  • 1-2-3: The die values are 1, 2, and 3 (order doesn't matter).

Note that a tie in terms of Trips would be both players rolling the same trips numbers (i.e. 3-3-3 and 3-3-3), and a tie in terms of Point would be both players rolling the same point (i.e. 6-2-6 and 3-3-2).

A single game of Cee-lo is both players rolling a recognized combination and NOT tying (that is, one player's combination beats the other players combination).

Write a program (called ceelo.c) that implements Cee-lo according to the above rules. The game should also allow for wagering. This means that you need to prompt the user for an initial bank balance (we assume they're honest!) from which their winnings or losings will be added or subtracted. Before each game, prompt the user for a wager. Re-ask for a wager if their wager is not valid (if they dont have enough money to make their wager). Once a game is won or lost, the bank balance should be adjusted, and, if the player has a non-zero balance, the player should be able to choose if they want to play another game or cash out their winnings (quit the program).

Several functions have been implemented for you in the template provided.

  • void print_game_rules(void) - Prints out the rules of the game.
  • void is_number_in_die(int die[3], int num) - Returns a 1 if num is one of the three values in the die array and a 0 if not.
  • void has_pair(int die[3]) - Returns the value of the unmatched die if a pair exists in the 3 die, or a 0 if no pair exists.

You will need to use the below functions to help you write the game logic. You may define more than just these, but these functions must be defined as described below:

  • double get_bank_balance(void) - Prompts the player for an initial bank balance and re- turns it.
  • double get_wager_amount(void) - Prompts the player for a wager on a particular game of Cee-lo. The wager is returned.
  • int check_wager_amount(double wager, double balance) - Checks to see if the wa- ger is within the limits of the player's available balance. If the wager exceeds the players balance, then 0 is returned; otherwise, 1 is returned.
  • void roll_die(int die[3]) - Rolls three die. This function should randomly generate a value between 1 and 6 inclusive for each dice value in the die array. Here we are using an array to hold a player's three dice. Remember that a function can change the values in an array that is passed in as an argument. To generate a random number, you use the rand() function. By itself, rand() will generate a random number between 0 and some maximum integer value, usually 32767. In other words int x = rand(); In the above example, x is now some random integer between 0 and 32767. What youll need to figure out is how to get a random number between 1 and 6 inclusive. Note that rand() takes no arguments, so there is no way for you to tell the function to generate specific values. Hint: use the mod operator and some other math.
  • int get_rank(int die[3])-Returns the rank of the combination for the dice values passed in. If the combination is 4-5-6, this function should return a 4; if the combination is Trips, this function should return a 3; if the combination is Point, this function should return a 2; if the combination is 1-2-3, this function should return a 1; if the combination is not one of the previous four (aka it is not a recognized combination), this function should return a 0. You will need to use the is_number_in_die() and has_pair() functions in this function.
  • int user_won_lost_or_neither(int user_die[3], int cpu_die[3]) - Returns a 1 if the user has won, a 0 if the user has lost, and a -1 if there was a tie game. This function will need to call the get_rank() function for both the player and the cpu's dice. If the users combination ranks higher than the cpus, the user has won; if the cpus combination ranks higher than the users, the user has lost. If the combination ranks are the same, you will need to check for the case of Trips and Point whether there was truly a tie or not. For example, if both user and cpu rolled trips, the get_rank() function will return 3 for both. However, it is only a tie game if both cpu and player had matching trips. If they didnt, the result of the game depends on the trips value. The same goes for the Point combination.
  • double adjust_balance(double balance, double wager, int add_or_subtract) - If add_or_subtract is 1, then the wager is added to the balance and the sum is returned. If add_or_subtract is 0, then the wager is subtracted from the balance and the different is returned. Otherwise, the balance remains unchanged and is returned.
  • Does not need to be implemented as a function, but various printf() statements in main() indicating the status of the game: at minimum, the bank balance at the start of a game, the dice rolls during the game, whether the player won or lost, and the player's resulting balance at the end of each game.
  • A main() function that makes use of the above functions in order to play the game of Cee-lo. The program should play multiple game of Cee-lo if the player's bank balance allows for it (the program should print out a message and quit if they player loses all their money). It should also allow the player to "cash out" at the end of a game (i.e. quit) even if they still have money to wager.

All functions should be prototyped above main and defined below it. There is a lot to this assignment, but a good approach is to implement the functions as they're described above (except for main()) and make sure they function as they should (by writing tests in main(). Once you know each function behaves as it should, begin work on main(). Write this function in steps: write it to play a single game of Cee-lo with no wagering, then add wagering (but still one game), then add the ability to play multiple games. You dont have to break it down like this, but many students have found it helpful to approach this assignment like this in the past.

Template

#include < stdio.h >
#include < time.h >
#include < stdlib.h >
void print_game_rules(void);
int is_number_in_die(int die[3], int num);
int has_pair(int die[3]);
// Add other function prototypes below here but above main().
int main(void)
{
// The line below sets the seed of the random number generator.
srand(time(0));
print_game_rules();
// Your game logic goes here.
return 0; }
// It is good practice to define all functions after the body of the main()
// function.
void print_game_rules(void)
{
printf("Cee-lo rules:\n"
"This is a battle of the player against the CPU. Both roll three\n"
"dice each until both have a recognized combination. Whoever rolls\n"
"the better combination wins! A tie results in both player and CPU\n"
"rerolling their die until they each have a new combination.\n"
"Combinations are ranked from best to worst as:\n"
"\t4-5-6:\n\t\tThe die values are 4, 5, and 6 (any order)\n"
"\tTrips:\n\t\tThe three die values are all the same. Higher trips\n"
"\t\tbeats lower trips (but still note that trips beats \"point\"\n"
"\t\t(see below).\n"
"\tPoint:\n\t\tA pair is rolled. The non-matching dice is the\n"
"\t\t\"point\". A higher point beats a lower point (so 1-1-5 beats\n"
"\t\t6-3-6)\n"
"\t1-2-3:\n\t\tThe die values are 1, 2, and 3 (any order)\n"
"The above are the only recognized combinations. A player must\n"
"reroll until they have one of the above combinations\n");
}

int is_number_in_die(int die[3], int num)
{
for (int i = 0; i < 3; i++)
{
if (num == die[i])
return 1;
}
return 0; }
int has_pair(int die[3])
{
if (die[0] == die[1])
return die[2];
else if (die[1] == die[2])
return die[0];
else if (die[0] == die[2])
return die[1];
else
return 0;
}
// Add your other function definitions below here

Expected Results

The following illustrates an example of the program described above. Your program can display the game output however you would like as long as the necessary information is there (see #8 above). Your dice rolls will likely be different.

Cee-lo rules:
This is a battle of the player against the CPU. Both roll three
dice each until both have a recognized combination. Whoever rolls
the better combination wins! A tie results in both player and CPU
rerolling their die until they each have a new combination.
Combinations are ranked from best to worst as:
1-2-3:
"point". A higher point beats a lower point (so 1-1-5 beats
6-3-6)
The die values are 1, 2, and 3 (any order)
The above are the only recognized combinations. A player must
reroll until they have one of the above combinations

Enter in an initial bank balance (in dollars): 100
Your current balance is $100.00

Enter in a wager amount (in dollars): 500
Enter in a wager amount (in dollars): 400
Enter in a wager amount (in dollars): 50
You rolled (5, 4, 3) (unrecognized combo)
You rolled (6, 3, 4) (unrecognized combo)
You rolled (3, 2, 1)
CPU rolled (5, 2, 4) (unrecognized combo)
CPU rolled (4, 4, 6)
You lost!
Do you want to continue playing or quit and take your money ($50.00)
(’c’ for continue, ’q’ for quit): c
Your current balance is $50.00
Enter in a wager amount (in dollars): 50
You rolled (1, 5, 6) (unrecognized combo)
You rolled (1, 2, 1)
CPU rolled (4, 1, 4)
You won!
Do you want to continue playing or quit and take your money ($100.00)
(’c’ for continue, ’q’ for quit): c
Your current balance is $100.00
Enter in a wager amount (in dollars): 100
You rolled (2, 4, 6) (unrecognized combo)
You rolled (6, 4, 6)
CPU rolled (1, 6, 1)
You lost!
You left with $0.00.
Better luck next time.
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.