简体   繁体   中英

Function Help on Dice Game

I have a new problem. I got most of the things fixed. I need my (turnAgain) function fixed. I specifically need to fix my if else statement. I need it to let the player continue rolling if they type in 'r'. else if they type 'h' the other player gets to roll. And I need it to say try again type 'r' or 'h' if they type in something that is is not 'r' or 'h'. I just dont know how to do that. New to coding thanks!! I REALLY NEEEEEEEEEEEEEEEEEEEEEEED HELP.

#include <stdio.h>   
#include <stdlib.h> 
#include <ctype.h>
#include <time.h>

#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 50 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 50 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;

}
void turnAgain(int status)
{
    char rollDie;

do
{

printf("Would you like to roll again or hold(r/h)?");
scanf("%c",&rollDie);

} while(rollDie!='r'&&rollDie!='h');

 if(rollDie=='r')
 {

 }
 else
 {

 }

//to switch the current player when the turn is over
int switchPlayer(int currentPlayer)
{
    if (currentPlayer == 1)
        return 2;
    else
        return 1;
}


char gameAgain()
// function: gameAgain
// pre: none
// post: returns a valid (y or n) answer
{
    char ans;

    do
    {
        printf("Play another game (y or n)? ");
        ans = getchar();
        getchar();
    }while (ans != 'y' && ans != 'n');
    return ans;
}

int gameOver (int p1Total, int p2Total)
// function: gameOver
// pre:  none
// post: returns a '1' if either total exceeds 50
{
    if (p1Total || p2Total >= 50)
    {
        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;           // status for choice 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 Fifty. 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

        answer = gameAgain();

    }while (answer != 'n'); //end game loop - loop while wanting to play again
    return 0;
}//end main
int rollDie ()
// function: rollDie
// pre: none
// post: generates a random number for the die 
{
   int d;

   d = rand() % SIDES + 1;
   return d;
}
...
int turnAgain(int status)
---> you forget the left brace {
    char rollDie; // Conflict, rollDie redeclared

Compile with warnings:

                  die1 = rollDie();
                         ^
demo.c:60:10: note: declared here
     char rollDie; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM