C 中的垃圾游戏
Crap games in C
我正在尝试制作一个掷骰子游戏,其中会掷骰子来检查它是否命中目标。我不能确定我的 while
循环有什么问题,但即使 while
循环要求为假,它也会保持 运行。这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int rollDice() {
int sum, d1, d2;
sum = 0;
d1 = (rand() % 6) + 1;
d2 = (rand() % 6) + 1;
sum = d1 + d2;
printf("Player rolled %d + %d = %d\n", d1, d2, sum);
return sum;
}
int main() {
int player, target, tries;
srand(time(NULL));
tries = 0;
player = rollDice();
target = player;
if (player == 7 || player == 11) {
printf("Player wins.\n");
}
else if (player == 2 || player == 3 || player == 12) {
printf("Player loses.\n");
} else {
printf("Point is %d. The game continues: \n", target);
player = rollDice();
}
printf("Player is %d, Target is %d.\n", player, target);
while (target != player || player != 7) {
player = rollDice();
}
return 0;
}
当掷出 7
或再次掷出玩家分数时,游戏结束。因此 while
测试应该是 while (target != player && player != 7)
,而不是 while (target != player || player != 7)
.
您还必须将 while
循环移动到最后一个 else
块中。
这是修改后的版本:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int rollDice(void) {
int sum, d1, d2;
sum = 0;
d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
sum = d1 + d2;
printf("Player rolled %d + %d = %d\n", d1, d2, sum);
return sum;
}
int main() {
int player, target, tries;
srand(time(NULL));
tries = 0;
player = rollDice();
target = player;
if (player == 7 || player == 11) {
printf("Player wins.\n");
} else
if (player == 2 || player == 3 || player == 12) {
printf("Player loses.\n");
} else {
printf("Point is %d. The game continues:\n", target);
player = rollDice();
printf("Player is %d, Target is %d.\n", player, target);
for (;;) {
if (player == 7) {
printf("Pass loses\n");
break;
}
if (player == target) {
printf("Pass wins\n");
break;
}
player = rollDice();
}
}
return 0;
}
我正在尝试制作一个掷骰子游戏,其中会掷骰子来检查它是否命中目标。我不能确定我的 while
循环有什么问题,但即使 while
循环要求为假,它也会保持 运行。这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int rollDice() {
int sum, d1, d2;
sum = 0;
d1 = (rand() % 6) + 1;
d2 = (rand() % 6) + 1;
sum = d1 + d2;
printf("Player rolled %d + %d = %d\n", d1, d2, sum);
return sum;
}
int main() {
int player, target, tries;
srand(time(NULL));
tries = 0;
player = rollDice();
target = player;
if (player == 7 || player == 11) {
printf("Player wins.\n");
}
else if (player == 2 || player == 3 || player == 12) {
printf("Player loses.\n");
} else {
printf("Point is %d. The game continues: \n", target);
player = rollDice();
}
printf("Player is %d, Target is %d.\n", player, target);
while (target != player || player != 7) {
player = rollDice();
}
return 0;
}
当掷出 7
或再次掷出玩家分数时,游戏结束。因此 while
测试应该是 while (target != player && player != 7)
,而不是 while (target != player || player != 7)
.
您还必须将 while
循环移动到最后一个 else
块中。
这是修改后的版本:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int rollDice(void) {
int sum, d1, d2;
sum = 0;
d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
sum = d1 + d2;
printf("Player rolled %d + %d = %d\n", d1, d2, sum);
return sum;
}
int main() {
int player, target, tries;
srand(time(NULL));
tries = 0;
player = rollDice();
target = player;
if (player == 7 || player == 11) {
printf("Player wins.\n");
} else
if (player == 2 || player == 3 || player == 12) {
printf("Player loses.\n");
} else {
printf("Point is %d. The game continues:\n", target);
player = rollDice();
printf("Player is %d, Target is %d.\n", player, target);
for (;;) {
if (player == 7) {
printf("Pass loses\n");
break;
}
if (player == target) {
printf("Pass wins\n");
break;
}
player = rollDice();
}
}
return 0;
}