gpt4 book ai didi

C - 无限循环

转载 作者:太空宇宙 更新时间:2023-11-04 03:27:07 27 4
gpt4 key购买 nike

此代码中的 for 循环永远不会结束并陷入无限循环,但我不明白为什么。在 for 循环中进行赋值时,我曾尝试使用临时变量代替 i,但这也不起作用。

指针指向大小为 10 的数组,其余代码在没有 for 循环的情况下运行良好。此外,计算机只会猜测 0。我猜这只是我的一个愚蠢错误,但我想不通。

编辑:只需将所有代码拉到帖子中

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void setBoard(int *board);
void setComputerBoard(int *board);
int playGame(int *human, int *computer);

int main() {
srand(time(NULL));

int humanBoard[10];
int *human = humanBoard;

int computerBoard[10];
int *computer = computerBoard;

setBoard(human);
setComputerBoard(computer);

int winner = playGame(human, computer);

if (winner == 0)
printf("Computer Wins!\n");
else
printf("Human Wins!\n");

return 0;
}

void setBoard(int *board) {
int pos1, pos2, i;
printf("Enter 1st position: ");
scanf("%d", &pos1);

while (pos1 > 9 || pos1 < 0) {
printf("Enter 1st position: ");
scanf("%d", &pos1);
}

printf("Enter 2nd position: ");
scanf("%d", &pos2);

while ((pos2 > 9) || (pos2 < 0) || (abs(pos2 - pos1) != 1)) {
printf("Enter 2nd position: ");
scanf("%d", &pos2);
}

for (i = 0; i < 10; i++) {
printf("%d", i);
*(board + i) = 0;
}

*(board + pos1) = 2;
*(board + pos2) = 2;
}

void setComputerBoard(int *board) {
int pos1 = rand() % 10, pos2;
if (pos1 == 9)
pos2 = pos1 - 1;
else
pos2 = pos1 + 1;

int i;
for (i = 0; i < 10; i++) {
if (i != pos1 && i != pos2)
*(board + i) = 0;
else
*(board + i) = 2;;
}
}

int playGame(int *human, int *computer) {
int winner = -1;
int computerShot, humanShot;
int computerHit = 0, humanHit = 0;
int i;

while (winner == -1) {
computerShot = humanShot = -1;

printf("Computer guesses ");
do {
computerShot = rand() % 10;
} while ((*(human + computerShot) != 1) && (*(human + computerShot) != 3));

printf("%d\n", computerShot);
if (*(human + computerShot) == 2) {
printf("HIT!\n");
*(human + computerShot) = 3;
computerHit++;
} else {
printf("MISS!\n");
*(human + computerShot) = 1;
}
printf("Human Board: \n");
printf("0 1 2 3 4 5 6 7 8 9\n");
for (i = 0; i < 10; i++) {
if (*(human + i) == 0)
printf("* ");
else
if (*(human + i) == 1)
printf("M ");
else
if (*(human + i) == 2)
printf("S ");
else
printf("H ");
}

printf("\nComputer Board: \n");
printf("0 1 2 3 4 5 6 7 8 9\n");
for (i = 0; i < 10; i++) {
if (*(computer + i) == 0)
printf("* ");
else
if (*(computer + i) == 1)
printf("M ");
else
if (*(computer + i) == 2)
printf("S ");
else
printf("H ");
}

printf("\nEnter guess: ");
scanf("%d", &humanShot);
printf("You guessed %d\n", humanShot);
if (*(computer + humanShot) == 2) {
printf("HIT!\n");
*(computer + humanShot) == 3;
humanHit++;
} else {
printf("MISS!\n");
*(computer + humanShot) == 1;
}

printf("Human Board: \n");
printf("0 1 2 3 4 5 6 7 8 9\n");
for (i = 0; i < 10; i++) {
if (*(human + i) == 0)
printf("* ");
else
if (*(human + i) == 1)
printf("M ");
else
if (*(human + i) == 2)
printf("S ");
else
printf("H ");
}
printf("\n");

printf("Computer Board: \n");
printf("0 1 2 3 4 5 6 7 8 9\n");
for (i = 0; i < 10; i++) {
if (*(computer + i) == 0)
printf("* ");
else
if (*(computer + i) == 1)
printf("M ");
else
if (*(computer + i) == 2)
printf("S ");
else
printf("H ");
}
printf("\n");
if (computerHit == 2)
winner = 0;
else
if (humanHit == 2)
winner = 1;
}
}

最佳答案

您的问题不是 for 循环,而是 playGame 函数中的 do-while 循环。 while条件需要改成

while ((*(human+computerShot == 1) || (*(human+computerShot) == 3));

因为当它等于 1 或 3(已经分别命中或未命中)时您想重​​做击球

关于C - 无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40288215/

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