login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > Overige > C en C++ > C++ - Game of Life

C++ - Game of Life

Auteur: TjOeNeR - 04 december 2006 - 15:17 - Gekeurd door: Joel - Hits: 3402 - Aantal punten: (0 stemmen)



Een hele simpele versie van het "Game of Life"

Kijk op http://en.wikip...me_of_Life

Code:
  1. <?/**
  2. * Auteur: Jan De Kock
  3. * Klas: 2EA-ICT/2
  4. * Kort: "Game of Life"
  5. * Bedenkingen
  6. * Game of life had ik al eens tegengekomen op het net, maar dan met leeftijden (dus dieptes) bij
  7. * Ik von dit wel een fascinerend iets, dus heb ik mij daar eens in verdiept.
  8. * Ik heb daarom ook deze C implementatie geschreven.
  9. * Bronnen: - http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life (voor informatie, niet voor source)
  10. */
  11.  
  12. #include <iostream>
  13. using namespace std;
  14.  
  15. #define WIDTH 72
  16. #define HEIGHT 50
  17. #define LIVE '*'
  18. #define DEAD '.'
  19. #define XHALF (WIDTH / 2)
  20. #define YHALF (HEIGHT / 2)
  21.  
  22. void display(char world[WIDTH+2][HEIGHT+2]);
  23. void empty (char world[WIDTH+2][HEIGHT+2]);
  24. void live (char world[WIDTH+2][HEIGHT+2]);
  25.  
  26. void setCell(int x, int y, char world[WIDTH+2][HEIGHT+2]);
  27.  
  28. void inputCell(char world[WIDTH+2][HEIGHT+2]);
  29.  
  30. int main()
  31. {
  32. char world[WIDTH+2][HEIGHT+2];
  33. empty(world);
  34. setCell(XHALF+3, YHALF-1, world);
  35. setCell(XHALF-3, YHALF+0, world);
  36. setCell(XHALF-2, YHALF+0, world);
  37. setCell(XHALF-2, YHALF+1, world);
  38. setCell(XHALF+2, YHALF+1, world);
  39. setCell(XHALF+3, YHALF+1, world);
  40. setCell(XHALF+4, YHALF+1, world);
  41.  
  42. while(true)
  43. {
  44. system("cls");
  45. display(world);
  46. system("pause");
  47. live(world);
  48. }
  49. }
  50.  
  51. /**
  52. * Geeft de wereld weer
  53. */
  54. void display(char world[WIDTH+2][HEIGHT+2])
  55. {
  56. int x, y;
  57. for(y = 1; y <= HEIGHT; y++)
  58. {
  59. for(x = 1; x <= WIDTH; x++)
  60. {
  61. cout << world[x][y];
  62. }
  63. cout << endl;
  64. }
  65. cout << endl;
  66. }
  67.  
  68. /**
  69. * Maakt de wereld leeg (of doods)
  70. */
  71. void empty(char world[WIDTH+2][HEIGHT+2])
  72. {
  73. memset(world, DEAD, (WIDTH+2)*(HEIGHT+2));
  74. }
  75.  
  76. /**
  77. * Geeft de wereld een iteratie bij.
  78. */
  79. void live(char world[WIDTH+2][HEIGHT+2])
  80. {
  81. int x, y, neighbours;
  82. char tempw[WIDTH+2][HEIGHT+2];
  83.  
  84. for(x = 1; x <= WIDTH; x++)
  85. {
  86. for(y = 1; y <= HEIGHT; y++)
  87. {
  88. // Tel het aantal buren
  89. neighbours = (
  90. (world[x-1][y-1] == LIVE ? 1 : 0) +
  91. (world[x-0][y-1] == LIVE ? 1 : 0) +
  92. (world[x+1][y-1] == LIVE ? 1 : 0) +
  93. (world[x-1][y-0] == LIVE ? 1 : 0) +
  94. (world[x+1][y-0] == LIVE ? 1 : 0) +
  95. (world[x-1][y+1] == LIVE ? 1 : 0) +
  96. (world[x-0][y+1] == LIVE ? 1 : 0) +
  97. (world[x+1][y+1] == LIVE ? 1 : 0));
  98.  
  99. if(neighbours < 2 || neighbours > 3)
  100. tempw[x][y] = DEAD;
  101. else if(neighbours == 3)
  102. tempw[x][y] = LIVE;
  103. else
  104. tempw[x][y] = world[x][y];
  105. }
  106. }
  107. memcpy(world, tempw, (WIDTH+2)*(HEIGHT+2));
  108. }
  109.  
  110. /**
  111. * Zet een cel op levend
  112. */
  113. void setCell(int x, int y, char world[WIDTH+2][HEIGHT+2])
  114. {
  115. world[x][y] = LIVE;
  116. }
  117.  
  118. /**
  119. * Geef een cel in
  120. */
  121. void inputCell(char world[WIDTH+2][HEIGHT+2])
  122. {
  123. int x, y;
  124. cout << "X-Positie van cel (van 0 tot " << WIDTH << "): ";
  125. cin >> x;
  126. cout << "Y-Positie van cel (van 0 tot " << HEIGHT << "): ";
  127. cin >> y;
  128. setCell(x, y, world);
  129. }
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Geen reacties (0)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.026s