-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
167 lines (145 loc) · 3.1 KB
/
game.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
#include "raylib.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
// screen size
const int WIDTH = 1200;
const int HEIGHT = 600;
const int GRID_WIDTH = WIDTH;
const int GRID_HEIGHT = HEIGHT;
void prefillGrid();
void drawGrid();
int cellsFate(int i, int j);
int cellsGrid[GRID_WIDTH][GRID_HEIGHT];
// This function decides the fate of a cell
int cellsFate(int i, int j)
{
int liveNeighbours = 0;
const int neighbours[8][2] = {
{i-1, j-1},
{i-1, j},
{i-1, j+1},
{i, j-1},
{i, j+1},
{i+1, j-1},
{i+1, j},
{i+1, j+1}
};
for (int ne = 0; ne < 8; ne++)
{
int x = neighbours[ne][0];
int y = neighbours[ne][1];
// std::cout << cellsGrid[x][y] << std::endl;
if (!(x < 0 || x > GRID_WIDTH || y < 0 || y > GRID_HEIGHT))
{
if (cellsGrid[x][y] == 1) liveNeighbours++;
// std::cout << "Live Neighbours : "<< liveNeighbours << std::endl;
}
}
// Evil society
if (cellsGrid[i][j] == 1)
{
if (liveNeighbours > 3 || liveNeighbours < 2)
{
// overpopulation or underpopulation
cellsGrid[i][j] = 0;
return 0;
}
return 1;
} else if (cellsGrid[i][j] == 0) {
if (liveNeighbours == 3)
{
cellsGrid[i][j] = 1;
return 1;
}
return 0;
} else return 0;
}
void prefillGrid()
{
for (int i = 0; i < GRID_WIDTH; i++)
{
for (int j = 0; j < GRID_HEIGHT; j++)
{
cellsGrid[i][j] = 0;
}
}
}
void drawGrid()
{
for (int i = 0; i < GRID_WIDTH; i++)
{
for (int j = 0; j < GRID_HEIGHT; j++)
{
if (cellsGrid[i][j] == 1) DrawRectangle(i+64, j+64, 4, 4, PURPLE);
}
}
}
int main(void)
{
prefillGrid();
/*
// samples
cellsGrid[20][89] = 1;
cellsGrid[123][45] = 1;
cellsGrid[430][89] = 1;
cellsGrid[453][45] = 1;
cellsGrid[32][89] = 1;
cellsGrid[353][45] = 1;
*/
InitWindow(WIDTH, HEIGHT, "Cellular Automata");
// init seed
// srand(time(NULL));
// init random points
/*
for (int i = 0; i < 150; i++)
{
int randI = GetRandomValue(0, GRID_WIDTH); // rand() % (GRID_WIDTH);
int randJ = GetRandomValue(0, GRID_HEIGHT); //rand() % (GRID_HEIGHT);
// int offset = rand() % 10;
cellsGrid[randI][randJ] = 1;
// std::cout << cellsGrid[randI][randJ] << std::endl;
}
*/
int generation = 0;
char genMsg[40]; // ~40+ just in case someone plays hard ;)
SetTargetFPS(60);
int play = 0;
while(!WindowShouldClose())
{
BeginDrawing();
ClearBackground(WHITE);
// draw all the cells in the array
sprintf(genMsg, "Generation : %d ", generation);
DrawText(genMsg, 20, 40, 30, MAROON);
if (IsMouseButtonDown(0))
{
Vector2 mousePosition = GetMousePosition();
cellsGrid[(int)mousePosition.x][(int)mousePosition.y] = 1;
// DrawRectangle(mousePosition.x+2, mousePosition.y+2, 3, 3, PURPLE);
// std::cout << mousePosition.x << " " << mousePosition.y << std::endl;
}
if (IsKeyPressed(KEY_P))
{
play = !play;
}
if (play) {
generation++;
for (int i = 0; i < GRID_WIDTH; i++)
{
for (int j = 0; j < GRID_HEIGHT; j++) {
int shouldDraw = cellsFate(i, j);
if (shouldDraw)
{
DrawRectangle(i+64, j+64, 4, 4, PURPLE);
}
}
}
} else {
drawGrid();
}
EndDrawing();
}
CloseWindow();
return 0;
}