簡體   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