Guest User

Untitled

a guest
Apr 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include <stdbool.h>
  2.  
  3. void printfile( FILE *file ) {
  4.  
  5. if ( file != NULL ) {
  6.  
  7. rewind( file );
  8.  
  9. int c = fgetc( file );
  10.  
  11. while ( c != EOF ) {
  12.  
  13. printf( "%c", c );
  14. c = fgetc( file );
  15.  
  16. }
  17.  
  18. }
  19.  
  20. printf("\n");
  21.  
  22. rewind( file );
  23.  
  24. }
  25.  
  26.  
  27. void inspect_maze_matrix( char array[][9] ) {
  28.  
  29. printf("[\n");
  30.  
  31. for( int i = 0; i < 9; i++ ) {
  32.  
  33. printf(" [ ");
  34.  
  35. for( int j = 0; j < 9; j++ ) {
  36.  
  37. printf( "%c ", array[i][j] );
  38.  
  39. }
  40.  
  41. printf("]\n");
  42.  
  43. }
  44.  
  45. printf("]\n");
  46.  
  47. }
  48.  
  49. void fill_maze_matrix( char array[][9] ) {
  50.  
  51. FILE *maze_file = fopen( "data.txt", "r" );
  52.  
  53. // printfile( maze_file );
  54.  
  55. int x = 0;
  56. int y = 0;
  57. char c = fgetc( maze_file );
  58.  
  59. while ( c != EOF ) {
  60.  
  61. if ( c == *"X" ) {
  62. array[x][y] = *"X";
  63. y++;
  64. } else if ( c == *"Y" ) {
  65. array[x][y] = *"Y";
  66. y++;
  67. } else if ( c == *"\n" ) {
  68. x++;
  69. y = 0;
  70. }
  71.  
  72. c = fgetc( maze_file );
  73. }
  74.  
  75. fclose( maze_file );
  76.  
  77. }
  78.  
  79. typedef struct {
  80. int x;
  81. int y;
  82. } Position;
  83.  
  84. typedef struct {
  85.  
  86. } PositionStack;
  87.  
  88.  
  89. void print_position( Position *pos ) {
  90. printf( "current position: x:%d y:%d\n", pos->x, pos->y );
  91. }
  92.  
  93. bool valid_position( Position *pos ) {
  94.  
  95. if (
  96.  
  97. pos->x < 9 &&
  98. pos->x >= 0 &&
  99. pos->y < 9 &&
  100. pos->y >= 0
  101.  
  102. ) {
  103.  
  104. return true;
  105.  
  106. } else {
  107.  
  108. return false;
  109.  
  110. }
  111.  
  112. }
  113.  
  114. bool search( char array[][9], Position *current_pos ) {
  115.  
  116. if ( current_pos->x == 8 && current_pos->y == 0 ) {
  117.  
  118. print_position( current_pos );
  119. return true;
  120.  
  121. } else {
  122.  
  123. if ( array[ current_pos->x ][ current_pos->y ] != *"Y" ) {
  124.  
  125. return false;
  126.  
  127. } else {
  128.  
  129. print_position( current_pos );
  130.  
  131. Position below = { current_pos->x + 1, current_pos->y };
  132. Position above = { current_pos->x - 1, current_pos->y };
  133. Position left = { current_pos->x, current_pos->y - 1 };
  134. Position right = { current_pos->x, current_pos->y + 1 };
  135.  
  136. if ( valid_position( &below ) && search( array, &below ) ) {
  137. return true;
  138. }
  139. else if ( valid_position( &left ) && search( array, &left ) ) {
  140. return true;
  141. }
  142. else {
  143. return false;
  144. }
  145.  
  146. }
  147.  
  148. }
  149.  
  150. }
Add Comment
Please, Sign In to add comment