Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. //plik cell.h
  2. #ifndef CELL_H
  3. #define CELL_H
  4.  
  5. #include <iostream>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. class Cell
  11. {
  12. bool state_current;
  13. bool state_future;
  14.  
  15. void editCell(bool n_state);
  16.  
  17. public:
  18. Cell();
  19. void show();
  20. void edit();
  21. };
  22.  
  23. #endif // CELL_H
  24.  
  25.  
  26. //plik cell.cpp
  27.  
  28. #include "cell.h"
  29.  
  30. Cell::Cell()
  31. {
  32. int a=0;
  33.  
  34. a=rand()%2;
  35. if(a==1)
  36. state_current=true;
  37. else
  38. state_current=false;
  39.  
  40. state_future=false;
  41. }
  42.  
  43. void Cell::show()
  44. {
  45. if(state_current==true)
  46. cout<<'X';
  47. else
  48. cout<<'O';
  49. }
  50.  
  51. void Cell::editCell(bool n_state)
  52. {
  53. state_current=n_state;
  54. }
  55.  
  56. void Cell::edit()
  57. {
  58. int option;
  59.  
  60. cout<<"choose avaible option:\n0.dead\n1.alive"<<endl;
  61.  
  62. cin>>option;
  63. while(option!=1 && option!=0)
  64. {
  65. cout<<"choose avaible option"<<endl;
  66. cin>>option;
  67. }
  68.  
  69. if(option==1)
  70. editCell(true);
  71. else
  72. editCell(false);
  73. }
  74.  
  75. //plik board.h
  76. #ifndef BOARD_H
  77. #define BOARD_H
  78.  
  79. #include "cell.h"
  80.  
  81. class Board :Cell
  82. {
  83. int v;
  84. int c;
  85. Cell **t;
  86. public:
  87. Board(int a=10, int b=10); //konstruktor z wartościami domyślnymi
  88. void showBoard();
  89. void getSize();
  90. void createBoard();
  91. };
  92.  
  93. #endif // BOARD_H
  94.  
  95.  
  96. //plik board.cpp
  97.  
  98. #include "board.h"
  99.  
  100. Board::Board(int a,int b)
  101. {
  102. v=a;
  103. c=b;
  104. t=new Cell *[v];
  105.  
  106. for(int i=0; i<v; i++)
  107. t[i]=new Cell [c];
  108. }
  109.  
  110. void Board::createBoard()
  111. {
  112. t=new Cell *[v];
  113.  
  114. for(int i=0; i<v; i++)
  115. t[i]=new Cell [c];
  116. }
  117.  
  118. void Board::showBoard()
  119. {
  120. for(int i=0; i<v; i++)
  121. {
  122. for(int j=0; j<c; j++)
  123. t[i][j].show();
  124. cout<<endl;
  125. }
  126. }
  127.  
  128. void Board::getSize()
  129. {
  130. int a,b;
  131.  
  132. cout<<"Enter natural numbers"<<endl;
  133. cin>>a;
  134. cin>>b;
  135. while(a<1 && b<1)
  136. {
  137. cout<<"Board can't have this size. Enter natural numbers"<<endl;
  138. cin>>a;
  139. cin>>b;
  140. }
  141. v=a;
  142. c=b;
  143. }
  144.  
  145.  
  146. //plik main.cpp
  147.  
  148. #include <iostream>
  149. #include <ctime>
  150. #include "board.h"
  151.  
  152.  
  153. using namespace std;
  154.  
  155. int main()
  156. {
  157. int a;
  158.  
  159. srand(time(NULL));
  160.  
  161. Board board;
  162.  
  163. cout<<"Hello in Game of Life"<<endl;
  164. cout<<"Select available options"<<endl;
  165. cout<<"1.Start"<<endl;
  166. cout<<"0.Exit"<<endl;
  167. cin>>a;
  168. while(a!=0)
  169. {
  170. switch(a)
  171. {
  172. case 1:
  173. {
  174. cout<<"Default board has got size 10x10"<<endl;
  175. cout<<"Select available options"<<endl;
  176. cout<<"1.Go"<<endl;
  177. cout<<"2.Change size"<<endl;
  178. cout<<"3.Back"<<endl;
  179. cin>>a;
  180. while(a!=3)
  181. {
  182. switch (a)
  183. {
  184. case 1:
  185. {
  186. board.showBoard();
  187. break;
  188. }
  189. case 2:
  190. {
  191. board.getSize();
  192. board.createBoard();
  193. }
  194. default:
  195. cout<<"This option does not exist"<<endl;
  196. break;
  197. }
  198. cout<<"Default board has got size 10x10"<<endl;
  199. cout<<"Select available options"<<endl;
  200. cout<<"1.Go"<<endl;
  201. cout<<"2.Change size"<<endl;
  202. cout<<"3.Back"<<endl;
  203. cin>>a;
  204. }
  205. break;
  206. }
  207. default:
  208. cout<<"This option does not exist"<<endl;
  209. break;
  210. }
  211. cout<<"1.Start"<<endl;
  212. cout<<"0.Exit"<<endl;
  213. cin>>a;
  214. }
  215.  
  216. return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement