#include <stdio.h>
#include <math.h>
#include <time.h>
#define FILENAME "dice_rdme.txt"
int main (void)
{
//prototypes
int dice(int sets);
void print_info(void);
int dicereadback(void);
//title
printf("Commence gamble, test your might \n");
int sets;
srand(time(0));
// user input dialogue
printf("Enter the number of rolls to wager \n");
scanf("%i", &sets);
dice(sets);
dicereadback();
print_info();
return;
}
//general print info
void print_info (void)
{
printf("\n student info \n");
printf("class professor and ta \n ");
printf("HOMEWORK ASSIGNMENT#6 DICE GAME 2.0 \n");
return;
}
//generate games, parse outcome, store
int dice (int sets)
{
FILE * dice_rdme;
int a, b;
int plyrsum = 0;
int j = 0;
int k = 0;
int d, f, dlrsum;
int c[sets], g[sets];
int plyrset = sets;
int dlrset = sets;
dice_rdme = fopen("dice_rdme.txt","w");
for (a=0; a<plyrset; a++)
{
b = 1 + rand() %6;
c[j] = b;
plyrsum += c[j];
fprintf(dice_rdme,"%i ", c[j]);
++j;
// if (b=6)
// a--;
}
if (a == plyrset)
{
fprintf(dice_rdme, "player score = %i \n", plyrsum);
}
for (d=0; d<dlrset; d++)
{
f = 1 + rand() %6;
g[k] = f;
dlrsum += g[k];
fprintf(dice_rdme,"%i ", g[k]);
++k;
// if (f=6)
//d--;
}
if (d == dlrset)
fprintf(dice_rdme, " dealer score = %i \n", dlrsum);
if (c>g)
fprintf(dice_rdme, " \n You win! \n");
if (g>c)
fprintf(dice_rdme, " \n You lose\n ");
fclose(dice_rdme);
return(0);
}
//games are done, spit em back to stdout
int dicereadback(void)
{
FILE * dice_rdme;
char buff[1000];
dice_rdme = fopen("dice_rdme.txt","r");
while(fgets(buff,1000,dice_rdme)!=NULL)
printf("%s",buff);
fclose(dice_rdme);
return(0);
}
在我将 plyrsum 更改为 = 0 作为定义之前,我遇到了以下错误,我不明白这些值来自哪里。玩家分数似乎已默认或用作占位符或地址作为整数(?捕获救命稻草?)
kettingstad@esc_151:~/workspace $ gcc HW6.c -lm -o hw6test.o
kettingstad@esc_151:~/workspace $ ./hw6test.o
Commence gamble, test your might
Enter the number of rolls to wager
3
6 2 4 player score = 32779
4 6 3 dealer score = 13
You lose, here are some pamphlets for gambling addiction treatment....
kettingstad@esc_151:~/workspace $ ./hw6test.o
Commence gamble, test your might
Enter the number of rolls to wager
3
4 3 5 player score = 32779
2 1 4 dealer score = 7
这里我将 plyrsum 预定义为 = 0,但我仍然无法解决让我的函数在每次玩家掷出 6 时添加额外掷骰的问题。我尝试使用嵌套在每个 for 中的 if 语句循环中为每个玩家生成结果,但我总是遇到 8 GB 的 .txt 文件和一个挂起的程序。该作业要求从 txt 文件中更复杂地读回分数,但我创建了现有函数,以便我可以测试函数“int dice(int set)”
kettingstad@esc_151:~/workspace $ gcc HW6.c -lm -o hw6test.o
kettingstad@esc_151:~/workspace $ ./hw6test.o
Commence gamble, test your might
Enter the number of rolls to wager
6
6 4 2 6 1 player score = 25
1 6 3 3 1 6 dealer score = 20
You win!
`
最佳答案
有多种方法可以做到这一点。基本上,您只想设置一个标志 if 6
发生时,保存当前sum
,再次滚动,然后 take the higher of the two rolls
。实现这一点的一种方法是:
int eroll = 0; /* flag for extra roll (when b = 6) */
int esum = 0; /* extra sum (to keep greater of 2 rolls) */
dice_rdme = fopen ("dice_rdme.txt", "w");
do {
eroll = 0; /* reset eroll */
for (a = 0; a < plyrset; a++) {
b = 1 + rand () % 6;
c[j] = b;
plyrsum += c[j];
fprintf (dice_rdme, "%i ", c[j]);
++j;
if (b == 6) {
eroll = 1; /* set eroll flag */
}
}
if (eroll) {
esum = plyrsum; /* save plyrsum */
fprintf (dice_rdme, "EXTRA ROLL! current sum %i \n", plyrsum);
}
plyrsum = (plyrsum > esum) ? plyrsum : esum; /* keep best roll */
} while (eroll);
输出:
$ ./bin/gameeroll
Commence gamble, test your might
Enter the number of rolls to wager
3
6 2 3 EXTRA ROLL! current sum 11
4 3 3 player score = 21
2 2 3 dealer score = 6
You win!
student info
class professor and ta
HOMEWORK ASSIGNMENT#6 DICE GAME 2.0
注意:额外的fprintf
只是为了测试而插入的。
正如已经指出的,您所有的等价物 if (a = whatever)
需要if (a == whatever)
。您将需要验证代码中的其余逻辑。 C 是一种精确语言,请花点时间思考每一行。
关于C编程101 : Dice game Assignment that adds an extra turn when players roll a 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26899842/