Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. #include <SFML/System.hpp>
  2. #include <SFML/Graphics.hpp>
  3.  
  4. #include <vector>
  5. #include <iostream>
  6. using namespace sf;
  7. using std::vector;
  8. using std::cout;
  9. using std::endl;
  10.  
  11.  
  12. class Grid
  13. {
  14. public:
  15. Grid(unsigned int x = 1000, unsigned int y = 1000, unsigned int diffs = 50) : w(x),h(y),diff(diffs)
  16. {
  17.  
  18. }
  19.  
  20. void draw(RenderWindow & window)
  21. {
  22. for(int i=0;i<w;i+=diff)
  23. {
  24. Vertex line[] =
  25. {
  26. sf::Vertex(sf::Vector2f(i,0)),
  27. sf::Vertex(sf::Vector2f(i, h))
  28. };
  29. line[0].color = Color(0,0,0);
  30. line[1].color = Color(0,0,0);
  31. window.draw(line, 2, sf::Lines);
  32. }
  33. for(int i=0;i<h;i+=diff)
  34. {
  35. Vertex line[] =
  36. {
  37. sf::Vertex(sf::Vector2f(0,i)),
  38. sf::Vertex(sf::Vector2f(w,i))
  39. };
  40. line[0].color = Color(0,0,0);
  41. line[1].color = Color(0,0,0);
  42.  
  43. window.draw(line, 2, sf::Lines);
  44. }
  45. }
  46.  
  47. unsigned int w;
  48. unsigned int h;
  49. unsigned int diff;
  50. };
  51.  
  52. class Square
  53. {
  54. public:
  55. Square()
  56. {
  57.  
  58. }
  59. Square(int x_pos, int y_pos, float w, bool alive = false)
  60. {
  61. square = new RectangleShape(Vector2f(w,w));
  62. square->setPosition(Vector2f(x_pos,y_pos));
  63. aliveState = alive;
  64. }
  65. /*Square (const Square& other )
  66. {
  67. if(this != &other)
  68. {
  69. delete this->square;
  70. this->square = other.square;
  71. }
  72. }
  73. Square& operator=(const Square& other)
  74. {
  75. if(this != &other)
  76. {
  77. delete this->square;
  78. this->square = other.square;
  79. }
  80. return *this;
  81. }*/
  82. ~Square()
  83. {
  84. delete square;
  85. }
  86.  
  87. void update()
  88. {
  89. if(aliveState)
  90. {
  91. square->setFillColor(Color::Black);
  92. }
  93. else
  94. {
  95. square->setFillColor(Color::White);
  96. }
  97.  
  98. }
  99.  
  100. void setFill(Color c = Color::White)
  101. {
  102. square->setFillColor(c);
  103. }
  104.  
  105. void draw(RenderWindow &window)
  106. {
  107. window.draw(*square);
  108. }
  109. RectangleShape* square;
  110.  
  111. bool aliveState;
  112. private:
  113.  
  114. };
  115.  
  116. vector<vector<Square*>> update(vector<vector<Square*>> v)
  117. {
  118. vector<vector<Square*>> tmp ;//= v;
  119.  
  120. for(int i=0;i<v.size();i++)
  121. {
  122. tmp.push_back(vector<Square*>());
  123. for(int j=0;j<v[0].size();j++)
  124. {
  125. tmp[i].push_back(new Square(v[i][j]->square->getPosition().x,v[i][j]->square->getPosition().y,v[i][j]->square->getSize().x,v[i][j]->aliveState));
  126. }
  127. }
  128.  
  129. int size1 = v.size();
  130. int size2= v[0].size();
  131.  
  132. for(int i=0;i<v.size();i++)
  133. {
  134. for(int j=0;j<size2;j++)
  135. {
  136. int live_neighbours = 0;
  137. live_neighbours += v[(size1+i-1)%size1][(size2+j-1)%size2]->aliveState;
  138. live_neighbours += v[(size1+i-1)%size1][(size2+j)%size2]->aliveState;
  139. live_neighbours += v[(size1+i-1)%size1][(size2+j+1)%size2]->aliveState;
  140.  
  141. live_neighbours += v[(size1+i)%size1][(size2+j-1)%size2]->aliveState;
  142. live_neighbours += v[(size1+i)%size1][(size2+j+1)%size2]->aliveState;
  143.  
  144. live_neighbours += v[(size1+i+1)%size1][(size2+j-1)%size2]->aliveState;
  145. live_neighbours += v[(size1+i+1)%size1][(size2+j)%size2]->aliveState;
  146. live_neighbours += v[(size1+i+1)%size1][(size2+j+1)%size2]->aliveState;
  147. //cout <<" X:"<<i << " y:"<< j << " Live neighbours:"<< live_neighbours<<endl;
  148. if(v[i][j]->aliveState)
  149. {
  150. if(live_neighbours < 2)
  151. {
  152. tmp[i][j]->aliveState = false;
  153. }
  154. else if(live_neighbours > 3)
  155. {
  156. tmp[i][j]->aliveState = false;
  157. }
  158. }
  159. else
  160. {
  161. if(live_neighbours == 3)
  162. {
  163. tmp[i][j]->aliveState = true;
  164. }
  165. }
  166. }
  167. }
  168.  
  169. return tmp;
  170. }
  171.  
  172.  
  173. /*void update(vector<vector<Square*>> &v)
  174. {
  175. vector<vector<Square*>> tmp = v;
  176.  
  177. for(int i=0;i<size1;i++)
  178. {
  179. for(int j=0;j<v[0].size();j++)
  180. {
  181. int live_neighbours = 0;
  182. live_neighbours += v[(i-1)%size1][(j-1)%v[0].size()]->aliveState;
  183. live_neighbours += v[(i-1)%size1][(j)%v[0].size()]->aliveState;
  184. live_neighbours += v[(i-1)%size1][(j+1)%v[0].size()]->aliveState;
  185. live_neighbours += v[(i)%size1][(j-1)%v[0].size()]->aliveState;
  186. live_neighbours += v[(i)%size1][(j+1)%v[0].size()]->aliveState;
  187. live_neighbours += v[(i+1)%size1][(j-1)%v[0].size()]->aliveState;
  188. live_neighbours += v[(i+1)%size1][(j)%v[0].size()]->aliveState;
  189. live_neighbours += v[(i+1)%size1][(j+1)%v[0].size()]->aliveState;
  190.  
  191. //cout <<" X:"<<i << " y:"<< j << " Live neighbours:"<< live_neighbours<<endl;
  192. if(v[i][j]->aliveState)
  193. {
  194. if(live_neighbours < 2)
  195. {
  196. //dead.push_back(Vector2i(i,j));
  197. tmp[i][j]->aliveState = false;
  198. }
  199. else if(live_neighbours > 3)
  200. {
  201. tmp[i][j]->aliveState = false;
  202. }
  203. }
  204. else
  205. {
  206. if(live_neighbours == 3)
  207. {
  208. //nextGen[i][j] = true;
  209. tmp[i][j]->aliveState = true;
  210. }
  211.  
  212.  
  213. }
  214.  
  215. }
  216. }
  217.  
  218. v=tmp;
  219.  
  220. }*/
  221.  
  222. /*vector<vector<Square*>> update(vector<vector<Square*>> v)
  223. {
  224. vector<vector<Square*>> tmp = v;
  225. int size1 = v.size(), size2 = v[0].size();
  226. for(int i=0;i<size1;i++)
  227. {
  228. for(int j=0;j<size2;j++)
  229. {
  230. int alive = 0;
  231. for(int k=-1;k<=1;k++)
  232. {
  233. for(int l = -1; l<=1;l++)
  234. {
  235. if(!(l==0 && k ==0))
  236. {
  237. alive += v[(i+k)%size1][(j+l)%size2]->aliveState;
  238. }
  239. }
  240. }
  241. if(v[i][j]->aliveState)
  242. {
  243. if(alive < 2)
  244. {
  245. tmp[i][j]->aliveState = false;
  246. }
  247. else if(alive > 3)
  248. {
  249. tmp[i][j]->aliveState = false;
  250. }
  251. }
  252. else
  253. {
  254. if(alive == 3)
  255. {
  256. tmp[i][j]->aliveState = true;
  257. }
  258. }
  259. }
  260. }
  261. }*/
  262.  
  263. void killall(vector<vector<Square*>> &v)
  264. {
  265. for(int i=0;i<v.size();i++)
  266. {
  267. for(int j=0;j<v[0].size();j++)
  268. {
  269. v[i][j]->aliveState=false;
  270. }
  271. }
  272. }
  273.  
  274. int main()
  275. {
  276. RenderWindow window(VideoMode(1000,1000),"Game of Life");
  277. window.setFramerateLimit(10);
  278. window.setActive();
  279.  
  280. Vector2u size = window.getSize();
  281.  
  282. Grid g(size.x,size.y,1000/40);
  283.  
  284.  
  285. int h = g.h/g.diff+1;
  286. int w = g.w/g.diff+1;
  287. //Square squares[h][w];
  288.  
  289. std::vector<std::vector<Square*>> squares;
  290.  
  291. bool edit_mode = true;
  292.  
  293.  
  294. for(int i=0;i<h;i++)
  295. {
  296. squares.push_back(vector<Square*>());
  297. for(int j=0;j<w;j++)
  298. {
  299. squares[i].push_back(new Square(i*g.diff+1,j*g.diff+2,g.diff-3));
  300. }
  301. }
  302. //squares[4][5]->aliveState=true;
  303.  
  304.  
  305. while (window.isOpen())
  306. {
  307. window.clear(sf::Color::White);
  308. // check all the window's events that were triggered since the last iteration of the loop
  309. sf::Event event;
  310. while (window.pollEvent(event))
  311. {
  312. // "close requested" event: we close the window
  313. if (event.type == sf::Event::Closed)
  314. {
  315. window.close();
  316. }
  317. else if(event.type == Event::MouseButtonPressed)
  318. {
  319. if(edit_mode && event.mouseButton.button == Mouse::Button::Left)
  320. {
  321. /*cout<<event.mouseButton.x<<" "<<event.mouseButton.y<<endl;
  322. cout<<event.mouseButton.x/g.diff<< " "<< event.mouseButton.y/g.diff<<endl;*/
  323. squares[event.mouseButton.x/g.diff][event.mouseButton.y/g.diff]->aliveState= !squares[event.mouseButton.x/g.diff][event.mouseButton.y/g.diff]->aliveState;
  324. cout<< "Changed state on entity at X:"<< event.mouseButton.x/g.diff << " Y:"<<event.mouseButton.y/g.diff << " to "<< (squares[event.mouseButton.x/g.diff][event.mouseButton.y/g.diff]->aliveState? "Alive" : "Dead")<<endl;
  325. }
  326. }
  327. else if(event.type == Event::KeyPressed)
  328. {
  329. if(event.key.code == Keyboard::Q)
  330. {
  331. cout<<"Close request recieved. Application will exit."<<endl;
  332. window.close();
  333. }
  334. if(edit_mode && event.key.code == Keyboard::C)
  335. {
  336. cout<< "Killed all entities." <<endl;
  337. killall(squares);
  338. }
  339. if(event.key.code == Keyboard::E)
  340. {
  341. edit_mode = !edit_mode;
  342. if(edit_mode)
  343. {
  344. cout<< "Changed to edit mode."<<endl;
  345. }
  346. else
  347. {
  348. cout<< "Changed to simulation mode."<<endl;
  349. }
  350.  
  351. }
  352. }
  353.  
  354.  
  355. }
  356.  
  357. /*s.draw(window);
  358. s.square->setPosition(Vector2f(s.square->getPosition().x+1,s.square->getPosition().y));*/
  359. g.draw(window);
  360.  
  361. for(int i=0;i<h;i++)
  362. {
  363. for( int j=0; j<w;j++)
  364. {
  365. squares[i][j]->draw(window);
  366. }
  367. }
  368.  
  369. window.display();
  370. if(!edit_mode) squares = update(squares);
  371. for(int i=0;i<h;i++)
  372. {
  373. for( int j=0; j<w;j++)
  374. {
  375. squares[i][j]->update();
  376. }
  377. }
  378. }
  379.  
  380. return EXIT_SUCCESS;
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement