2016-10-11 6 views
2

ユーザーにyまたはnの入力を求めていると、ゲームが終了するか、続行されます。私はまた、総勝利を表示したいとルーズとユーザーが終了します。たぶん私は機能のブール値と返品の本当の手を得ていないのですか?クラップスのゲーム

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

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

int main(void) 
{ 
    srand((unsigned)(time(NULL))); 
    char userInput; 
    while (true) 
    { 
     playGame(); 
     printf("Would you like to play again?"); 
     scanf("%c", &userInput); 
     if (userInput == 'n' || userInput == 'N') 
     { 
      return false; 
     } 
     else 
     { 
      return true; 
     } 
    } 
    return 0; 
} 
int rollDice(void) 
{ 

    int dice1 = rand()%6+1; 
    int dice2 = rand()%6+1; 
    int totaldice = dice1 + dice2; 

    return totaldice; 
} 
bool playGame(void) 
{ 
    int point, total; 
    int winCounter, looseCounter; 
    printf("The game is starting!\n"); 
    total = rollDice(); 
    printf("You rolled: %d\n", total); 
    if (total == 7 || total == 11) 
    { 

     printf("Wow it's your lucky day! You Win!\n"); 
     winCounter++; 

    } 
    else if (total == 2 || total == 3 || total == 12) 
    { 
     printf("Unlucky! You Loose!\n"); 
     looseCounter++; 
    } 
    else { 
     point = total; 
     printf("Your Point is: %d\n", point); 
     while (true) 
     { 
      total = rollDice(); 
      printf("You rolled: %d\n", total); 
      if (total == point) 
      { 
       printf("You made your point! You Win!\n"); 
       winCounter++; 
       break; 
      } 
      else if (total == 7) 
      { 
       printf("Thats a %d. You Loose!\n", total); 
       looseCounter++; 
       break; 
      } 
     } 

    }return true; 
} 
+3

'のscanf(」 %c "、&userInput);' - > 'scanf("%c "、&userInput);' – LPs

+0

'int winCounter、looseCounter;' - > 'int winCounter = 0、looseCounter = 0;' – LPs

+0

' ' - >' // trueを返します; ' – LPs

答えて

0

あなたの主な問題は、'n'または'N'異なるユーザ入力何かの場合には、あなたがそれinstruction.Remove returnでメインを終了し、ループを続けることができるということです。

より良いあなたは、whileループを終了するには、ブール変数を使用する場合:

int main(void) 
{ 
    srand((unsigned)(time(NULL))); 
    char userInput; 

    bool paygame = true; 

    while (paygame) 
    { 
     playGame(); 
     printf("Would you like to play again?"); 
     scanf(" %c", &userInput); 

     printf ("Test: %c\n", userInput); 
     if (userInput == 'n' || userInput == 'N') 
     { 
      paygame = false; 
     } 
    } 
    return 0; 
} 

第二の大きな問題は、playgame機能のカウンタです:彼らはそれ以外の場合は0

int winCounter = 0, looseCounter = 0; 

にinitedする必要があります乱数からカウントを開始します。

あなたがカウントしたい場合は、すべてが勝つと、すべてのプレイされるゲームの緩いあなたは、単に静的VARSを使用することができます。scanfは「フラッシュ」できるようにするために

bool playGame(void) 
{ 
    int point, total; 
    static int winCounter = 0, looseCounter = 0; 
    printf("The game is starting!\n"); 
    total = rollDice(); 
    printf("You rolled: %d\n", total); 
    if (total == 7 || total == 11) 
    { 

     printf("Wow it's your lucky day! You Win!\n"); 
     winCounter++; 

    } 
    else if (total == 2 || total == 3 || total == 12) 
    { 
     printf("Unlucky! You Loose!\n"); 
     looseCounter++; 
    } 
    else { 
     point = total; 
     printf("Your Point is: %d\n", point); 
     while (true) 
     { 
      total = rollDice(); 
      printf("You rolled: %d\n", total); 
      if (total == point) 
      { 
       printf("You made your point! You Win!\n"); 
       winCounter++; 
       break; 
      } 
      else if (total == 7) 
      { 
       printf("Thats a %d. You Loose!\n", total); 
       looseCounter++; 
       break; 
      } 
     } 

    } 

    printf ("Won: %d - Lose: %d\n", winCounter, looseCounter); 

    return true; 
} 

最後の事は" %c"scanf書式指定子を変更改行'\n' CHARユーザーが入力するたびにstdinに入ります。

0

whileループではリターンを使用しません。代わりに、変数を使用して、条件として使用します。また、何もあなたが押されるまで、すべての時間以来、条件が真である、それは本当のようにする必要はありませんnまたはN

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

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

int main(void) 
{ 
    srand((unsigned)(time(NULL))); 
    char userInput; 
    bool again = true; 
    while (again==true) 
    { 
     printf("Would you like to play again?"); 
     scanf("%c", &userInput); 
     if (userInput == 'n' || userInput == 'N') 
     { 
      again = false; 
     } 
    } 
    return 0; 
} 
0

あなたは、ユーザーの後に合計を表示したい場合は、playGame機能の外にそれらを格納する必要が終了します。

playGameからの戻り値は、現時点では無意味なので、のは、プレイヤーが勝ったかどうかを示すためにそれを使用してみましょう:

bool playGame(void) 
{ 
    int point, total; 
    printf("The game is starting!\n"); 
    total = rollDice(); 
    printf("You rolled: %d\n", total); 
    if (total == 7 || total == 11) 
    { 
     printf("Wow it's your lucky day! You Win!\n"); 
     return true; 
    } 
    else if (total == 2 || total == 3 || total == 12) 
    { 
     printf("Unlucky! You Lose!\n"); 
     return false; 
    } 
    else 
    { 
     point = total; 
     printf("Your Point is: %d\n", point); 
     while (true) 
     { 
      total = rollDice(); 
      printf("You rolled: %d\n", total); 
      if (total == point) 
      { 
       printf("You made your point! You Win!\n"); 
       return true; 
      } 
      else if (total == 7) 
      { 
       printf("Thats a %d. You Lose!\n", total); 
       return false; 
      } 
     } 

    } 
    return false; 
} 

そしてmainのわずかな書き換え:

int main(void) 
{ 
    srand((unsigned)(time(NULL))); 
    int total = 0; 
    int wins = 0; 
    char userInput; 
    while (true) 
    { 
     total += 1; 
     if (playGame()) 
     { 
      wins += 1; 
     } 
     printf("Would you like to play again?"); 
     scanf("%c", &userInput); 
     if (userInput == 'n' || userInput == 'N') 
     { 
      break; 
     } 
    } 
    printf("Of %d games, you won %d.", total, wins); 
    return 0; 
} 
+0

こんにちは@molbdniloちょっと簡単な質問です。私のテキストブックでは、bool(void)を使う必要があります。関数の最後にfalseを返すとき、実際に何をしていますか?私は関数の終わりに偽または真の戻り値の違いを見ません.... – xxFlashxx

関連する問題