fork(2) download
  1. // CALLED FUNCTION
  2.  
  3. std::map<int, Board*> Board::potentialMoves() {
  4.  
  5. cout<<"Find potentialMoves for:\n"<<*this<<endl;
  6. int space = -1, v = -1;
  7.  
  8. std::map<int, Board*> moves;
  9.  
  10.  
  11. for(int i = 0; i < size_; i++){
  12.  
  13. if(tiles_[i] == 0){
  14. space = i;
  15. }
  16. }
  17.  
  18. std::cout<<"Space is at :"<<space<<endl;
  19. std::cout<<"Dim value: "<<dim<<endl;
  20.  
  21. if(space >= dim){
  22.  
  23. v = tiles_[space-dim];
  24.  
  25. int temptiles[this->size_];
  26.  
  27. for(int i = 0; i < this->size_; i++){
  28.  
  29. temptiles[i] = this->tiles_[i];
  30. }
  31.  
  32. Board *newb = new Board(temptiles, this->size_);
  33. newb -> move(v);
  34.  
  35. //cout<<"Top Move: \n"<<*newb<<endl;
  36. //cout<<"Move"<<newb;
  37.  
  38. moves.insert(std::pair<int,Board*>(v,newb));
  39. //bug.insert(std::make_pair(v,*newb));
  40.  
  41. for(map<int,Board*>::iterator it = moves.begin();
  42. it != moves.end(); ++it)
  43. {
  44. std::cout << "Inside: \n" << *(it->second) << "\n";
  45. }
  46.  
  47. // std::cout<<"Added a top neighbor"<<endl;
  48. }
  49.  
  50. for(map<int,Board*>::iterator it = moves.begin();
  51. it != moves.end(); ++it)
  52. {
  53. std::cout << "Outside: \n" << *(it->second) << "\n";
  54. }
  55.  
  56.  
  57.  
  58. // if(space%dim != 0){
  59.  
  60. // v = tiles_[space-1];
  61.  
  62. // int temptiles[this->size_];
  63.  
  64. // for(int i = 0; i < this->size_; i++){
  65.  
  66. // temptiles[i] = this->tiles_[i];
  67. // }
  68.  
  69. // Board *newb = new Board(temptiles, this->size_);
  70. // newb -> move(v);
  71.  
  72. // cout<<"Left Move: \n"<<*newb<<endl;
  73.  
  74. // moves.insert(std::pair<int,Board*>(v,newb));
  75.  
  76. // // std::cout<<"Added a left neighbor"<<endl;
  77.  
  78. // }
  79.  
  80. // if(space + dim < size_) {
  81.  
  82.  
  83. // v = tiles_[space+dim];
  84.  
  85. // int temptiles[this->size_];
  86.  
  87. // for(int i = 0; i < this->size_; i++){
  88.  
  89. // temptiles[i] = this->tiles_[i];
  90. // }
  91.  
  92. // Board *newb = new Board(temptiles, this->size_);
  93. // newb -> move(v);
  94.  
  95. // cout<<"Bottom Move: \n"<<*newb<<endl;
  96.  
  97. // moves.insert(std::pair<int,Board*>(v,newb));
  98.  
  99. // // std::cout<<"Added a bottom neighbor"<<endl;
  100.  
  101. // }
  102.  
  103. // if(space%dim != dim - 1) {
  104.  
  105. // v = tiles_[space+1];
  106.  
  107. // int temptiles[this->size_];
  108.  
  109. // for(int i = 0; i < this->size_; i++){
  110.  
  111. // temptiles[i] = this->tiles_[i];
  112. // }
  113.  
  114. // Board *newb = new Board(temptiles, this->size_);
  115. // newb -> move(v);
  116.  
  117. // cout<<"Right Move: \n"<<*newb<<endl;
  118.  
  119. // moves.insert(std::pair<int,Board*>(v,newb));
  120.  
  121. // // std::cout<<"Added a right neighbor"<<endl;
  122.  
  123. // }
  124.  
  125. for(map<int,Board*>::iterator it = moves.begin();
  126. it != moves.end(); ++it)
  127. {
  128. std::cout << "Sending board: \n" << it->second << "\n";
  129. }
  130.  
  131. return moves;
  132.  
  133. }
  134.  
  135. // CALLING FUNCTION
  136.  
  137. std::map<int, Board*> suc;
  138.  
  139. suc = (t->b_)->potentialMoves();
  140.  
  141. std::map<int, Board*>::iterator it = suc.begin();
  142.  
  143. for (it=suc.begin(); it!=suc.end(); ++it){
  144.  
  145. cout<<"Successor: \n"<<it->second<<endl;
  146.  
  147. // BoardSet::iterator i = closedlist.find(it->second);
  148.  
  149. // if(i == closedlist.end()){
  150.  
  151. // PuzzleMove *old = new PuzzleMove(*it->second);
  152.  
  153. // // (it->second)->move(it->first);
  154.  
  155. // // cout<<"Pushing: \n"<<*(it->second)<<endl;
  156.  
  157. // PuzzleMove *p = new PuzzleMove(it->first,it->second,old);
  158.  
  159. // openlist.push(p);
  160. // expansions_++;
  161.  
  162. // closedlist.insert(p->b_);
  163.  
  164. // }
  165. }
  166.  
  167. // PRINTING FUNCTION
  168.  
  169. std::ostream& operator<<(std::ostream &os, const Board &b) {
  170.  
  171. for(int i = 0; i < b.size_; i++){
  172.  
  173. if(i!=0 && i%(int)sqrt(b.size_) == 0) os << endl;
  174. os << " ";
  175. os << setw(2) << b.tiles_[i] ;
  176. }
  177.  
  178. return os;
  179. }
  180.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty