Guest User

Header

a guest
Apr 22nd, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "scanner.h"
  5. #include "maze.h"
  6.  
  7. struct maze
  8. {
  9. int startX, startY;//coordinates for the starting point
  10. int finishX, finishY; //coordinates for the finish point
  11.  
  12. int rows, cols; //the number of rows and columns in the 2D arrays
  13.  
  14. int **maze; //holds the encodings
  15. char **visits; //holds the path
  16. };
  17.  
  18. //these are the prototypes for our static functions
  19. static Maze createMaze(int rows, int columns);
  20. static void initializePath(Maze newMaze, FILE *fp);
  21. static int step(Maze maze, int row, int column);
  22. static void debugPrint(Maze maze);
  23. static void displayRowTop(Maze maze, int row, int columns);
  24. static void displayRowContent(Maze maze, int row, int columns);
  25. static void displayBottom(Maze maze, int rows, int columns);
  26.  
  27.  
  28. static int getCellHasTop(Maze maze, int row, int column);
  29. static int getCellHasBottom(Maze maze, int row, int column);
  30. static int getCellHasRight(Maze maze, int row, int column);
  31. static int getCellHasLeft(Maze maze, int row, int column);
  32. static char getVisit(Maze maze, int row, int column);
  33. static void setVisit(Maze maze, int row, int column, char visit);
  34.  
  35. //will call createMaze to allocate memory and will initialize the 2D arrays
  36. Maze initializeMaze(FILE *fp)
  37. {
  38. int rows = readInt(fp);
  39. int cols = readInt(fp);
  40.  
  41. Maze newMaze = createMaze(rows, cols); //allocates memory
  42. initializePath(newMaze, fp);//initializes values in the path
  43.  
  44. //TODO: INITIALIZE MAZE
  45. int i, j;
  46. for(i = 0; i < rows; ++i)
  47. {
  48. for( j = 0; j < cols; ++j)
  49. newMaze->maze[i][j] = readInt(fp);
  50. }
  51. //debugPrint(newMaze); //Used for testing initializetion
  52.  
  53. return newMaze;
  54. }
  55.  
  56. //the next 4 functions check if there is a wall on top, bottom, right, or left
  57. static int getCellHasTop(Maze maze, int row, int column)
  58. {
  59. return maze->maze[row][column] & 1;
  60. }
  61.  
  62. static int getCellHasBottom(Maze maze, int row, int column)
  63. {
  64. return maze->maze[row][column] & 4;
  65. }
  66.  
  67. static int getCellHasRight(Maze maze, int row, int column)
  68. {
  69. return maze->maze[row][column] & 2;
  70. }
  71.  
  72. static int getCellHasLeft(Maze maze, int row, int column)
  73. {
  74. return maze->maze[row][column] & 8;
  75. }
  76.  
  77. //these two functions will handle updating and getting values from the visits array
  78. //these make for more readable code than accessing the array directly
  79. static char getVisit(Maze maze, int row, int column)
  80. {
  81. return maze->visits[row][column];
  82. }
  83.  
  84. static void setVisit(Maze maze, int row, int column, char visit)
  85. {
  86. maze->visits[row][column] = visit;
  87. }
  88.  
  89. //takes in a maze and calls step to start backtracking, provides the starting
  90. //point as the first point to visit in the maze
  91. void findPath(Maze maze)
  92. {
  93. //TODO: MAY NEED TO ALTER THIS FUNCION TO HANDLE SPECIAL CASE
  94. step(maze, maze->startX, maze->startY);
  95. }
  96.  
  97. static int step(Maze maze, int row, int column)
  98. {
  99. //TODO: FINISH THE BACKTRACKING ALGORITHM
  100. int i,j;
  101. //int st;
  102. int successful;
  103. //maze->maze[row][column] = st;
  104.  
  105. if(getVisit(maze, row, column) == maze->maze[maze->finishX][maze->finishY])
  106. {
  107. displayMaze(maze);
  108. return 1;
  109. }
  110. // findPath(maze);
  111. displayMaze(maze);
  112. getchar();
  113. // getVisit(maze, row, column);//gets pos
  114. for(i = -2; i <= 2; ++i)
  115. {
  116. for(j = -2; j <= 2; ++j)
  117. {
  118. //i*i != j*j dont move in diagonal
  119. //row+i >= 0 && row + i < n, bounds checking
  120. //col+j >= 0 && col+j < n, bounds checking
  121. if(i*i != j*j && row+i >= 0 && column+j >= 0 )
  122. {
  123. getVisit(maze, row, column);//Gets position
  124. if(getCellHasTop(maze, row, column + 1) != 1)//Wall check, Zero meaning NO WALL
  125. {
  126. //++st;
  127. //gets that position and sees if it is unvisited...
  128. //need a test if it is visited.. if it has been visited before.. Set current spot as BAD_PATH
  129. if(getVisit(maze,row,column+j) == UNVISITED)
  130. {
  131. setVisit(maze, row, column+1, GOOD_PATH);
  132. successful = step(maze, row, column + 1);
  133. }
  134. // if(getVisit(maze, row, column+j) == GOOD_PATH)
  135. // {
  136. // setVisit(maze, row-i, column, BAD_PATH);
  137. // successful = step(maze, row, column+j);
  138. // }
  139. if(successful)
  140. return 1;
  141. }
  142. if(getCellHasLeft(maze,row - 1,column) != 1)
  143. {
  144. if(getVisit(maze, row-1, column) == UNVISITED)
  145. {
  146. setVisit(maze, row-1, column, GOOD_PATH);
  147. successful = step(maze, row-1, column);
  148. }
  149.  
  150. // if(getVisit(maze, row-i, column) == GOOD_PATH)
  151. // {
  152. //setVisit(maze, row-i, column, BAD_PATH);
  153. // successful = step(maze, row-i, column);
  154. // }
  155. //++st;
  156. if(successful)
  157. return 1;
  158. }
  159. if(getCellHasRight(maze,row + 1, column) != 1)
  160. {
  161. if(getVisit(maze, row+1, column) == UNVISITED)
  162. {
  163. setVisit(maze, row+1, column, GOOD_PATH);
  164. successful = step(maze, row+1, column);
  165. }
  166.  
  167. // if(getVisit(maze, row+i, column) == GOOD_PATH)
  168. // {
  169. // setVisit(maze, row+i, column, BAD_PATH);
  170. // successful = step(maze, row+i, column);
  171. // }
  172. //++st;
  173. if(successful)
  174. return 1;
  175. }
  176. if(getCellHasBottom(maze, row, column - 1) != 1)
  177. {
  178. if(getVisit(maze, row, column-1) == UNVISITED)
  179. {
  180. setVisit(maze, row, column-1, GOOD_PATH);
  181. successful = step(maze, row, column-1);
  182. }
  183.  
  184. // if(getVisit(maze, row, column-j) == GOOD_PATH)
  185. // {
  186. // setVisit(maze, row, column-j, BAD_PATH);
  187. // successful = step(maze, row, column-j);
  188. // }
  189. //++st;
  190. if(successful)
  191. return 1;
  192. }
  193. //PART 2
  194. //Do not do else if because then each one would require... That is last resort
  195. if(getCellHasTop(maze, row, column + 1) == 0)//Wall check
  196. {
  197. //++st;
  198. //a test if it is visited.. if it has been visited before.. Set current spot as BAD_PATH
  199. if(getVisit(maze,row, column+1) == GOOD_PATH)
  200. {
  201. setVisit(maze, row, column, BAD_PATH);
  202. successful = step(maze, row, column + 1);
  203. }
  204. if(successful)
  205. return 1;
  206. }
  207. if(getCellHasLeft(maze,row - 1,column) == 0)
  208. {
  209. if(getVisit(maze, row-1, column) == GOOD_PATH)
  210. {
  211. setVisit(maze, row, column, BAD_PATH);
  212. successful = step(maze, row-1, column);
  213. }
  214. //++st;
  215. if(successful)
  216. return 1;
  217. }
  218. if(getCellHasRight(maze,row + 1, column) == 0)
  219. {
  220. if(getVisit(maze, row+1, column) == GOOD_PATH)
  221. {
  222. setVisit(maze, row, column, BAD_PATH);
  223. successful = step(maze, row+1, column);
  224. }
  225. //++st;
  226. if(successful)
  227. return 1;
  228. }
  229. if(getCellHasBottom(maze, row, column - 1) == 0)
  230. {
  231. if(getVisit(maze, row, column-1) == GOOD_PATH)
  232. {
  233. setVisit(maze, row, column, BAD_PATH);
  234. successful = step(maze, row, column-1);
  235. }
  236. //++st;
  237. if(successful)
  238. return 1;
  239. }
  240. //Part 3
  241. if(getCellHasRight(maze, row + 1, column) != 0 && getCellHasLeft(maze,row - 1, column) != 0)// If there is a wall at those locationsZZ
  242. {
  243. if(getVisit(maze,row, column+1) == BAD_PATH)
  244. {
  245. setVisit(maze, row, column, BAD_PATH);
  246. successful = step(maze, row, column-1);
  247. }
  248. if(successful)
  249. return 1;
  250. }
  251. if(getCellHasTop(maze, row, column + 1) != 0 && getCellHasBottom(maze, row, column - 1) != 0)
  252. {
  253. if(getVisit(maze, row-1, column) == BAD_PATH)
  254. {
  255. setVisit(maze, row, column, BAD_PATH);
  256. successful = step(maze, row+1, column);
  257. }
  258. if(successful)
  259. return 1;
  260. }
  261. if(getCellHasTop(maze, row, column +1) != 0 && getCellHasBottom(maze, row, column - 1) != 0)
  262. {
  263. if(getVisit(maze, row+1, column) == BAD_PATH)
  264. {
  265. setVisit(maze, row, column, BAD_PATH);
  266. successful = step(maze, row-1, column);
  267. }
  268. if(successful)
  269. return 1;
  270. }
  271. }
  272. }
  273. }
  274. displayMaze(maze);
  275. getchar();
  276. return 0;
  277. }
  278. //this function handles allocating memory for the 2D arrays
  279. static Maze createMaze(int rows, int columns)
  280. {
  281. int i;
  282. Maze newMaze = malloc(sizeof *newMaze);
  283. newMaze->rows = rows;
  284. newMaze->cols = columns;
  285.  
  286. //TODO: ALLOCATE THE 2D ARRAYS
  287. newMaze->visits = malloc(rows * sizeof(char*));
  288. for(i = 0; i < rows; ++i)
  289. newMaze->visits[i] = malloc(columns * sizeof(char));
  290. newMaze->maze = malloc(rows * sizeof(int*));
  291. for(i = 0; i < rows; ++i)
  292. newMaze->maze[i] = malloc(columns * sizeof(int));
  293.  
  294. return newMaze;
  295. }
  296.  
  297. //this function handles initializing the values in the visits array
  298. static void initializePath(Maze newMaze, FILE *fp)
  299. {
  300. //TODO: INITIALIZE THE VISITS ARRAT. REMEMBER TO USE THE DEFINES IN .h
  301. newMaze->startX = readInt(fp);
  302. newMaze->startY = readInt(fp);
  303. newMaze->finishX = readInt(fp);
  304. newMaze->finishY = readInt(fp);
  305. int i,j;
  306.  
  307. for(i = 0; i < newMaze->rows; ++i)
  308. {
  309.  
  310. for(j = 0; j < newMaze->cols; ++j)
  311. newMaze->visits[i][j] = UNVISITED;
  312.  
  313. }
  314. newMaze->visits[newMaze->startX][newMaze->startY] = START;
  315. newMaze->visits[newMaze->finishX][newMaze->finishY] = FINISH;
  316. }
  317.  
  318. //used only for debugging purposes
  319. static void debugPrint(Maze maze)
  320. {
  321. int i,j;
  322. printf("Maze\n");
  323. for(i = 0; i < maze->rows; ++i)
  324. {
  325. for(j = 0; j < maze->cols; ++j)
  326. {
  327. printf("%d ", maze->maze[i][j]);
  328. }
  329. printf("\n");
  330. }
  331.  
  332. printf("Visits\n");
  333. for(i = 0; i < maze->rows; ++i)
  334. {
  335. for(j = 0; j < maze->cols; ++j)
  336. {
  337. printf("%c ", maze->visits[i][j]);
  338. }
  339. printf("\n");
  340. }
  341. }
  342.  
  343. //The next four functions handle displaying the maze
  344. void displayMaze(Maze maze)
  345. {
  346. int i;
  347. for(i = 0; i < maze->rows; ++i)
  348. {
  349. displayRowTop(maze, i, maze->cols);
  350. displayRowContent(maze, i, maze->cols);
  351. }
  352. displayBottom(maze, maze->rows, maze->cols);
  353. }
  354.  
  355. void displayRowTop(Maze maze, int row, int columns)
  356. {
  357. int i;
  358. for(i = 0; i < columns; ++i)
  359. {
  360. printf("+");
  361. if(getCellHasTop(maze, row, i)) //checks if wall is needed
  362. printf("---");
  363. else
  364. printf(" ");
  365. }
  366. printf("+\n");
  367. }
  368.  
  369. void displayRowContent(Maze maze, int row, int columns)
  370. {
  371. int i;
  372. for(i = 0; i < columns; ++i)
  373. {
  374. if(getCellHasLeft(maze, row, i)) //checks if wall is needed
  375. printf("|");
  376. else
  377. printf(" ");
  378. printf(" %c ",getVisit(maze,row, i));
  379. }
  380. if(getCellHasRight(maze, row, columns - 1))
  381. printf("|\n");
  382. else
  383. printf("\n");
  384. }
  385.  
  386. void displayBottom(Maze maze, int rows, int columns)
  387. {
  388. int i;
  389. for(i = 0; i < columns; ++i)
  390. {
  391. printf("+");
  392. if(getCellHasBottom(maze,rows-1,i))
  393. printf("---");
  394. else
  395. printf(" ");
  396. }
  397. printf("+\n");
  398. }
  399.  
  400. void freeMaze(Maze maze)
  401. {
  402. //TODO: FREE THE MEMORY FOR THE MAZE
  403. //TODO: FREE THE MEMORY FOR THE MAZE
  404. free(maze->cols);
  405. free(maze->rows);
  406. free(maze->visits);
  407. free(maze->maze);
  408. free(maze);
  409.  
  410. }
Add Comment
Please, Sign In to add comment