Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. int main()
  2. {
  3. int lines, columns;
  4. Play game; // Déclaration à refaire proprement avec new() et un constructeur
  5. Play& jeu = game;
  6. srand(time(NULL));
  7.  
  8.  
  9. cout << "Random board nLines : ";
  10. cin >> lines;
  11. cout << "n Columns : ";
  12. cin >> columns;
  13.  
  14.  
  15. Board* randomTab = new Board(lines, columns, jeu);
  16. Board temporary = *randomTab;
  17. Board* tmp = NULL;
  18. Graph tree(randomTab);
  19.  
  20. print(randomTab); // this print works perfectly fine !
  21.  
  22. cout << "________________" << endl;
  23.  
  24. tree.CreateAllChildren(jeu); // create every possible children for the current board
  25.  
  26. vector<Graph*> children = tree.getchildren();
  27. for(vector<Graph*>::iterator itr = children.begin(); itr != children.end(); ++itr)
  28. {
  29. tmp = (*itr)->gettab();
  30. print(tmp); // this print fail.
  31.  
  32. // trace(temporary);
  33. }
  34.  
  35.  
  36. class Board
  37. {
  38.  
  39. private:
  40. int** plato;
  41. int nbline;
  42. int nbcolumn;
  43. Position emptyspot;
  44.  
  45. public:
  46. [...] // bunch of unused in this use case constructors, destructors etc...
  47. Board(int, int, Play&); // constructor in this use case
  48.  
  49. void setValue(Position&, int);
  50. void setNbline(int m);
  51. void setNbcolumn(int n);
  52. int getValue(Position&);
  53. int** getPlato();
  54. int getNbline();
  55. int getNbcolumn();
  56. int getEmptyline();
  57. int getEmptycolumn();
  58. void setEmptySpot(Position&);
  59. Position& getEmptySpot();
  60. [...]
  61.  
  62. };
  63.  
  64.  
  65.  
  66. Board::Board(int m, int n, Play& jeu) : plato(new int*[m]), nbline(m), nbcolumn(n), emptyspot(n-1,m-1)
  67. {
  68.  
  69. int x(1);
  70.  
  71. for (int i = 0; i < m; ++i)
  72. {
  73. plato[i] = new int[n];
  74.  
  75. for(int j = 0; j < n; ++j)
  76. {
  77. plato[i][j] = x;
  78. x++;
  79. }
  80. }
  81.  
  82. plato[m-1][n-1]=0;
  83. x=0;
  84.  
  85. while (x!=1000)
  86. {
  87.  
  88. int numbers[] = { UP, DOWN, LEFT, RIGHT };
  89. int length = sizeof(numbers) / sizeof(int);
  90. int randomNumber = numbers[rand() % length];
  91.  
  92. jeu.moves(*this, randomNumber);
  93. x++;
  94. }
  95. }
  96.  
  97.  
  98. class Play
  99. {
  100. /// nothing really relevant in the use case except the move methods
  101. private:
  102. [...]
  103. public:
  104.  
  105. void moves(Board*);
  106. bool moves(Board*, int);
  107. bool moves(Board&, int); // overload to use move in Board constructor with *this
  108.  
  109. };
  110.  
  111.  
  112. bool Play::moves(Board& tab, int code)
  113. {
  114.  
  115. Position temporary(0,0);
  116. Position& tmp = temporary;
  117. bool control(false);
  118.  
  119. switch(code)
  120. {
  121.  
  122. case LEFT :
  123. if(tab.getEmptycolumn()>0)
  124. {
  125.  
  126. tmp.setposition(tab.getEmptyline(),tab.getEmptycolumn()-1); // on place la destinsation du vide
  127. tab.setValue(tab.getEmptySpot(),tab.getValue(tmp)); // le vide vaut la valeur de sa destination
  128. tab.setEmptySpot(tmp); // actualisation du vide
  129. control = true;
  130. return control;
  131. }
  132. break;
  133.  
  134. case UP :
  135. if(tab.getEmptyline() > 0)
  136. {
  137. tmp.setposition(tab.getEmptyline()-1,tab.getEmptycolumn()); // on place la destinsation du vide
  138. tab.setValue(tab.getEmptySpot(),tab.getValue(tmp)); // le vide vaut la position de la case qui va bouger
  139. tab.setEmptySpot(tmp); // actualisation du vide
  140. control = true;
  141. return control;
  142. }
  143. break;
  144.  
  145.  
  146. case RIGHT :
  147. if(tab.getEmptycolumn() < tab.getNbcolumn()-1)
  148. {
  149. tmp.setposition(tab.getEmptyline(),tab.getEmptycolumn()+1); // on place la destinsation du vide
  150. tab.setValue(tab.getEmptySpot(),tab.getValue(tmp)); // le vide vaut la position de la case qui va bouger
  151. tab.setEmptySpot(tmp); // actualisation du vide
  152. control = true;
  153. return control;
  154. }
  155. break;
  156.  
  157. case DOWN :
  158. if(tab.getEmptyline() < tab.getNbline()-1)
  159. {
  160. tmp.setposition(tab.getEmptyline()+1,tab.getEmptycolumn()); // on place la destinsation du vide
  161. tab.setValue(tab.getEmptySpot(),tab.getValue(tmp)); // le vide vaut la position de la case qui va bouger
  162. tab.setEmptySpot(tmp); // actualisation du vide
  163. control = true;
  164. return control;
  165. }
  166. break;
  167.  
  168. default :
  169. cerr << "Mouvement impossible " << endl;
  170. break; // erreur
  171. }
  172. return control;
  173.  
  174. }
  175.  
  176.  
  177.  
  178.  
  179. class Graph
  180. {
  181. private:
  182. Graph* parent;
  183. vector<Graph*> children;
  184. Board* tab;
  185.  
  186.  
  187. public:
  188. Graph(Board*);
  189. Graph(Board*, Graph*);
  190. Graph(const Graph&);
  191. ~Graph();
  192. void AddNode(Board*);
  193. void CreateAllChildren(Play&); // just a call of AddNode each time Play.move with a direction return true.
  194.  
  195.  
  196. Graph& operator=(Graph);
  197. vector<Graph*>& getchildren();
  198. Graph* getparent();
  199. Board* gettab();
  200.  
  201. };
  202.  
  203. void Graph::AddNode(Board* tablo)
  204. {
  205.  
  206. Graph* newChild = new Graph(tablo, this);
  207. children.push_back(newChild);
  208. }
  209.  
  210.  
  211.  
  212. void print(Board* tab)
  213. {
  214.  
  215. Position pos(0,0);
  216. Position& p = pos;
  217.  
  218.  
  219. for (int i = 0; i < tab->getNbline(); i++)
  220. {
  221. for(int j = 0; j < tab->getNbcolumn(); j++)
  222. {
  223.  
  224.  
  225. p.setposition(i,j);
  226. if(i == tab->getEmptyline() && j == tab->getEmptycolumn())
  227. {
  228.  
  229. cout << setw(tab->getNbcolumn()) << tab->getValue(p);
  230.  
  231. if (j == tab->getNbcolumn()-1)
  232. {
  233. cout << endl;
  234. }
  235.  
  236. }
  237.  
  238. else
  239. {
  240. /// Call stack point at this line
  241. cout << setw(tab->getNbcolumn()) << tab->getValue(p);
  242.  
  243. if (j == tab->getNbcolumn()-1)
  244. {
  245. cout << endl;
  246. }
  247. }
  248. }
  249. }
  250. cout << endl;
  251. return;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement