繁体   English   中英

C程序骰子游戏

[英]C program dice game

我的骰子游戏遇到了麻烦。 我有一个任务:

游戏规则如下:1.玩家掷骰子并加总面值。 2.如果第一轮为7或11,则玩家获胜。 3.如果第一卷为2、3或12,则玩家会放松。 4.如果第一卷为任何其他数字,则该总和成为玩家的分数。 5.要赢得胜利,玩家必须继续掷骰子,直到他/她“取得积分”为止。6.玩家在掷骰子之前掷出7便输了。

1)在程序中将WON和LOST定义为宏。 对于WON使用0值,对于LOSE使用1值。2)实现一个函数,函数原型为int rollDice(void);

rollDice()应该使用rand()随机生成一个介于1到6之间的数字

返回rand()生成的数字

3)实现一个函数,函数原型为int playGame(void);

当玩家准备好玩时,他将使用Enter键来掷骰子

如果用户赢得了他的第一个掷骰,则祝贺玩家并以WON返回

如果用户松开第一卷,则向玩家表示祝贺并返回LOSE

让用户继续玩,直到赢/输,给予适当的按摩,并以最后的掷骰值结束游戏。

4)您的main()应该调用您的函数playGame()

询问用户是否要继续玩另一游戏,跟踪损失和获胜的次数

当用户决定结束播放时,显示他的获胜次数。

根据用户获胜或失败的次数向用户提供适当的消息

返回值为EXIT_SUCCESS

这就是我现在所拥有的,但是它告诉我有错误。 谁能帮我完成这个任务?

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

#define WON 0
#define LOSE 1

int rollDice(void);
int playGame(void);

int rollDice(void) {
    return ((rand() % 6) + 1);
}

int playGame(void){
  int dice_1 = 0;
  int dice_2 = 0;
  int sum = 0;
  time_t t;
  srand(time(&t));
  printf("ROLL THE DICE WITH [ENTER]\n");
  dice_1 = rollDice();
  dice_2 = rollDice();
  sum = dice_1 + dice_2;
  if (sum == 7 || sum == 11){
   printf("Congratulations you roll %d and WON at your first try!", sum);
  }
  else {
    printf("Your roll was %d ,you lose try agian.\n", sum);
  }
  return 0;
}

int main (void){
  playGame();
}

错误是(在gcc linux中):

xc:9:1:错误:程序中出现杂散'\\ 302'

int rollDice(void);

^

xc:9:1:错误:程序中流浪了'\\ 240'

xc:10:1:错误:程序中出现杂散'\\ 302'

int playGame(void);

^

xc:10:1:错误:程序中流浪了'\\ 240'

xc:12:1:错误:程序中杂散了\\ 302

int rollDice(void){

^

xc:12:1:错误:程序中杂散了\\ 240

xc:16:1:错误:程序中出现杂散'\\ 302'

int playGame(void){

^

xc:16:1:错误:程序中杂散了\\ 240

您尚未满足游戏规则。 您的代码将7和11视为赢家,将其他任何数字视为输家。

在7/11检查之后,您需要检查2、3或12,如果为true,则打印“ lose”消息。 如果没有,您将获得点数,并且需要提示用户继续滚动,直到他获得点数(胜利)或7(失败)为止。

您还需要跟踪main的赢/输。 您需要循环调用playGame ,提示用户继续进行每次迭代。

这里有些错误。

  1. 您不是在读取/使用playGame()的返回值。 您应该存储结果并对其进行操作。
  2. 您的逻辑尚不完整,因为“争分夺秒”和亏损的标准是相同的。
  3. 您没有任何地方可以迫使程序等待用户按下ENTER键

我在下面为您提供了完整的代码清单。

代码清单


/*******************************************************************************
 * Preprocessor directives
 ******************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>

#define WON 0
#define LOSE 1


/*******************************************************************************
 * Function prototypes
 ******************************************************************************/
int rollDice(void);
int playGame(void);


/*******************************************************************************
 * Function definitions
 ******************************************************************************/
/*----------------------------------------------------------------------------*/
int rollDice(void) {
    return ((rand() % 6) + 1);
}

/*----------------------------------------------------------------------------*/
int playGame(void){
    int dice_1 = 0;
    int dice_2 = 0;
    int sum = 0;
    int result;
    int point = 0;
    time_t t;
    bool playForPoint = false;

    srand(time(&t)); // Initialize random seed
    printf("ROLL THE DICE WITH [ENTER]\n");
    fgetc(stdin);
    dice_1 = rollDice();
    dice_2 = rollDice();
    sum = dice_1 + dice_2;
    printf("D1:%2d - D2:%2d - Sum:%2d\n", dice_1, dice_2, sum);

    switch ( sum )
    {
        case 7:
        case 11:
            result = WON;
            break;
        case 2:
        case 3:
        case 12:
            result = LOSE;
            break;
        default:
            playForPoint = true;
            point = sum;
            printf("Playing for point:%d. Please hit enter.\n", point);
            fgetc(stdin);
            break;
    }

    while ( playForPoint )
    {
        dice_1 = rollDice();
        dice_2 = rollDice();
        sum = dice_1 + dice_2;
        printf("D1:%2d - D2:%2d - Sum:%2d\n", dice_1, dice_2, sum);
        if ( sum == 7 ) {
            playForPoint = false;
            result = LOSE;
        } else if ( sum == point ) {
            playForPoint = false;
            result = WON;
        } else {
            printf("Please roll the dice again with [ENTER].\n");
            fgetc(stdin);
        }
    }

    return result;
}

/*----------------------------------------------------------------------------*/
int main (void){
    int result = playGame();
    switch ( result )
    {
        case WON:
            printf("You won the game.\n");
            break;
        case LOSE:
            printf("You lost the game.\n");
            break;
        default:
            printf("Something went wrong in the program.\n");
            break;
    }

    return 0;
}

样本输出


ROLL THE DICE WITH [ENTER]

D1: 3 - D2: 5 - Sum: 8
Playing for point:8. Please hit enter.

D1: 3 - D2: 1 - Sum: 4
Please roll the dice again with [ENTER].

D1: 3 - D2: 2 - Sum: 5
Please roll the dice again with [ENTER].

D1: 1 - D2: 5 - Sum: 6
Please roll the dice again with [ENTER].

D1: 3 - D2: 2 - Sum: 5
Please roll the dice again with [ENTER].

D1: 2 - D2: 6 - Sum: 8
You won the game.

暂无
暂无

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

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