简体   繁体   English

掷骰子游戏-scanf自动输入大量数字

[英]Craps game - scanf automatically input huge number

The instructions for the part of the project I am having trouble with are: 我遇到问题的项目部分的说明是:

"Playing" the game: A second function playing() will be used to play a single game of craps until the player either wins or loses a bet, based upon the rules given above. “玩”游戏:根据上面给出的规则,第二个函数play()将用于玩掷骰子的单局游戏,直到玩家赢得或失去下注。 This function should be modify the current $ amount of the player's bank roll according to the game result, modify the array values of the player won or lost, and whether the player bet for or against him/herself. 此功能应根据游戏结果来修改玩家当前银行存款的$金额,修改赢家或输家的数组值,以及玩家是否为自己下注或下注。 Within the function, the player is asked whether s/he would like to place a bet. 在该功能内,询问玩家他/她是否想下注。 If so, the player must choose whether to bet "for" or "against" him/herself (see game rules above). 如果是这样,玩家必须选择是“自己”下注还是“反对”自己下注(参见上面的游戏规则)。 The player then "rolls the dice" (simulated by a call to the dice-rolling function rolling()). 然后,玩家“掷骰子”(通过调用掷骰子滚动函数rolling()模拟)。 This should be done interactively (via a key press from the player), rather than simply having the program continuously roll the dice until the game is over. 这应该以交互方式完成(通过玩家的按键操作),而不是简单地让程序连续掷骰子直到游戏结束。 After each roll, this function should report the two random dice values and the sum of the two. 每次掷骰后,此函数应报告两个随机骰子值以及两个值之和。 If, after the first roll, the game is not over, the player should be asked whether s/he would like to double the amount of the bet. 如果在第一轮掷骰后游戏仍未结束,则应询问玩家是否希望将下注金额加倍。 When a roll causes the end of a game, the player is notified whether s/he won or lost money in that game overall, and the amount won or lost. 当掷骰导致游戏结束时,会通知玩家该玩家在该游戏中总体上是赢还是赔,以及赢或输的金额。 In all other cases, the player is notified that s/he needs to roll again. 在所有其他情况下,都会通知玩家他/她需要再次滚动。

I have to code in a craps game that asks user for a bet amount and then they play craps. 我必须在掷骰子游戏中编码,该游戏要求用户下注金额,然后他们才能玩掷骰子。 They have an initial bank roll of $100. 他们的初始银行存款为100美元。 I have to scan a bet amount in using scanf but it keeps making my bet amount very large without any user input. 我必须使用scanf扫描下注金额,但是在没有任何用户输入的情况下,它会使我的下注金额非常大。 Can someone please help me, I have this project due tonight. 有人可以帮我吗,我今晚有这个项目。

#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;
    }

Here is sample output: 这是示例输出:

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): 

Your formatting needs a lot of fixing to understand. 您的格式需要大量修复才能理解。 However, the real issue is a buffer overflow, caused by not allocating enough memory to the char . 但是,真正的问题是由于未向char分配足够的内存而导致的缓冲区溢出。 Instead, I would suggest using simple int values like 0 and 1 to determine the player's choices. 相反,我建议使用简单的int值(例如0和1)来确定玩家的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM