繁体   English   中英

骰子游戏的功能帮助

[英]Function Help on Dice Game

我有一个新问题。 我修复了大部分问题。 我需要修复我的 (turnAgain) 功能。 我特别需要修复我的 if else 语句。 如果他们输入'r',我需要它让玩家继续滚动。 否则,如果他们输入“h”,其他玩家就可以滚动。 我需要它说如果他们输入的不是 'r' 或 'h' 的东西,请再试一次输入 'r' 或 'h'。 我只是不知道该怎么做。 编码新手谢谢!! 我真的 NEEEEEEEEEEEEEEEEEEEEEE 帮助。

#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

编译警告:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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