game-of-life/:wq
2023-11-24 14:05:48 +01:00

123 lines
2.0 KiB
Plaintext

#include <stdio.h>
#include <graph.h>
#define WIDTH 15
#define HEIGHT 15
#define BLOCK_SIZE 50
#define CYCLE 1000000L
void display_map(int map[HEIGHT][WIDTH]) {
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
couleur c;
if (map[i][j])
{
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
}
else
{
ChoisirCouleurDessin(CouleurParComposante(255, 255, 255));
}
RemplirRectangle(
i * BLOCK_SIZE,
j * BLOCK_SIZE,
BLOCK_SIZE,
BLOCK_SIZE
);
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
DessinerRectangle(
i * BLOCK_SIZE,
j * BLOCK_SIZE,
BLOCK_SIZE,
BLOCK_SIZE
);
}
}
}
int get_neighbor(int map[HEIGHT][WIDTH], int x, int y)
{
if (x < 0 || y < 0 || x > WIDTH - 1 || y > HEIGHT - 1) return 0;
return map[x][y];
}
void tick(int map[HEIGHT][WIDTH])
{
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
int count = 0;
count += get_neighbor(map, i - 1, j - 1);
count += get_neighbor(map, i - 1, j);
count += get_neighbor(map, i - 1, j + 1);
count += get_neighbor(map, i, j - 1);
count += get_neighbor(map, i, j + 1);
count += get_neighbor(map, i + 1, j - 1);
count += get_neighbor(map, i + 1, j);
count += get_neighbor(map, i + 1, j + 1);
printf("%d\n", count);
}
}
}
int main()
{
int map[HEIGHT][WIDTH] = {};
int x, y;
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
map[i][j] = 0;
}
}
InitialiserGraphique();
CreerFenetre(0, 0, WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE);
x = _X / BLOCK_SIZE;
y = _Y / BLOCK_SIZE;
display_map(map);
while (1)
{
SourisPosition();
if (ToucheEnAttente()) break;
x = _X / BLOCK_SIZE;
y = _Y / BLOCK_SIZE;
if (SourisCliquee())
{
map[x][y] = !map[x][y];
display_map(map);
}
}
unsigned long suivant = Microsecondes() + CYCLE;
while (1)
{
if (Microsecondes() > suivant)
{
tick(map);
suivant = Microsecondes() + CYCLE;
}
}
FermerGraphique();
return 0;
}