繁体   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