C文字游戏分割故障及调试

时间:2018-10-30 11:03:22

标签: c random segmentation-fault double do-while

我想用C语言编写一个简单的文本游戏。该游戏使用-10.000至10.000(X,Y)范围内的2个双变量作为“宝藏”位置来生成随机2D位置。玩家从位置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;

}

2 个答案:

答案 0 :(得分:2)

我只是试图编译您的代码。

首先:如果您使用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-else语句中的任何内容都可以放入自己的函数中。这将帮助您和其他尝试理解您的代码的人。

答案 1 :(得分:1)

  1. char *direction_x;char *direction_y;应该与char st[1024]中使用的scanf类似
  2. i必须在使用前进行初始化
  3. scanf("%s", &direction_x);应该是scanf("%s", direction_x);
  4. 对于double,应使用%lf,而不是%f
  5. -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;
}