Here is my latest code:
Here is the output based on how I responded to the questions: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; void prnRules(void) // function: prnRules // pre: none // post: prints the rules { 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: dice have been rolled // 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; } int turnAgain(int status) // function: turnAgain // pre: Player must have rolled a non disqualifying point combination // post: returns a valid (y or n) answer { char ans; if (status == 1) { printf("TURNAGAIN Would you like to risk the points for this round and continue? (y or n)? "); ans = getchar(); getchar(); } if (status == 0) { 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 gameAgain(int choice) // function: gameAgain // pre: none // post: returns a valid (y or n) answer { char ans; //do { if (choice == 1) printf("Play another game (y or n)? "); ans = getchar(); getchar(); } //while (tolower(ans) != 'y' && tolower(ans) != 'n'); return tolower(ans); } int gameOver (int p1Total, int p2Total) // function: gameOver // pre: none // post: returns a '1' if either total exceeds 100 { if (p1Total || p2Total >= 100) { return 1; } else { return 0; } } //The final print statement with all the totals void finalOutput(int p1Total, int p2Total) // function: finalOutput // pre: gameOver returns a '1' // post: returns the final scores of the game { 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) // function: pudatePlayerTotal // pre: none // post: returns the player total { 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\n", playerTotal); return playerTotal; } int main (void) {//begin main // variable declarations int sum; // for sum of two dice char answer; // for yes/no questions int tempTotal = 0; // temporary player total for the round int p1Total = 0; // running total for player 1 int p2Total = 0; // running total for player 2 int total = 0; // player total after each round int die1 = 0; // the roll value of die1 int die2 = 0; // the roll value of die2 int currentPlayer = 1; // start with Player 1 int status = 0; // ability to choose to continue srand(time(NULL)); // seed random # generator do // play at least one game {//Begin game loop //give option to view the rules printf("Welcome to the game of Pig. Would you like to view the rules? (y or n)?\n"); answer = getchar(); getchar(); if (answer == 'y') prnRules(); do // until game is won {//Begin roll loop1 { //Begin roll loop2 die1 = rollDie(); die2 = rollDie(); sum = (die1 + die2); printf("Player %d:\n", currentPlayer); 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(" Player %d, you rolled a double %d!\n ", currentPlayer, die1); printf(" That's worth %d points.\n", (sum * 2)); tempTotal = (tempTotal + total + (sum * 2)); status = 1; } //award points (rolled double 1's) else if (turnPoints(die1, die2) == onesPoints) { printf(" Player %d, You rolled a double 1!\n ", currentPlayer); printf(" That's worth 25 points.\n"); tempTotal = (tempTotal + total + 25); status = 1; } //award no points (one of the two dice = 1) else if (turnPoints(die1, die2) == noPoints) { printf("Sorry Player %d, you rolled a single 1. You do not earn any points this round\n", currentPlayer); printf("Your current total is %d\n", total); tempTotal = 0; total = total + tempTotal; status = 0; } //award points (rolled singles) else if (turnPoints(die1, die2) == singlesPoints) { printf("Player %d, your roll totals %d points.\n", currentPlayer, sum); tempTotal = tempTotal + (total + sum); status = 1; } }while (status == 1 && (turnAgain(status) == 'y'));//end roll loop2 - while player continues roll if (turnAgain(status) == 'n')//(answer == 'n') { total = (tempTotal + total); if (currentPlayer == 1) { p1Total = updatePlayerTotal(currentPlayer, tempTotal, p1Total); } else { p2Total = updatePlayerTotal(currentPlayer, tempTotal, p2Total); } //switch players currentPlayer = switchPlayer(currentPlayer); } }while (gameOver (p1Total, p2Total) != 1); //end loop1 - continue while game is not over gameAgain(1); }while (answer != 'n'); //end game loop - loop while wanting to play again return 0; }//end main
Thats only part of the output, but the patterns continue the same way. There are several issues here but :Code:Welcome to the game of Pig. Would you like to view the rules? (y or n)? n Player 1: Die 1 is a 6. Die 2 is a 2. Player 1, your roll totals 8 points. TURNAGAIN Would you like to risk the points for this round and continue? (y or n)? n TURNAGAIN Would you like to risk the points for this round and continue? (y or n)? n Player 1: Die 1 is a 6. Die 2 is a 3. Player 1, your roll totals 9 points. TURNAGAIN Would you like to risk the points for this round and continue? (y or n)? y TURNAGAIN Would you like to risk the points for this round and continue? (y or n)? y Player 1: Die 1 is a 1. Die 2 is a 2. Sorry Player 1, you rolled a single 1. You do not earn any points this round Your current total is 0 Player 1, your total at the start of this turn was 0 . Your total at the end of this turn is 0. Player 2: Die 1 is a 1. Die 2 is a 5. Sorry Player 2, you rolled a single 1. You do not earn any points this round Your current total is 0 Player 2, your total at the start of this turn was 0 . Your total at the end of this turn is 0. Player 1: Die 1 is a 3. Die 2 is a 4. Player 1, your roll totals 7 points. TURNAGAIN Would you like to risk the points for this round and continue? (y or n)? n TURNAGAIN Would you like to risk the points for this round and continue? (y or n)? n Player 1: Die 1 is a 3. Die 2 is a 2. Player 1, your roll totals 5 points. TURNAGAIN Would you like to risk the points for this round and continue? (y or n)? n TURNAGAIN Would you like to risk the points for this round and continue? (y or n)? n Player 1: Die 1 is a 1. Die 2 is a 3. Sorry Player 1, you rolled a single 1. You do not earn any points this round Your current total is 0 Player 1, your total at the start of this turn was 0 . Your total at the end of this turn is 0.
1). I shouldnt have to answer y (or n) twice to make it continue (or not continue).
2). When I tell it to continue it should continue with the same player
3). When I answer no to continue it should switch to the other player.
4). Whenever the player switches it should give a cumulative total for the rounds played.
For now I just want to figure out how to answer the question once and make it switch players.
This may be a potential problem line of code:
I just added the "status ==1 &&" to it. before I added that to it I could answer yes to the continue question all day and it would just keep asking it over. If I answered no, it would end the game after asking it a second time.Code:}while (status == 1 && (turnAgain(status) == 'y'));//end roll loop2 - while player continues roll
Can anyone give me any hints as to how I should fix this?
Thanks a bunch
crazychile



LinkBack URL
About LinkBacks


