Code:
#include <stdio.h> // for interactive input and output
#include <stdlib.h> // for the rand() function
#include <ctype.h> // for tolower() macro
#define SIDES 6
typedef enum {doublesPoints, onesPoints, noPoints, singlesPoints} status;
//the rules of the game
void prn_rules(void)
{
printf("%s\n",
"The goal is to be the first player to reach 100 points.\n\n"
"On a turn, a player rolls the two dice repeatedly until either a 1 is rolled \n"
"or the player chooses to hold and stop rolling.\n\n"
"If a 1 is rolled, that player's turn ends and no points are earned for the round.\n"
"If the player chooses to hold, all of the points rolled during that round are added to their score.\n\n"
"If a player rolls double 1s, that counts as 25 points.\n"
"Other doubles are worth 2x double points, so that a 2-2 is worth 8 points; 3-3 is worth 12; \n"
"4-4 is worth 16; 5-5 is worth 20; and 6-6 is worth 24.\n\n"
"When a player reaches a total of 100 or more points, the game ends and that player is the winner.\n");
}
int rollDie ()
// function: rollDie
// pre: none
// post: generates a random number for the die
{
int d;
d = rand() % SIDES + 1;
return d;
}
status turnPoints(int die1, int die2)
// function: turnStatus
// pre:
// post: returns the status of the roll (doublesPoints, onesPoints, no Points, singlesPoints)
{
if (die1 == die2 && !(die1 == 1 && die2 == 1))
return doublesPoints;
else if (die1 == 1 && die2 == 1)
return onesPoints;
else if ((die1 == 1 || die2 == 1) && !(die1 == 1 && die2 == 1))
return noPoints;
else
return singlesPoints;
}
char turnAgain()
// function: turnAgain
// pre: none
// post: returns a valid (y or n) answer
{
char ans;
do
{
printf("Would you like to risk the points for this round and continue? (y or n)? ");
ans = getchar();
getchar();
} while (tolower(ans) != 'y' && tolower(ans) != 'n');
return tolower(ans);
}
//to switch the current player when the turn is over
int switchPlayer(int currentPlayer)
{
if (currentPlayer == 1)
return 2;
else
return 1;
}
char playAgain(int choice)
// function: playAgain
// pre: none
// post: returns a valid (y or n) answer
{
char ans;
do
{
if (choice == 1)
printf("Play another game (y or n)? ");
if (choice == 2)
printf("Do you want to risk it all and roll again (y or n)? ");
ans = getchar();
getchar();
} while (tolower(ans) != 'y' && tolower(ans) != 'n');
return tolower(ans);
}
//to determine if the game is over
int gameOver (int p1Total, int p2Total)
{
if (p1Total || p2Total >= 100)
{
return 1;
}
else
{
return 0;
}
}
//The final print statement with all the totals
void finalOutput(int p1Total, int p2Total)
{
printf("\n");
printf("WE HAVE A WINNER!!!\n");
printf("Player 1 your final score was %d\n", p1Total);
printf("Player 2 your final score was %d\n", p2Total);
}
int updatePlayerTotal(int currentPlayer, int turnTotal, int playerTotal)
{
printf("Player %d, your total at the start of this turn was %d .\n",
currentPlayer, playerTotal);
playerTotal = playerTotal + turnTotal;
printf("Your total at the end of this turn is %d ,\n", playerTotal);
return playerTotal;
}
int main (void)
{
// variable declarations
int sum; // for sum of two dice
char answer; // for yes/no questions
int tempTotal = 0;
int p1Total; //Running total for player 1
int p2Total; //Running total for player 2
int total = 0; //total before assigning to player 1 or 2
int die1 = 0;
int die2 = 0;
int currentPlayer = 1; // Start with Player 1
srand(time(NULL)); // seed random # generator
do // play at least one game
{
//give option to view the rules
prnRules();
//roll and show results
makePlay();
die1 = rollDie();
die2 = rollDie();
printf("Die 1 is a %d.\n", die1);
printf("Die 2 is a %d.\n", die2);
//award points (rolled a double but not double 1's)
if (turnPoints(die1, die2) == doublesPoints)
{
printf(" You rolled a double %d!\n ", die1);
printf(" That's worth %d points.\n", (sum * 2));
tempTotal = (tempTotal + total + (sum * 2));
}
//award points (rolled double 1's)
else if (turnPoints(die1, die2) == onesPoints)
{
printf(" You rolled a double 1!\n ");
printf(" That's worth 25 points.\n");
tempTotal = (tempTotal + total + 25);
}
//award no points (one of the two dice = 1)
else if (turnPoints(die1, die2) == noPoints)
{
printf("Sorry, you rolled a single 1. You do not earn any points this round\n");
printf("Your current total is %d\n", total);
tempTotal = 0;
total = total + tempTotal;
switchPlayer;
}
//award points (rolled singles)
else (turnPoints(die1, die2) == singlesPoints)
{
printf("Your roll totals %d points.\n", sum);
tempTotal = tempTotal + (total + sum);
}
answer = playAgain(2); //LINE 195
while (turnAgain == 'y'); //LINE 196
{
// roll again
makePlay();
printf("Die 1 is a %d.\n", die1);
printf("Die 2 is a %d.\n", die2);
}
//total the points for the player and switch to other player
if (answer == 'n')
{
total = (tempTotal + total);
if (currentPlayer == 1)
{
p1Total = updatePlayerTotal(currentPlayer, tempTotal,
p1Total);
}
else
{
p2Total = updatePlayerTotal(currentPlayer, tempTotal,
p2Total);
}
// return p1Total, p2Total;
currentPlayer = switchPlayer(currentPlayer);
}
if (gameOver(p1Total, p2Total) == 1)
finalOutput (p1Total, p2Total);
// see if they want to play another game
answer = playAgain(1);
} while (answer != 'n');
return 0;
}