3D Game of Life implementation in c++
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gameoflife.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /* GameOfLife - Conway's Game of Life in 3D
  2. *
  3. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  4. * http://www.seba-geek.de
  5. *
  6. * This file is part of GameOfLife.
  7. *
  8. * GameOfLife is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * GameOfLife is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GameOfLife. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "gameoflife.h"
  22. GameOfLife::GameOfLife(int _x, int _y) {
  23. init();
  24. x = _x;
  25. y = _y;
  26. allocate();
  27. }
  28. GameOfLife::GameOfLife(std::string file) {
  29. init();
  30. load(file);
  31. }
  32. GameOfLife::GameOfLife(int _x, int _y, int r_life, int r_dead) {
  33. init();
  34. fillRandom(_x, _y, r_life, r_dead);
  35. }
  36. void GameOfLife::init() {
  37. view3d = true;
  38. feld = 0;
  39. x = y = 0;
  40. generation = 0;
  41. thickness = 0.5f;
  42. cellwidth = 1.0f;
  43. torus = true;
  44. fullcelluse = true;
  45. radius = 0.3f;
  46. height = 1.0f;
  47. parts = 12;
  48. sectobuild = 0.91f;
  49. b_secdone = 0.0f;
  50. secpertick = 1.0f;
  51. t_secdone = 0.0f;
  52. editmode = false;
  53. // Calc cylinder sinvals
  54. sinval = new float[parts];
  55. cosval = new float[parts];
  56. for(int i=0; i<parts; i++) {
  57. sinval[i] = radius * sin(segl::deg2rad(360.0f/parts*i));
  58. cosval[i] = radius * cos(segl::deg2rad(360.0f/parts*i));
  59. }
  60. quad = gluNewQuadric();
  61. }
  62. void GameOfLife::allocate() {
  63. feld = new State*[x];
  64. for(int i=0; i<x; i++) {
  65. feld[i] = new State[y];
  66. // clean it
  67. for(int j=0; j<y; j++)
  68. feld[i][j] = dead;
  69. }
  70. }
  71. void GameOfLife::cleanup() {
  72. if(feld) {
  73. for(int i=0; i<x; i++) {
  74. delete[](feld[i]);
  75. }
  76. delete(feld);
  77. feld = 0;
  78. }
  79. }
  80. void GameOfLife::renderBrett() {
  81. float x1 = cellwidth*x / 2.0f;
  82. float y1 = thickness / 2.0f;
  83. float z1 = cellwidth*y / 2.0f;
  84. // Brettquader
  85. GLfloat mat_diffuse[] = { 1.0f, 0.0f, 0.0f, 1.0f };
  86. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  87. glColor3f(1.0f, 0.0f, 0.0f);
  88. glBegin(GL_QUADS);
  89. // Platte
  90. glNormal3f(0.0f, 1.0f, 0.0f);
  91. glVertex3f(-x1, y1, -z1);
  92. glVertex3f( x1, y1, -z1);
  93. glVertex3f( x1, y1, z1);
  94. glVertex3f(-x1, y1, z1);
  95. // Boden
  96. glNormal3f(0.0f,-1.0f, 0.0f);
  97. glVertex3f(-x1, -y1, -z1);
  98. glVertex3f( x1, -y1, -z1);
  99. glVertex3f( x1, -y1, z1);
  100. glVertex3f(-x1, -y1, z1);
  101. // Seiten
  102. glNormal3f(0.0f, 0.0f, 1.0f);
  103. glVertex3f(-x1, -y1, z1);
  104. glVertex3f( x1, -y1, z1);
  105. glVertex3f( x1, y1, z1);
  106. glVertex3f(-x1, y1, z1);
  107. glNormal3f(0.0f, 0.0f,-1.0f);
  108. glVertex3f(-x1, -y1,-z1);
  109. glVertex3f( x1, -y1,-z1);
  110. glVertex3f( x1, y1,-z1);
  111. glVertex3f(-x1, y1,-z1);
  112. glNormal3f(-1.0f, 0.0f, 0.0f);
  113. glVertex3f(-x1, -y1, z1);
  114. glVertex3f(-x1, y1, z1);
  115. glVertex3f(-x1, y1,-z1);
  116. glVertex3f(-x1, -y1,-z1);
  117. glNormal3f(1.0f, 0.0f, 0.0f);
  118. glVertex3f( x1, -y1, z1);
  119. glVertex3f( x1, y1, z1);
  120. glVertex3f( x1, y1,-z1);
  121. glVertex3f( x1, -y1,-z1);
  122. glEnd();
  123. //Gitter
  124. glPushMatrix();
  125. mat_diffuse[0] = 0.0f;
  126. mat_diffuse[1] = 1.0f;
  127. mat_diffuse[2] = 0.0f;
  128. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  129. glColor3f(0.0f, 1.0f, 0.0f);
  130. for(int a=0; a<1; a++) {
  131. glTranslatef(0.0f, 0.01f, 0.0f);
  132. glBegin(GL_LINES);
  133. glNormal3f(0.0f, 1.0f, 0.0f);
  134. for(float i=-x1; i<=x1; i+=cellwidth) {
  135. glVertex3f(i, y1, -z1);
  136. glVertex3f(i, y1, z1);
  137. }
  138. for(float i=-z1; i<=z1; i+=cellwidth) {
  139. glVertex3f(-x1, y1, i);
  140. glVertex3f( x1, y1, i);
  141. }
  142. glEnd();
  143. }
  144. glPopMatrix();
  145. }
  146. void GameOfLife::renderStein(State s) {
  147. float sin1, sin2 = sinval[0], cos1, cos2 = cosval[0];
  148. float height2 = 0.0f;
  149. switch(s) {
  150. case alife:
  151. height2 = height;
  152. break;
  153. case born:
  154. height2 = height * (b_secdone/sectobuild);
  155. break;
  156. case dies:
  157. height2 = height * ((sectobuild-b_secdone)/sectobuild);
  158. break;
  159. case dead:
  160. // unlikely to happen...
  161. height2 = 0.0f;
  162. break;
  163. }
  164. if(height2<0.001f)
  165. return;
  166. glColor3f(0.2f, 0.2, 0.9f);
  167. // for(float i=0, step=360.0f/parts; i<360.0f; i+= step) {
  168. for(int i=0; i<parts; i++) {
  169. sin1 = sin2;
  170. cos1 = cos2;
  171. sin2 = sinval[(i+1)%parts];
  172. cos2 = cosval[(i+1)%parts];
  173. // sin2 = radius * sin(deg2rad(i+step));
  174. // cos2 = radius * cos(deg2rad(i+step));
  175. GLfloat mat_diffuse[] = { 0.2f, 0.2, 0.9f, 1.0f };
  176. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  177. /*
  178. glPushMatrix();
  179. glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
  180. gluCylinder(quad, radius, radius, height2, 12, 1);
  181. glTranslatef(0.0f, 0.0f, height2);
  182. gluDisk(quad, 0.0f, radius, 12, 1);
  183. glPopMatrix();
  184. */
  185. glBegin(GL_QUADS);
  186. glNormal3f(0.0f, 0.0f,-1.0f);
  187. glVertex3f(-radius, 0.0f, -radius);
  188. glVertex3f(-radius, height2,-radius);
  189. glVertex3f( radius, height2,-radius);
  190. glVertex3f( radius, 0.0f, -radius);
  191. glNormal3f(0.0f, 0.0f, 1.0f);
  192. glVertex3f( radius, 0.0f, radius);
  193. glVertex3f( radius, height2, radius);
  194. glVertex3f(-radius, height2, radius);
  195. glVertex3f(-radius, 0.0f, radius);
  196. glNormal3f(-1.0f, 0.0f, 0.0f);
  197. glVertex3f(-radius, 0.0f, -radius);
  198. glVertex3f(-radius, 0.0f, radius);
  199. glVertex3f(-radius, height2, radius);
  200. glVertex3f(-radius, height2,-radius);
  201. glNormal3f( 1.0f, 0.0f, 0.0f);
  202. glVertex3f( radius, 0.0f, radius);
  203. glVertex3f( radius, 0.0f, -radius);
  204. glVertex3f( radius, height2,-radius);
  205. glVertex3f( radius, height2, radius);
  206. glNormal3f(0.0f, 1.0f, 0.0f);
  207. glVertex3f(-radius, height2, radius);
  208. glVertex3f( radius, height2, radius);
  209. glVertex3f( radius, height2,-radius);
  210. glVertex3f(-radius, height2,-radius);
  211. glEnd();
  212. /*
  213. glBegin(GL_TRIANGLES);
  214. // Boden Unten
  215. glNormal3f(0.0f,-1.0f, 0.0f);
  216. glVertex3f(0.0f, 0.0f, 0.0f);
  217. glVertex3f(sin1, 0.0f, cos1);
  218. glVertex3f(sin2, 0.0f, cos2);
  219. // Boden Oben
  220. glNormal3f(0.0f, 1.0f, 0.0f);
  221. glVertex3f(0.0f, height2, 0.0f);
  222. glVertex3f(sin1, height2, cos1);
  223. glVertex3f(sin2, height2, cos2);
  224. //Seite
  225. glNormal3f((sin1+sin2)/2.0f, 0.0f, (cos1+cos2)/2.0f);
  226. glVertex3f(sin1, 0.0f, cos1);
  227. glVertex3f(sin2, 0.0f, cos2);
  228. glVertex3f(sin2, height2, cos2);
  229. glNormal3f((sin1+sin2)/2.0f, 0.0f, (cos1+cos2)/2.0f);
  230. glVertex3f(sin2, height2, cos2);
  231. glVertex3f(sin1, height2, cos1);
  232. glVertex3f(sin1, 0.0f, cos1);
  233. glEnd();
  234. */
  235. }
  236. }
  237. void GameOfLife::translateTo(int _x, int _y) {
  238. glTranslatef(-cellwidth/2.0f*x+cellwidth*_x+cellwidth/2.0f, thickness/2.0f, -cellwidth/2.0f*y+cellwidth*_y+cellwidth/2.0f);
  239. }
  240. void GameOfLife::lockStates() {
  241. for(int i=0; i<x; i++) {
  242. for(int j=0; j<y; j++) {
  243. if(feld[i][j]==dies)
  244. feld[i][j] = dead;
  245. else if(feld[i][j]==born)
  246. feld[i][j] = alife;
  247. }
  248. }
  249. }
  250. void GameOfLife::set3D(bool on) {
  251. view3d = on;
  252. }
  253. bool GameOfLife::load(std::string file) {
  254. std::ifstream data(file.c_str());
  255. if(!data)
  256. return false;
  257. cleanup();
  258. std::string tmp;
  259. data >> x;
  260. data >> y;
  261. data >> torus;
  262. if(x<=0 || y <=0)
  263. return false;
  264. allocate();
  265. int j=0;
  266. getline(data, tmp); // Remove \n
  267. while(getline(data, tmp) && j<y) {
  268. for(int i=0; i<x && i<(int)tmp.length(); i++) {
  269. feld[i][j] = (tmp[i]=='1') ? born : dead;
  270. }
  271. j++;
  272. }
  273. return true;
  274. }
  275. bool GameOfLife::save(std::string file) {
  276. std::ofstream data(file.c_str());
  277. if(!data)
  278. return false;
  279. data << x << " " << y << " " << (torus?1:0) << std::endl;
  280. for(int j=0; j<y; j++) {
  281. for(int i=0; i<x; i++) {
  282. data << ((feld[i][j]==alife||feld[i][j]==born) ? '1' : '0');
  283. }
  284. data << std::endl;
  285. }
  286. return true;
  287. }
  288. void GameOfLife::fillRandom(int _x, int _y, int r_life, int r_dead) {
  289. cleanup();
  290. x = _x;
  291. y = _y;
  292. allocate();
  293. srand(time(0));
  294. if(r_life+r_dead==0) {
  295. r_life = rand()%100 + 1;
  296. r_dead = rand()%100 + 1;
  297. }
  298. float ratio = r_life/(float)(r_life+r_dead);
  299. for(int i=0; i<x; i++) {
  300. for(int j=0; j<y; j++) {
  301. if((rand()%100)/100.0f < ratio) {
  302. feld[i][j] = born;
  303. } else {
  304. // should be set to dead, but to be sure...
  305. feld[i][j] = dead;
  306. }
  307. }
  308. }
  309. }
  310. // void GameOfLife::toggle(int _x, int _y, State stat) {
  311. // if(feld && _x>=0 && _x<x && _y>=0 && _y<y) {
  312. // feld[_x][_y] = stat;
  313. // }
  314. // }
  315. // const bool** GameOfLife::getFeld() {
  316. // return (const bool**)feld;
  317. // }
  318. void GameOfLife::setEditMode(bool e) {
  319. editmode = e;
  320. if(e) {
  321. lockStates();
  322. } else {
  323. }
  324. }
  325. void GameOfLife::move(int up, int right) {
  326. if(up>0)
  327. setpos.y = ((int)setpos.y+y+1)%y;
  328. else if(up<0)
  329. setpos.y = ((int)setpos.y+y-1)%y;
  330. if(right>0)
  331. setpos.x = ((int)setpos.x+x+1)%x;
  332. else if(right<0)
  333. setpos.x = ((int)setpos.x+x-1)%x;
  334. }
  335. void GameOfLife::toggle() {
  336. if(feld[(int)setpos.x][(int)setpos.y]==born||feld[(int)setpos.x][(int)setpos.y]==alife) {
  337. feld[(int)setpos.x][(int)setpos.y] = dead;
  338. } else {
  339. feld[(int)setpos.x][(int)setpos.y] = alife;
  340. }
  341. }
  342. void GameOfLife::render(float sec) {
  343. if(!feld)
  344. return;
  345. // Cylinder-building-timer
  346. if(b_secdone<=sectobuild) {
  347. b_secdone += sec;
  348. if(b_secdone>=sectobuild) {
  349. lockStates();
  350. }
  351. }
  352. // Tick-timer
  353. t_secdone += sec;
  354. if(t_secdone>=secpertick) {
  355. tick();
  356. t_secdone = 0.0f;
  357. b_secdone = 0.0f;
  358. }
  359. if(view3d) {
  360. renderBrett();
  361. for(int j=0; j<y; j++) {
  362. for(int i=0; i<x; i++) {
  363. if(feld[i][j]!=dead) {
  364. glPushMatrix();
  365. translateTo(i, j);
  366. renderStein(feld[i][j]);
  367. glPopMatrix();
  368. }
  369. if(editmode) {
  370. if(i==setpos.x && j==setpos.y) {
  371. glPushMatrix();
  372. float mat_diffuse[3] = { 0.0f, 1.0f, 0.0f };
  373. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  374. translateTo(i, j);
  375. glTranslatef(0.0f, 0.01f, 0.0f);
  376. glBegin(GL_QUADS);
  377. glColor3f(0.0f, 1.0f, 0.0f);
  378. glNormal3f(0.0f, 1.0f, 0.0f);
  379. glVertex3f(-cellwidth/2.0f, 0.0f, cellwidth/2.0f);
  380. glVertex3f( cellwidth/2.0f, 0.0f, cellwidth/2.0f);
  381. glVertex3f( cellwidth/2.0f, 0.0f,-cellwidth/2.0f);
  382. glVertex3f(-cellwidth/2.0f, 0.0f,-cellwidth/2.0f);
  383. glEnd();
  384. glPopMatrix();
  385. }
  386. }
  387. }
  388. }
  389. } else {
  390. SDL_Surface *screen = SDL_GetVideoSurface();
  391. float wperp = screen->w /(float)x;
  392. float hperp = screen->h /(float)y;
  393. float border;
  394. glPushMatrix();
  395. if(wperp<hperp) {
  396. glTranslatef(0.0f, (hperp-wperp)*y/2.0f, 0.0f);
  397. hperp = wperp;
  398. } else {
  399. glTranslatef((wperp-hperp)*x/2.0f, 0.0f, 0.0f);
  400. }
  401. border = 0.0f;
  402. glBegin(GL_QUADS);
  403. for(int j=0; j<y; j++) {
  404. for(int i=0; i<x; i++) {
  405. if(feld[i][j]==alife||feld[i][j]==born) {
  406. glColor3f(0.0f, 0.0f, 1.0f);
  407. } else {
  408. glColor3f(1.0f, 0.0f, 0.0f);
  409. }
  410. glVertex2f(hperp*i+border, hperp*j+border);
  411. glVertex2f(hperp*i+border, hperp*(j+1)-border);
  412. glVertex2f(hperp*(i+1)-border, hperp*(j+1)-border);
  413. glVertex2f(hperp*(i+1)-border, hperp*j+border);
  414. }
  415. }
  416. glEnd();
  417. if(editmode) {
  418. glBegin(GL_QUADS);
  419. glColor3f(0.0f, 1.0f, 0.0f);
  420. glVertex2f(hperp*setpos.x+border, hperp*setpos.y+border);
  421. glVertex2f(hperp*setpos.x+border, hperp*(setpos.y+1)-border);
  422. glVertex2f(hperp*(setpos.x+1)-border, hperp*(setpos.y+1)-border);
  423. glVertex2f(hperp*(setpos.x+1)-border, hperp*setpos.y+border);
  424. glEnd();
  425. }
  426. // glTranslatef(0.0f, 0.0f, 0.1f);
  427. glColor3f(0.0f, 0.0f, 0.0f);
  428. glBegin(GL_LINES);
  429. for(float i=0; i<x; i+=1.0f) {
  430. glVertex2f(i*hperp, 0);
  431. glVertex2f(i*hperp, y*hperp);
  432. }
  433. for(float i=0; i<y; i+=1.0f) {
  434. glVertex2f(0, i*hperp);
  435. glVertex2f(x*hperp, i*hperp); }
  436. glEnd();
  437. glPopMatrix();
  438. }
  439. }
  440. float GameOfLife::getTickSec() {
  441. return secpertick;
  442. }
  443. float GameOfLife::getBuildSec() {
  444. return sectobuild;
  445. }
  446. void GameOfLife::setTickSec(float s) {
  447. secpertick = s;
  448. }
  449. void GameOfLife::setBuildSec(float s) {
  450. sectobuild = s;
  451. }
  452. void GameOfLife::setTorus(bool t) {
  453. torus = t;
  454. }
  455. unsigned long GameOfLife::getGeneration() {
  456. return generation;
  457. }
  458. void GameOfLife::tick() {
  459. if(sectobuild>secpertick)
  460. lockStates();
  461. int near;
  462. int x1, y1;
  463. for(int i=0; i<x; i++) {
  464. for(int j=0; j<y; j++) {
  465. near = 0;
  466. for(int a=i-1; a<=(i+1); a++) {
  467. for(int b=j-1; b<=(j+1); b++) {
  468. if((a==i && b==j) || (!torus && (a<0 || b<0 || a>=x || b>=y)))
  469. continue;
  470. if(torus) {
  471. x1 = (a+x)%x;
  472. y1 = (b+y)%y;
  473. } else {
  474. x1 = a;
  475. y1 = b;
  476. }
  477. if(feld[x1][y1]==alife || (fullcelluse && (feld[x1][y1]==dies)))
  478. near++;
  479. }
  480. }
  481. if(feld[i][j]==alife) {
  482. if(near!=2 && near!=3)
  483. feld[i][j] = dies;
  484. } else if(feld[i][j]==dead) {
  485. if(near==3)
  486. feld[i][j] = born;
  487. }
  488. }
  489. }
  490. if(sectobuild<0.1f)
  491. lockStates();
  492. generation++;
  493. }
  494. void GameOfLife::clear() {
  495. for(int i=0; i<x; i++) {
  496. for(int j=0; j<y; j++) {
  497. feld[i][j] = dead;
  498. }
  499. }
  500. }
  501. GameOfLife::~GameOfLife() {
  502. cleanup();
  503. delete[](sinval);
  504. delete[](cosval);
  505. }