我想写一个简单的文字游戏C,游戏产生随机的2D位置,使用两个双变量范围从-10.000到10.000 (X,Y)作为“宝藏”位置。玩家从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;}
发布于 2018-10-30 13:00:35
char *direction_x;和char *direction_y;应该像char st[1024]一样用于scanfi必须在使用它之前插入它scanf("%s", &direction_x);应该是scanf("%s", direction_x);double,应该使用%lf,而不是%f-0.050 <= score_x <= 0.050应该是-0.050 <= score_x && score_x <= 0.050等等..。
我重写了。
下面的code可以工作:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
int main()
{
srand (time(NULL));
double random_x = (double)rand()/RAND_MAX*20.000-10.000;
double random_y = (double)rand()/RAND_MAX*20.000-10.000;
double player_x=0.000;
double player_y=0.000;
double score_x;
double score_y;
int step = 0;
printf("Hello friend! Let's find the hidden treasure!\n");
do {
step++;
char st[1024];
double data;
printf("Where would you want to go? (horizontal or vertical)\n");
scanf("%s", st);
printf("How far?\n");
scanf("%lf", &data);
if (strcmp(st, "horizontal") == 0)
{
player_x = player_x + data;
printf("You've walked %.3f left!\n", data);
printf("Your current position is: %.3f, %.3f.\n", player_x, player_y);
if (fabs(player_x - random_x) < fabs(player_x - data - random_x))
printf("You're closer...\n");
else
printf("Don't like this way...\n");
}
else if (strcmp(st, "vertical") == 0)
{
player_y = player_y + data;
printf("You've walked %.3f down!\n", data);
printf("Your current position is: %.3f, %.3f.\n", player_x, player_y);
if (fabs(player_y - random_y) < fabs(player_y - data - random_y))
printf("You're closer...\n");
else
printf("Don't like this way...\n");
} else {
continue;
}
score_x = player_x - random_x;
score_y = player_y - random_y;
} while (fabs(score_x) > 0.050 && fabs(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", step);
return 0;
}发布于 2018-10-30 11:44:40
我只是试着编译你的代码。
第一:如果使用scanf(),如果要以双读方式读取,则应指定"%lf"。例如:
scanf("%lf", &direction_right);第二:许多变量没有初始化。第一个分段错误可能会在该行的while-循环开始时抛出:
scanf("%s", &direction_x);
因为没有初始化direction_x,所以它存储一个带有随机值的指针。这意味着它指向内存中的某个位置,这取决于以前存储在direction_x所在位置的值。这很可能导致坠机。
通过声明类似的数组来避免这种情况。
char direction_x[42];
char direction_y[42];但别忘了检查你的输入长度!
在初始化这些变量之前,需要使用更多的变量。通过添加标志-Wall (如果使用gcc)检查编译器警告。
第三:尝试构造代码。if- own语句中的任何内容都可以放入自己的函数中。这将帮助您和其他试图理解您的代码的人。
https://stackoverflow.com/questions/53062905
复制相似问题