gpt4 book ai didi

C文本游戏段错误及调试

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:50 25 4
gpt4 key购买 nike

我想用 C 编写一个简单的文字游戏。该游戏使用范围从 -10.000 到 10.000 (X, Y) 的 2 个 double 变量生成随机二维位置作为“宝藏”位置。玩家从位置 0.000、0.000 开始。然后游戏询问玩家在 X 和 Y 的哪个方向。之后游戏需要给玩家一个简单的提示,如果他越来越近(正确或错误的方向取决于宝藏位置)。当玩家在任何方向距离宝藏小于 0.050 时,游戏结束并打印出分数(循环步数)。

我写了这篇文章,但我遇到了“段错误”,我也很想知道您将如何修改它以使其更有效和更实用。

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

int main()
{
srand (time(NULL));
char *direction_x;
char *direction_y;
double direction_forward;
double direction_back;
double direction_left;
double direction_right;
double score_x;
double score_y;
double diff_x;
double diff_y;
double lastmove_x;
double lastmove_y;
int i;



double random_x = (double)rand()/RAND_MAX*20.000-10.000;
double rounded_x = (int)(random_x * 1000.0)/1000.0;
printf ( "%f\n", random_x);
printf ( "%.3f\n", rounded_x);


double random_y = (double)rand()/RAND_MAX*20.000-10.000;
double rounded_y = (int)(random_y * 1000.0)/1000.0;
printf ( "%f\n", random_y);
printf ( "%.3f\n", rounded_y);

double player_x=0.000;
double player_y=0.000;
printf("Hello freind! Let's find the hidden treasure!\n");

do{
i++;
printf("Where would you want to go? ('straight', 'back')\n");

scanf("%s", &direction_x);
if (strcmp(direction_x, "straight") == 0)
{
printf("How far?\n");
scanf("%f", &direction_forward);
player_x = player_x + direction_forward;
printf("You've walked %.3f straight!\n", direction_forward);
printf("Your current postion is: %.3f, %.3f.\n", player_x, player_y);
diff_x = random_x - player_x;
if (diff_x < random_x)
{
printf("You're closer...\n");
}
else
{
printf("Don't like this way...\n");
}
}

else if (strcmp(direction_x, "back") == 0)
{
printf("How far?\n");
scanf("%f", &direction_back);
player_x = player_x - direction_back;
printf("You've walked %.3f backwards!\n", direction_back);
printf("Your current position is: %.3f, %.3f.\n", player_x, player_y);
if (diff_x < random_x)
{
printf("You're closer...\n");
}
else
{
printf("Don't like this way...\n");
}
}
else
{
printf("Don't accept this direction...\n");
}
printf("Where now? ('left', 'right')\n");

scanf("%s", &direction_y);
if (strcmp(direction_y, "left") == 0)
{
printf("How far?\n");
scanf("%f", &direction_left);
player_y = player_y + direction_left;
printf("You've walked %.3f left!\n", direction_left);
printf("Your current position is: %.3f, %.3f.\n", player_x, player_y);
if (diff_y < random_y)
{
printf("You're closer...\n");
}
else
{
printf("Don't like this way...\n");
}
}

else if (strcmp(direction_y, "right") == 0)
{
printf("How far?\n");
scanf("%f", &direction_right);
player_y = player_y - direction_right;
printf("You've walked %.3f right!\n", direction_right);
printf("Your current position is: %.3f, %.3f.\n", player_x, player_y);
if (diff_y < random_y)
{
printf("You're closer...\n");
}
else
{
printf("Don't like this way...\n");
}
}
else
{
printf("Don't accept this direction...\n");
}

score_x = (player_x + 0.050) - random_x;

score_y = (player_y + 0.050) - random_y;


}while ((-0.050 <= score_x <= 0.050) || (-0.050 <= score_y <= 0.050));

printf("Congratulations, treasure was finally founded! Treasure position is %.3f, %.3f.\n", random_x, random_y);
printf("Score (steps taken, less is better): %d\n", i);
return 0;

最佳答案

我只是想编译你的代码。

首先:如果你使用scanf(),你应该指定"%lf",如果你想读取一个double。例如:

scanf("%lf", &direction_right);

第二:你的很多变量都没有初始化。第一个段错误可能会在您的 while 循环开始时抛出:

scanf("%s", &direction_x);

因为 direction_x 没有被初始化,所以它存储了一个具有随机值的指针。这意味着,它指向您内存中的某个位置,具体取决于先前存储在 direction_x 所在位置的值。这可能会导致崩溃。

通过像这样声明一个数组来避免这种情况。

char direction_x[42];
char direction_y[42];

但不要忘记检查您的输入长度!

在初始化它们之前,您还可以使用更多的变量。通过添加标志 -Wall(如果您使用的是 gcc)来检查您的编译器警告。


第三:尝试构建代码。 if-else 语句中的任何内容都可以放入自己的函数中。这将帮助您和任何其他试图理解您的代码的人。

关于C文本游戏段错误及调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53062905/

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