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
  #6
Nov 30th, 2008
Okay, so far so good. I understood what you've suggested to me. However, I'm still in a pinch.

I got this whole chunk of code done up, and the backtracking works to a certain extend. I know something is wrong with my backtracking, but I've got no idea how to fix it.

  1. void findPath(void)
  2. {
  3. for (int i = 0; i<8; i++)
  4. {
  5. for (int j = 0; j <16; j++)
  6. {
  7. if (gameArray[i][j] == 'M')
  8. {
  9. if (positionCheck == false)
  10. {
  11. startX = j;
  12. startY = i;
  13. positionCheck = true;
  14. cout << "Mouse is at: " << i << " " << j << endl;
  15. }
  16. }
  17. }
  18. }
  19. // moving in north
  20. if (gameArray[startY-1][startX] == ' ')
  21. {
  22. cout << "Replacing path!" << endl;
  23. gameArray[startY-1][startX] = '+';
  24. startY--;
  25. findPath();
  26. }
  27. // moving in east
  28. else if (gameArray[startY][startX+1] == ' ')
  29. {
  30. cout << "Replacing path!" << endl;
  31. gameArray[startY][startX+1] = '+';
  32. startX++;
  33. findPath();
  34. }
  35. // backtracking
  36. else if (gameArray[startY][startX] == '+')
  37. {
  38. cout << "Backtracking" << endl;
  39. gameArray[startY][startX] = 'X';
  40. if (moveEast == false)
  41. {
  42. startX--;
  43. }
  44. else if (moveNorth == false)
  45. {
  46. startY++;
  47. }
  48. findPath();
  49. }
  50. // checking for max north
  51. else if (gameArray[startY-1][startX] == '*' && moveNorth == true)
  52. {
  53. moveNorth = false;
  54. cout << "Max for north reached!" << endl;
  55. gameArray[startY][startX] = 'X';
  56. startY++;
  57. findPath();
  58. }
  59. // checking for max east
  60. else if (gameArray[startY][startX+1] == '*' && moveEast == true)
  61. {
  62. moveEast = false;
  63. cout << "Max for east reached!" << endl;
  64. gameArray[startY][startX] = 'X';
  65. startX--;
  66. findPath();
  67. }
  68. }

The code above is the whole findPath function. There is something wrong with the backtracking area, but I've got no idea how to fix it.

A screenshot of the problem is displayed in the attachment. Please advise!
Attached Images
 
Reply With Quote