HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Simple Version of Bunco Game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
versionbuncosimplegame

Problem

Any idea to write this code (fixable part) smarter?

Note: Arrays, pointers, global value are not allowed to use.

The game is played with 6 rounds. At the beginning, each player rolls one dice, with the highest roll
going first. On their turns, players roll three dice in order to earn points. During each round, players
attempt to roll the same number as the number of the round (for example, rolling three 3's
in Round 3 would earn the player points). For every number rolled that matches the round number,
one point is awarded to that player. A player keeps rolling until they score no points. Then the dice
are passed to other player.

Scoring:

Players score points when any of the dice they roll match the number of the round. Each matching
number rolled scores 1 point. For example, if a player rolls two 2's in round 2, the player receives two
points and rolls again.

The numbers on the dice are not added together, nor does the number on the dice indicate point
value. A 1 rolled in round 1 is worth 1 point, a 3 rolled in round 3 is worth 1 point, and a 6 rolled in round
6 is also worth 1 point.

However, if a player's dice all show the same number, they will earn more points.
If the three dice match the number of the round being played, it's called a Bunco and the player
earns 21 points. The player must call out "Bunco!" in order to receive the points. (For example, if a
player rolls three 4's in round 4, they would call out "Bunco!" and earn 21 points.)

If a player rolls three of another number that doesn't match the number of the current round (for
example, rolling three 6's in round 4), the player earns 5 points instead.

In this project, your task is to write a simplified Bunco game for 2 players and 6 rounds. Your program
should display the dice values for each round, accumulate and display the points won by each player
and find the player that makes more points. Please note that, if a player's dice all show the same
number, they will earn more points(BUNCO!).

E

Solution

Bug

In compute_points(), you never initialized the variable point, which means the function could end up returning the wrong value. You should initialize it to 0 like this:

int point = 0;


Simplifications for compute_points()

There are a few things that I would change in the compute_points() function.

  • You check whether the dice are equal twice, when you only need to do it once.



  • You make a special case for no dice matching the round, which is unnecessary.



  • You put the return statements on the same line as the if statements.



Here is how I would have written the function:

int compute_points(int dice1, int dice2, int dice3, int round)
{
    // If all 3 dice match, it's either a 21 point BUNCO or a 5 point BUNCO,
    // depending on whether the dice match the round or not.
    if (dice1 == dice2 && dice1 == dice3) {
        if (dice1 == round)
            return 21;
        else
            return 5;
    }

    // Otherwise, you get one point for each die that matches the round.
    return (dice1 == round) + (dice2 == round) + (dice3 == round);
}


Notes:

  • I took advantage of the fact that (dice1 == round) evaluates to 1 if true and 0 if false.



  • There is a practical reason for putting return statements on separate lines from if statements (not just for stylistic purposes). When you start using a debugger on your programs, you will often find that you will need to set a breakpoint on a return line to catch when your function returns a particular value. If you put the if statement on the same line as the return, your breakpoint will stop the program at the if instead of the return.



Other things

  • A few of your while loops could be better off by being do loops instead, but I'm not sure you have learned about do loops yet.



  • You tend to put a lot of things on the same line, such as an if statement, the code following the if, and a comment. I would advise putting each of these on a separate line. Again, this has a practical purpose for debugging.



-
For printing out multiple lines using printf(), I use a trick with string concatenation so that I can visualize what I'm printing better. For example, your code is this:

printf("Roll %d \nDice are rolled:\nDice 1: %d \t Dice 2: %d \t Dice 3: %d", rollNumber, dice1, dice2, dice3 );


I would do this:

printf("Roll %d \n"
       "Dice are rolled:\n"
       "Dice 1: %d \t Dice 2: %d \t Dice 3: %d",
       rollNumber, dice1, dice2, dice3);


When you have two strings back to back such as "string1" "string2", the C compiler will automatically concatenate those two strings into one string, even if they are on different lines. So my version is equivalent to yours.

Code Snippets

int point = 0;
int compute_points(int dice1, int dice2, int dice3, int round)
{
    // If all 3 dice match, it's either a 21 point BUNCO or a 5 point BUNCO,
    // depending on whether the dice match the round or not.
    if (dice1 == dice2 && dice1 == dice3) {
        if (dice1 == round)
            return 21;
        else
            return 5;
    }

    // Otherwise, you get one point for each die that matches the round.
    return (dice1 == round) + (dice2 == round) + (dice3 == round);
}
printf("Roll %d \nDice are rolled:\nDice 1: %d \t Dice 2: %d \t Dice 3: %d", rollNumber, dice1, dice2, dice3 );
printf("Roll %d \n"
       "Dice are rolled:\n"
       "Dice 1: %d \t Dice 2: %d \t Dice 3: %d",
       rollNumber, dice1, dice2, dice3);

Context

StackExchange Code Review Q#136274, answer score: 3

Revisions (0)

No revisions yet.