Guest User

Untitled

a guest
Aug 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. Why pointer assignment sets all parent pointers above it to the same thing C
  2. state current = state(state::goalState);
  3. current.setEqualTo(OPEN.front());
  4.  
  5.  
  6. void state::setEqualTo(const state x){
  7. this->parent = x.parent;
  8. for (int i = 0; i < 3; ++i) {
  9. for (int j = 0; j < 3; ++j) {
  10. this->board[i][j] = x.board[i][j];
  11. }
  12. }
  13. this->howWeGotHere = x.howWeGotHere;
  14. }
  15.  
  16. list<state> OPEN;
  17. state current = state(state::goalState);
  18. current.setEqualTo(OPEN.front());
  19. list<state> children = current.generateChildren();
  20. OPEN.splice(OPEN.end(), children);
  21.  
  22. //State Class
  23. state *parent = NULL;
  24. string howWeGotHere = "";
  25. int board[3][3];
  26.  
  27. state::state(int x[3][3], state *parent) {
  28. this->parent = parent;
  29. for (int i = 0; i < 3; ++i) {
  30. for (int j = 0; j < 3; ++j) {
  31. this->board[i][j] = x[i][j];
  32. }
  33. }
  34. }
  35.  
  36. state state::moveLeft() {
  37. int iIndex;
  38. int jIndex;
  39. for (int i = 0; i < 3; ++i) {
  40. for (int j = 0; j < 3; ++j) {
  41. if (board[i][j] == 0) {
  42. iIndex = i;
  43. jIndex=j;
  44. }
  45. }
  46. }
  47. state z = state(board, this);
  48. if (2 == jIndex) {
  49. return *this;
  50. } else {
  51. z.board[iIndex][jIndex] = z.board[iIndex][jIndex+1];
  52. z.board[iIndex][jIndex+1] = 0;
  53. z.howWeGotHere = "LEFT";
  54. }
  55. return z;
  56. }
  57.  
  58. state state::moveRight() {
  59. int iIndex;
  60. int jIndex;
  61. for (int i = 0; i < 3; ++i) {
  62. for (int j = 0; j < 3; ++j) {
  63. if (board[i][j] == 0) {
  64. iIndex = i;
  65. jIndex=j;
  66. }
  67. }
  68. }
  69. state z = state(board, this);
  70. if (0 == jIndex) {
  71. return *this;
  72. } else {
  73. z.board[iIndex][jIndex] = z.board[iIndex][jIndex-1];
  74. z.board[iIndex][jIndex-1] = 0;
  75. z.howWeGotHere = "RIGHT";
  76. }
  77. return z;
  78. }
  79.  
  80. state state::moveDown() {
  81. int iIndex;
  82. int jIndex;
  83. for (int i = 0; i < 3; ++i) {
  84. for (int j = 0; j < 3; ++j) {
  85. if (board[i][j] == 0) {
  86. iIndex = i;
  87. jIndex=j;
  88. }
  89. }
  90. }
  91. state z = state(board, this);
  92. if (0 == iIndex) {
  93. return *this;
  94. } else {
  95. z.board[iIndex][jIndex] = z.board[iIndex-1][jIndex];
  96. z.board[iIndex-1][jIndex] = 0;
  97. z.howWeGotHere = "DOWN";
  98. }
  99. return z;
  100. }
  101.  
  102. state state::moveUp() {
  103. int iIndex;
  104. int jIndex;
  105. for (int i = 0; i < 3; ++i) {
  106. for (int j = 0; j < 3; ++j) {
  107. if (board[i][j] == 0) {
  108. iIndex = i;
  109. jIndex = j;
  110. }
  111. }
  112. }
  113. state z = state(board, this);
  114. if (2 == iIndex) {
  115. return *this;
  116. } else {
  117. z.board[iIndex][jIndex] = z.board[iIndex+1][jIndex];
  118. z.board[iIndex+1][jIndex] = 0;
  119. z.howWeGotHere = "UP";
  120.  
  121. }
  122. return z;
  123. }
  124.  
  125. void state::setEqualTo(const state x){
  126. this->parent = x.parent;
  127. for (int i = 0; i < 3; ++i) {
  128. for (int j = 0; j < 3; ++j) {
  129. this->board[i][j] = x.board[i][j];
  130. }
  131. }
  132. this->howWeGotHere = x.howWeGotHere;
  133. }
  134.  
  135. list<state> state::generateChildren(){
  136. list<state> children;
  137. state temp(board);
  138. temp.setEqualTo(this->moveUp());
  139. if(*this == temp) {
  140. } else {
  141. children.push_front(temp);
  142. }
  143. temp.setEqualTo(this->moveDown());
  144. if(*this == temp) {
  145. } else {
  146. children.push_front(temp);
  147. }
  148. temp.setEqualTo(this->moveLeft());
  149. if(*this == temp) {
  150. } else {
  151. children.push_front(temp);
  152. }
  153. temp.setEqualTo(this->moveRight());
  154. if(*this == temp) {
  155. } else {
  156. children.push_front(temp);
  157. }
  158. return children;
  159. }
Add Comment
Please, Sign In to add comment