Пример #1
0
game init_game(int level){
  FILE* f = fopen("rush_hour.txt", "r");
  if(f == NULL)
    exit(EXIT_FAILURE);
  char tmp[100]; 
  for(int i=0; i<level; ++i){
    if(!fgets(tmp, 100, f)){
      fprintf(stderr, "There is no level %d\n", level);
      exit(EXIT_FAILURE);
    }
  }
  const char limit[1] = ".";
  char* splited = strtok(tmp, limit);
  int nb_pieces = atoi(splited);
  piece pieces[nb_pieces];
  char value[1];
  for(int i=0; i<nb_pieces; ++i){
    splited = strtok(NULL, limit);
    printf("%s\n", splited);
    value[0] = splited[0];
    int x = atoi(value);
    value[0] = splited[1];
    int y = atoi(value);
    value[0] = splited[2];
    bool small = atoi(value);
    value[0] = splited[3];
    bool horizontal = atoi(value);
    pieces[i] = new_piece_rh(x, y, small, horizontal);
  }
  fclose(f);
  return new_game_hr(nb_pieces, pieces);
}
Пример #2
0
int main(int argc, char *argv[]){
  set_up_pieces();
  game g = new_game_hr(NB_PIECES, pieces);
  char buf[3][100];
  int piece_num;
  dir d;
  int distance;
  while(!game_over_hr(g)){
    bool good = false;
    while(!good){
      while(!good){
	set_up_board(g);
	print_game(g);
	printf("Move the pieces for free the piece 0 to the exit:\n");
	printf("Total move: %d\n",game_nb_moves(g));
	while(!good){
	  printf("What piece do you want to move?\n");
	  read(0, buf[0], sizeof(char)*100);
	  if(strcmp(buf[0], "cancel") == 10)
	    break;
	  if(strcmp(buf[0], "exit") == 10)
	    return EXIT_SUCCESS;
	  if(buf[0][0]<48 || buf[0][0]>=48+NB_PIECES || buf[0][1] != 10)
	    printf("Write a number between 0 and %d\tor write cancel or exit.\n",NB_PIECES-1);
	  else{
	    piece_num = atoi(buf[0]);
	    good = true;
	  }
	}
	if(!good)
	  break;
	good = false;
	while(!good){
	  printf("In what direction?\n");
	  read(0, buf[1],  sizeof(char)*100);
	  if(strcmp(buf[1], "cancel") == 10)
	    break;
	  if(strcmp(buf[1], "exit") == 10)
	    return EXIT_SUCCESS;
	  if(!is_dir_option(buf[1]))
	    printf("Write one of those direction: up, down, right, left\tor write cancel or exit.\n");
	  else{
	    for(int i=0; i<4; ++i){
	      if(strcmp(buf[1], direction[i].dir_name) == 10)
		d = direction[i].dir_option;
	    }
	    if(!good_direction(g, piece_num, d)){
	      if(is_horizontal(game_piece(g, piece_num)))
		printf("The piece %d cannot move vertycaly\n", piece_num);
	      else
		printf("The piece %d cannot move horizontaly\n", piece_num);
	    }else
	      good = true;
	  }
	}
	if(!good)
	  break;
	good = false;
	while(!good){
	  printf("How many case?\n");
	  read(0, buf[2],  sizeof(char)*100);
	  if(strcmp(buf[2], "cancel") == 10)
	    break;
	  if(strcmp(buf[2], "exit") == 10)
	    return EXIT_SUCCESS;
	  if(buf[2][0]<48 || buf[2][0]>=48+SIZE_ARRAY || buf[2][1] != 10)
	    printf("Write a number between 0 and %d\tor write cancel or exit.\n",SIZE_ARRAY-1);
	  else{
	    distance = atoi(buf[2]);
	    good = play_move(g, piece_num, d, distance);
	    if(!good)
	      printf("The piece %d cannot move to that case.\n", piece_num);
	  }
	}
      }
    }
  }
  set_up_board(g);
  print_game(g);
  printf("CONGRETULATION\nYou won in %d moves\n", game_nb_moves(g));
  return EXIT_SUCCESS;
}