/OneLoneCoder_GameOfLife.cpp

https://github.com/OneLoneCoder/videos · C++ · 170 lines · 67 code · 27 blank · 76 comment · 14 complexity · 620e44be8bc38130446de00abcec7ce4 MD5 · raw file

  1. /*
  2. OneLoneCoder.com - What are Cellular Automata? John Conway's Game Of Life
  3. "Get lost..." - @Javidx9
  4. License
  5. ~~~~~~~
  6. Copyright (C) 2018 Javidx9
  7. This program comes with ABSOLUTELY NO WARRANTY.
  8. This is free software, and you are welcome to redistribute it
  9. under certain conditions; See license for details.
  10. Original works located at:
  11. https://www.github.com/onelonecoder
  12. https://www.onelonecoder.com
  13. https://www.youtube.com/javidx9
  14. GNU GPLv3
  15. https://github.com/OneLoneCoder/videos/blob/master/LICENSE
  16. From Javidx9 :)
  17. ~~~~~~~~~~~~~~~
  18. Hello! Ultimately I don't care what you use this for. It's intended to be
  19. educational, and perhaps to the oddly minded - a little bit of fun.
  20. Please hack this, change it and use it in any way you see fit. You acknowledge
  21. that I am not responsible for anything bad that happens as a result of
  22. your actions. However this code is protected by GNU GPLv3, see the license in the
  23. github repo. This means you must attribute me if you use it. You can view this
  24. license here: https://github.com/OneLoneCoder/videos/blob/master/LICENSE
  25. Cheers!
  26. Background
  27. ~~~~~~~~~~
  28. Sometimes, its worth reflecting on the fact that from simple things can emerge
  29. significant beauty. This is an implementaion of Game Of Life that runs in the console
  30. Author
  31. ~~~~~~
  32. Twitter: @javidx9
  33. Blog: www.onelonecoder.com
  34. Video:
  35. ~~~~~~
  36. https://youtu.be/E7CxMHsYzSs
  37. Last Updated: 14/07/2017
  38. */
  39. #include <iostream>
  40. using namespace std;
  41. #include "olcConsoleGameEngine.h"
  42. class OneLoneCoder_GameOfLife : public olcConsoleGameEngine
  43. {
  44. public:
  45. OneLoneCoder_GameOfLife()
  46. {
  47. m_sAppName = L"Game Of Life";
  48. }
  49. private:
  50. int *m_output;
  51. int *m_state;
  52. protected:
  53. // Called by olcConsoleGameEngine
  54. virtual bool OnUserCreate()
  55. {
  56. m_output = new int[ScreenWidth() * ScreenHeight()];
  57. m_state = new int[ScreenWidth() * ScreenHeight()];
  58. memset(m_output, 0, ScreenWidth() * ScreenHeight() * sizeof(int));
  59. memset(m_state, 0, ScreenWidth() * ScreenHeight() * sizeof(int));
  60. for (int i = 0; i < ScreenWidth()*ScreenHeight(); i++)
  61. m_state[i] = rand() % 2;
  62. auto set = [&](int x, int y, wstring s)
  63. {
  64. int p = 0;
  65. for (auto c : s)
  66. {
  67. m_state[y*ScreenWidth() + x + p] = c == L'#' ? 1 : 0;
  68. p++;
  69. }
  70. };
  71. /*
  72. // R-Pentomino
  73. set(80, 50, L" ## ");
  74. set(80, 51, L" ## ");
  75. set(80, 52, L" # ");
  76. // Gosper Glider Gun
  77. set(60, 45, L"........................#............");
  78. set(60, 46, L"......................#.#............");
  79. set(60, 47, L"............##......##............##.");
  80. set(60, 48, L"...........#...#....##............##.");
  81. set(60, 49, L"##........#.....#...##...............");
  82. set(60, 50, L"##........#...#.##....#.#............");
  83. set(60, 51, L"..........#.....#.......#............");
  84. set(60, 52, L"...........#...#.....................");
  85. set(60, 53, L"............##.......................");
  86. // Infinite Growth
  87. set(20, 50, L"########.#####...###......#######.#####");
  88. */
  89. return true;
  90. }
  91. // Called by olcConsoleGameEngine
  92. virtual bool OnUserUpdate(float fElapsedTime)
  93. {
  94. //this_thread::sleep_for(50ms);
  95. //if (!m_keys[VK_SPACE].bReleased)
  96. // return true;
  97. auto cell = [&](int x, int y)
  98. {
  99. return m_output[y * ScreenWidth() + x];
  100. };
  101. // Store output state
  102. for (int i = 0; i < ScreenWidth()*ScreenHeight(); i++)
  103. m_output[i] = m_state[i];
  104. for (int x = 1; x < ScreenWidth() - 1; x++)
  105. for (int y = 1; y < ScreenHeight() - 1; y++)
  106. {
  107. // The secret of artificial life =================================================
  108. int nNeighbours = cell(x - 1, y - 1) + cell(x - 0, y - 1) + cell(x + 1, y - 1) +
  109. cell(x - 1, y + 0) + 0 + cell(x + 1, y + 0) +
  110. cell(x - 1, y + 1) + cell(x + 0, y + 1) + cell(x + 1, y + 1);
  111. if (cell(x, y) == 1)
  112. m_state[y*ScreenWidth() + x] = nNeighbours == 2 || nNeighbours == 3;
  113. else
  114. m_state[y*ScreenWidth() + x] = nNeighbours == 3;
  115. // ===============================================================================
  116. if (cell(x, y) == 1)
  117. Draw(x, y, PIXEL_SOLID, FG_WHITE);
  118. else
  119. Draw(x, y, PIXEL_SOLID, FG_BLACK);
  120. }
  121. return true;
  122. }
  123. };
  124. int main()
  125. {
  126. // Seed random number generator
  127. srand(clock());
  128. // Use olcConsoleGameEngine derived app
  129. OneLoneCoder_GameOfLife game;
  130. game.ConstructConsole(160, 100, 8, 8);
  131. game.Start();
  132. return 0;
  133. }