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