-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
156 lines (122 loc) · 3.8 KB
/
main.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
#include <cstdio>
#include <iostream>
#include <raylib.h>
#include <bits/stdc++.h>
// Header File contains all the Structures and Functions
#include "Structures.h"
// Function to initialize the grid
void InitializeGrid(int** grid, int creature) {
// clear grid
ClearGrid(grid);
switch ( creature ) {
case 1:
SetRandom(grid, 0, 0);
break;
case 2:
SetGlider(grid, 10, 10);
break;
case 3:
SetPulsar(grid, 50, 100);
break;
case 4:
SetGosperGliderGun(grid, 10, 10);
break;
case 5:
SetReplicator(grid, 100, 100);
break;
case 6:
SetExplorer(grid, 10, 10);
break;
case 7:
SetBeacon(grid, 20 , 20);
break;
case 8:
SetAcorn(grid, 120, 120);
break;
}
}
// Function to update the grid based on Conway's rules
void UpdateGrid(int** grid) {
int** newGrid = (int**)malloc(gridSize * sizeof(int*));
for (int i = 0; i < gridSize; i++) {
newGrid[i] = (int*)malloc(gridSize * sizeof(int));
for (int j = 0; j < gridSize; j++) {
int neighbors = 0;
// Count live neighbors
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
int ni = (i + x + gridSize) % gridSize;
int nj = (j + y + gridSize) % gridSize;
neighbors += grid[ni][nj];
}
}
// Apply Conway's rules
if (grid[i][j]) {
neighbors--; // Subtract the cell itself
newGrid[i][j] = (neighbors == 2 || neighbors == 3) ? 1 : 0;
} else {
newGrid[i][j] = (neighbors == 3) ? 1 : 0;
}
}
}
// Copy the new grid back to the original grid
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
grid[i][j] = newGrid[i][j];
}
}
// Free memory for the new grid
for (int i = 0; i < gridSize; i++) {
free(newGrid[i]);
}
free(newGrid);
}
int main() {
int creature = 0;
// clear the screen
system("clear");
std::cout << "\033[31mred Enter what u Creature U want to Place\n" << std::endl;
std::cout << "\033[0m1. Random" << std::endl;
std::cout << "2. Glider" << std::endl;
std::cout << "3. Pulsar" << std::endl;
std::cout << "4. Gosper Glider Gun" << std::endl;
std::cout << "5. Replicator" << std::endl;
std::cout << "6. Explorer" << std::endl;
std::cout << "7. Beacon" << std::endl;
std::cout << "8. Acorn \n\nHere -> : ";
std::cin >> creature;
while ( creature < 1 || creature > 8 ) {
std::cout << "Enter a valid Value\n :" << std::endl;
std::cin >> creature;
}
InitWindow(screenWidth, screenHeight, "Conway's Game of Life");
// Initialize the grid and allocate memory
int** grid = (int**)malloc(gridSize * sizeof(int*));
for (int i = 0; i < gridSize; i++) {
grid[i] = (int*)malloc(gridSize * sizeof(int));
}
InitializeGrid(grid, creature);
SetTargetFPS(10);
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
// Update the grid
UpdateGrid(grid);
// Render the grid
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
if (grid[i][j]) {
DrawRectangle(i * cellSize, j * cellSize, cellSize, cellSize, BLACK);
}
}
}
EndDrawing();
}
// Free memory for the grid
for (int i = 0; i < gridSize; i++) {
free(grid[i]);
}
free(grid);
CloseWindow();
return 0;
}