我遇到问题的项目部分的说明是:
“玩”游戏: 根据上面给出的规则,第二个函数playing()将用于玩单个游戏的掷骰子,直到玩家赢或输。此功能应根据游戏结果修改玩家银行掷骰的当前$金额,修改玩家赢或输的数组值,以及玩家是否支持他/她自己。在该功能中,向玩家询问他/她是否想要下注。如果是这样,玩家必须选择是否“打击”或“反对”他/她自己(参见上面的游戏规则)。然后玩家“滚动骰子”(通过调用骰子滚动功能滚动()模拟)。这应该以交互方式完成(通过玩家的按键),而不是简单地让程序连续掷骰子直到游戏结束。每次滚动后,此函数应报告两个随机骰子值和两者的总和。如果在第一次掷骰后,游戏还未结束,则应询问玩家是否希望将下注金额加倍。当掷骰引起游戏结束时,玩家将被告知他/她是否在该游戏中赢得或赔钱,以及赢或输的数额。在所有其他情况下,通知玩家他/她需要再次滚动。
我必须在掷骰子游戏中编码,询问用户下注金额,然后他们玩掷骰子。他们的初始银行存款为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):
答案 0 :(得分:-1)
您的格式化需要大量修复才能理解。但是,真正的问题是缓冲区溢出,这是由于没有为char
分配足够的内存而导致的。相反,我建议使用简单的int
值(如0和1)来确定玩家的选择。