mardi 30 octobre 2018

C text game segmentation fault and debugging

I wanted to wrote a simple text game in C. The game generates random 2D position using 2 double variables in range from -10.000 to 10.000 (X, Y) as the "treasure" position. Player begins at position 0.000, 0.000. Then the game asks the player which directions to go in X and Y. After that the game needs to give the player a simple hint, if he's getting closer(right or wrong direction based on the treasure position). When the player is less than 0.050 away from treasure in any direction, the game ends and prints out the score(cycle steps).

I wrote this, but I'm getting 'Segmentation fault' and I'm also interested about how would you modify it to get it more effective and functional.

#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;

}




Aucun commentaire:

Enregistrer un commentaire