簡體   English   中英

擲骰子游戲-scanf自動輸入大量數字

[英]Craps game - scanf automatically input huge number

我遇到問題的項目部分的說明是:

“玩”游戲:根據上面給出的規則,第二個函數play()將用於玩擲骰子的單局游戲,直到玩家贏得或失去下注。 此功能應根據游戲結果來修改玩家當前銀行存款的$金額,修改贏家或輸家的數組值,以及玩家是否為自己下注或下注。 在該功能內,詢問玩家他/她是否想下注。 如果是這樣,玩家必須選擇是“自己”下注還是“反對”自己下注(參見上面的游戲規則)。 然后,玩家“擲骰子”(通過調用擲骰子滾動函數rolling()模擬)。 這應該以交互方式完成(通過玩家的按鍵操作),而不是簡單地讓程序連續擲骰子直到游戲結束。 每次擲骰后,此函數應報告兩個隨機骰子值以及兩個值之和。 如果在第一輪擲骰后游戲仍未結束,則應詢問玩家是否希望將下注金額加倍。 當擲骰導致游戲結束時,會通知玩家該玩家在該游戲中總體上是贏還是賠,以及贏或輸的金額。 在所有其他情況下,都會通知玩家他/她需要再次滾動。

我必須在擲骰子游戲中編碼,該游戲要求用戶下注金額,然后他們才能玩擲骰子。 他們的初始銀行存款為100美元。 我必須使用scanf掃描下注金額,但是在沒有任何用戶輸入的情況下,它會使我的下注金額非常大。 有人可以幫我嗎,我今晚有這個項目。

#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       


//function prototype
int rolling(void);
int playing(int c_amt);//inital money $100, min bet of $5, no max except money     you have
void beginning(void);
void ending(void);

int
main()
{
printf("\nWelcome to Craps! Get ready to play!");
int bank_amt = 100;
char y_n = 'n';

do
{
  bank_amt = playing(bank_amt);

  if(bank_amt == 0)
    {
       printf("\nYou bet and lost all your money! You can't play anymore.  Goodbye!");
       exit(0);
    }
  if(bank_amt != 0)
    {
      printf("\nYour current balance is %d %d", &bank_amt, bank_amt);
      printf("\nDo you want to play again? (y/n): ");
      scanf("\n%c", &y_n);
    }
 }
 while(y_n != 'n' && y_n != 'N');

 }


int
rolling(void)
{
  int dice1, dice2;
  char roll;
  srand((int)(time(NULL))); // "Seed" random number gen. with system time

  printf("\nPress enter to roll the dice!");
  fflush(stdin);
  scanf("%c",&roll);
  dice1 = 1+rand()%6; // random num from 1-6
  dice2 = 1+rand()%6; // random num from 1-6

  return dice1+dice2;
}


  int
  playing(int c_amt)
  {
    char gametype, y_n = 'n';
    int total, point, for_u, against_u, winlose, win = 0, lose = 0;
    int moneychange, bet_amt, final_amt;

    printf("\nPlease press 'f' if you are betting for yourself and 'a' if your are betting against yourself.\n");
    scanf("\n%c", &gametype);

  do
   {
     printf("\nYour current bank balance is %d.", c_amt);
     printf("\nEnter the amount you want to bet: ");
     scanf(" %d", &bet_amt);
     printf("Bet amount: %d", bet_amt);

     if(bet_amt > c_amt || bet_amt < 5)
   {
        printf("\nYou dont have that much money or you placed a bet less than the minimum. Please place a proper bet.");
   }

   }
  while(bet_amt > c_amt);

    if (gametype == 'f' || gametype == 'F')
    {
      for_u++;
      printf("\nYou are betting for yourself!\nLets get started!");
      total = rolling();
      printf("\nThe value rolled is %d.", total);
      if (total == 7 || total == 11)
      {
        printf("\nGood job! You win :)");
        winlose = 1;
      }
      else if (total == 2 || total == 3 || total == 12)
      {
        printf("\nCraps, you lose.");
        winlose = 0;
      }
    else
    {
      point = total;
      printf("\nYour point is %d.", point);
      if(bet_amt*2 <= c_amt)
        {
          printf("\nWould you like to double your bet? (y/n)");
          scanf("\n%c", &y_n);
        }
          if(y_n == 'y' || y_n == 'Y')
          {
            bet_amt = bet_amt*2;
          }

      do
      {
          total = rolling();
          if(total == point)
            {
              printf("\nGood job! You win! :)");
              winlose = 1;
              break;
            }

      }
      while(total != 7);

     if(total == 7)
      {
      printf("You rolled a seven. You lose! :(");
      winlose = 0;
      }
    }
   }

  else if (gametype == 'a' || gametype == 'A')
   {
    against_u++;
    printf("You are betting against yourself!\nLet\'s get started!");
    total = rolling();
    printf("\nThe value rolled is %d.", total);
    if (total == 2 || total == 3 || total == 12
        {
        printf("Good job! You win :)\n");
        }
    else if (total == 7 || total == 11)
        {
        printf("Craps, you lose.\n");
        }
    else
        {
        point = total;
        printf("Your point is %d.\n", point);
        if(bet_amt*2 <= c_amt)
         {
         printf("\nWould you like to double your bet? (y/n)");
         scanf("\n%c", &y_n);
         }
        if(y_n == 'y' || y_n == 'Y')
         { 
         bet_amt = bet_amt*2;
         }

         do
         {
         total = rolling();
         if(total == 7)
           {
           printf("\nGood job! You win! :)");
           winlose=1;
           break;
           }

         }
         while(total != point);

         if(total == 7)
          {
          printf("You rolled a seven before making your point. You lose! :(");
          winlose = 0;
          }

          }
      }
          if(winlose ==  1)
            {
              final_amt = bet_amt + c_amt;
              win++;
            }
          else
            {
              final_amt = c_amt - bet_amt;
              lose++;
            }
        printf("Final amount is %d.", final_amt);
        return final_amt;
    }

這是示例輸出:

Welcome to Craps! Get ready to play!
Please press 'f' if you are betting for yourself and 'a' if your are betting against yourself.

Your current bank balance is 100.
Enter the amount you want to bet: Bet amount: -1219958512
You dont have that much money or you placed a bet less than the minimum. Please place a proper bet.Final amount is 1219958612.
Your current balance is -1074871812 1219958612
Do you want to play again? (y/n): 

您的格式需要大量修復才能理解。 但是,真正的問題是由於未向char分配足夠的內存而導致的緩沖區溢出。 相反,我建議使用簡單的int值(例如0和1)來確定玩家的選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM