gpt4 book ai didi

双骰子游戏-scanf自动输入巨数

转载 作者:行者123 更新时间:2023-11-30 18:12:27 25 4
gpt4 key购买 nike

我遇到问题的项目部分的说明是:

“玩”游戏:第二个函数playing()将用于玩一场双骰游戏,直到玩家根据上面给出的规则赢得或输掉赌注。该函数应该根据游戏结果修改玩家当前的资金金额,修改玩家赢或输的数组值,以及玩家是否对自己下注。在该函数中,玩家被询问是否愿意下注。如果是这样,玩家必须选择是“支持”还是“反对”自己(参见上面的游戏规则)。然后,玩家“掷骰子”(通过调用掷骰子函数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)来确定玩家的选择。

关于双骰子游戏-scanf自动输入巨数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36048235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com