View Single Post
Join Date: Nov 2008
Posts: 11
Reputation: Nyaato is an unknown quantity at this point 
Solved Threads: 0
Nyaato Nyaato is offline Offline
Newbie Poster

Re: Need help in finding correct method for my program!

 
0
  #8
Dec 1st, 2008
As much as I would like to change the maze, it's not really possible for me to do that, as the maze stated in standard.txt is what I need solve.

I'll paste the whole code here again. I've fixed some of the error I've found, and it should, more or less work properly now, however, there are still some errors around.

I'll work on changing the for loop into while loop, as you've suggested, and it seems quite true, since I've got no idea how many steps I would need to finish the whole maze.

Anyway... here's the whole code. I know it's kind of messy... but bear with me. Thanks again.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. char gameArray[8][16];
  7. int startX = 0;
  8. int startY = 0;
  9. bool moveNorth = true; // checks if it is able to move north
  10. bool moveEast = true; // checks if it's able to move east
  11. bool positionCheck = false; // used to check if Mouse's (M) position is located
  12.  
  13. void findPath(void)
  14. {
  15. for (int i = 0; i<8; i++)
  16. {
  17. for (int j = 0; j <16; j++)
  18. {
  19. if (gameArray[i][j] == 'M')
  20. {
  21. if (positionCheck == false)
  22. {
  23. startX = j;
  24. startY = i;
  25. positionCheck = true;
  26. cout << "Mouse is at: " << i << " " << j << endl;
  27. }
  28. }
  29. }
  30. }
  31. // moving in north
  32. if (gameArray[startY-1][startX] == ' ')
  33. {
  34. moveEast = false;
  35. cout << "Replacing path!" << endl;
  36. gameArray[startY-1][startX] = '+';
  37. startY--;
  38. findPath();
  39. }
  40. // moves in the east direction
  41. if (gameArray[startY][startX+1] == ' ')
  42. {
  43. moveNorth = false;
  44. cout << "Replacing path!" << endl;
  45. gameArray[startY][startX+1] = '+';
  46. startX++;
  47. findPath();
  48. }
  49. // moves in the west direction
  50. if (gameArray[startY][startX-1] == ' ')
  51. {
  52. moveEast = false;
  53. moveNorth = false;
  54. cout << "Replacing path!" << endl;
  55. gameArray[startY][startX-1] = '+';
  56. startX--;
  57. findPath();
  58. }
  59. // backtracking
  60. else if (gameArray[startY][startX] == '+')
  61. {
  62. if (moveNorth == false)
  63. {
  64. // backtracking in north direction
  65. cout << "Back tracking!" << endl;
  66. gameArray[startY][startX] = 'X';
  67. startX--;
  68. }
  69. else if (moveEast == false)
  70. {
  71. // backtracking in east direction
  72. cout << "Back tracking!" << endl;
  73. gameArray[startY][startX] = 'X';
  74. startY++;
  75. }
  76. }
  77. // check if north reaches a dead end
  78. else if (gameArray[startY-1][startX] == '*' && moveNorth == true)
  79. {
  80. cout << "Max for north reached!" << endl;
  81. gameArray[startY][startX] = 'X';
  82. startY++;
  83. findPath();
  84. }
  85. // checks if east reaches a dead end
  86. else if (gameArray[startY][startX] == '+' && moveEast == true)
  87. {
  88. cout << "Backtracking" << endl;
  89. gameArray[startY][startX] = 'X';
  90. startY++;
  91. findPath();
  92. }
  93.  
  94. }
  95.  
  96.  
  97.  
  98. char inputStandard(void)
  99. {
  100. ifstream myfile ("standard.txt");
  101.  
  102. if (myfile.is_open())
  103. {
  104. for (int i = 0; i<8; i++)
  105. {
  106. for (int j = 0; j < 16; j++)
  107. {
  108. myfile.get(gameArray[i][j]);
  109. }
  110. }
  111. myfile.close();
  112. }
  113. return 0;
  114. }
  115.  
  116.  
  117. char inputCustom(void)
  118. {
  119. ifstream myfile ("custom.txt");
  120.  
  121. if (myfile.is_open())
  122. {
  123. for (int i = 0; i<8; i++)
  124. {
  125. for (int j = 0; j < 16; j++)
  126. {
  127. myfile.get(gameArray[i][j]);
  128. }
  129. }
  130. myfile.close();
  131. }
  132. return 0;
  133. }
  134.  
  135. void main(void)
  136. {
  137. string status_mode = "NONE";
  138. int choice;
  139. for (int start_loop = 1; start_loop>0; start_loop++)
  140. {
  141. if (status_mode != "NONE")
  142. {
  143. for (int indexrow=0; indexrow<8; indexrow++)
  144. {
  145. for (int indexcol=0; indexcol<16;indexcol++)
  146. {
  147. cout << gameArray[indexrow][indexcol];
  148. }
  149. }
  150. cout << endl;
  151. }
  152. cout << "Active Maze: " << status_mode << endl;
  153. cout << "1. Select Standard Maze" << endl;
  154. cout << "2. Select Custom Maze" << endl;
  155. cout << "3. Find Path" << endl;
  156. cout << "4. Print Path" << endl;
  157. cout << "5. End " << endl;
  158.  
  159. cin >> choice;
  160.  
  161. if (choice == 1)
  162. {
  163. status_mode = "STANDARD";
  164. inputStandard();
  165. cout << "Standard Mode Selected!" << endl << endl;
  166. }
  167. if (choice == 2)
  168. {
  169. status_mode = "CUSTOM";
  170. inputCustom();
  171. cout << "Custom Mode Selected!" << endl << endl;
  172. }
  173. if (choice == 3)
  174. {
  175. cout << "DISABLED." << endl << endl;
  176. findPath();
  177. }
  178. if (choice == 4)
  179. {
  180. cout << "DISABLED. " << endl << endl;
  181. }
  182. if (choice == 5)
  183. {
  184. cout << "EXITING!" << endl;
  185. break;
  186. }
  187. if (!cin)
  188. {
  189. cout << "ERROR IN INPUT!" << endl;
  190. }
  191. }
  192. }
Reply With Quote