2015-09-25 113 views
0

我在骰子游戏中遇到麻烦。我有一个任务:C程序骰子游戏

游戏的规则如下: 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

返回()

3由兰特产生的数目)实施的功能,与函数原型int琐事(空隙);

当播放机准备好打,他(她)会用Enter键掷骰子

如果用户胜在他/她的第一个卷,祝贺播放机和带WON

返回

如果用户在第一次掷骰子时放松,祝贺玩家,并用LOSE返回

让用户继续游戏直到他赢/输,直到他/她输掉一个合适的按摩并以最后一次掷骰值结束游戏。

4)你的main()应该 调用函数琐事()

询问用户是否他(她)想要继续玩另一个游戏,保持损失的数字轨迹,赢得

当用户决定完成比赛时,显示他所赢得的比赛数量。

给用户根据胜或负他(她)有

返回与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):

x.c:9:1:错误:杂散“\ 302”在程序

INT rollDice(无效);

^

x.c:9:1:错误:杂散“\ 240”在程序

X。c:10:1:错误:在程序中丢失'\ 302'

int playGame(void);

^

XC:10:1:错误:杂散 '\ 240' 在程序

XC:12:1:错误:杂散 '\ 302' 在程序

INT rollDice(空隙){

^

XC:12:1:错误:杂散 '\ 240' 在程序

XC:16:1:错误:16:1:错误:杂散 '\ 240' 在程序在程序

INT琐事(无效){

^

XC杂散 '\ 302'

+1

作为题外话,因为没有“做我的家工作“网站。 –

+0

我投票关闭这个问题 – Olaf

+1

什么不行? – xxbbcc

回答

1

这里有一些错误。

  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. 
看来你什么都没有,现在...
+0

谢谢!!!你救了我的生命 – AnaF

+1

@AnaF没问题,如果这能够充分回答你的问题问题,请检查问题顶部附近的复选标记,并考虑提升它。 – DevNull

+0

@Dogbert - 你为他们做了功课。 – PhillyNJ

1

你还没有满足游戏的规则。您的代码需要7和11作为获胜者,并将其他任何数字作为失败者。

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

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

+0

谢谢!我不'我真的明白如何在main – AnaF

+0

@AnaF中调用playGame(),你需要做的是从'playGame'返回'WIN'或'LOSE',并在'main'中捕获它,更新相应的计数器 – dbush