I am trying to create Conway's game of life in C using raylab. I have made the manual version which works fine which is below but when I try to implement the version in which the user clicks the cell and when clicked space button the simulation starts, it fails.
MANUAL WORKING VERSION
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include "raylib.h"
#include "raymath.h"
int main()
{
int mat[20][20] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int buffer[20][20];
int up, down, left, right;
int life;
int k, i, j;
int COLS = 20;
int ROWS = 20;
int screenWidth = 600;
int screenHeight = 600;
int cellWidth = screenWidth / COLS;
int cellHeight = screenHeight / ROWS;
SetTargetFPS(2);
InitWindow(screenWidth, screenHeight, "trial render");
while (!WindowShouldClose())
{
BeginDrawing();
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
buffer[i][j] = 0;
}
}
// default temp mat
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
buffer[i][j] = 0;
}
}
for (k = 0; k < 4; k++)
{
// sleep(1);
// system("@cls||clear");
ClearBackground(WHITE);
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
right = (j + 1) % 20; // 19->0 2
left = ((j - 1) + 20) % 20; // 0->19 1->20
up = ((i - 1) + 20) % 20; // 0->19 1->20
down = (i + 1) % 20; // 20-->1 19-->0
// add 8 dir
life = mat[down][j] + mat[i][left] + mat[up][left] + mat[up][j] + mat[up][right] + mat[i][right] + mat[down][right] + mat[down][left];
if (mat[i][j] == 1 && (life < 2 || life > 3))
{
buffer[i][j] = 0;
}
else if (mat[i][j] == 1 && (life == 2 || life == 3))
{
buffer[i][j] = 1;
}
else if (mat[i][j] == 0 && life == 3)
{
buffer[i][j] = 1;
}
}
}
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
mat[i][j] = buffer[i][j];
}
}
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
if (mat[i][j] == 0)
{
DrawRectangleLines(i * cellWidth, j * cellHeight, cellWidth, cellHeight, WHITE);
}
else
{
DrawRectangle(i * cellWidth, j * cellHeight, cellWidth, cellHeight, BLACK);
}
DrawRectangleLines(i * cellWidth, j * cellHeight, cellWidth, cellHeight, GRAY);
}
}
EndDrawing();
}
}
CloseWindow();
return 0;
}
NOT WORKING HELP
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include "raylib.h"
#include "raymath.h"
int main()
{
int mat[20][20];
int buffer[20][20];
int up, down, left, right;
int life;
int k, i, j;
int COLS = 20;
int ROWS = 20;
int screenWidth = 600;
int screenHeight = 600;
int cellWidth = screenWidth / COLS;
int cellHeight = screenHeight / ROWS;
SetTargetFPS(2);
InitWindow(screenWidth, screenHeight, "trial render");
while (!WindowShouldClose())
{
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
mat[i][j] = 0;
}
}
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
buffer[i][j] = 0;
}
}
// alive cell on the mouse click------------------------------------------------------
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
Vector2 mPos = GetMousePosition();
int x = mPos.x;
int y = mPos.y;
mat[x][y] = 1;
}
// default temp mat
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
buffer[i][j] = 0;
}
}
//---------------------------------------------------------------------
// start
if (IsKeyPressed(KEY_SPACE))
{
for (k = 0; k < 4; k++)
{
ClearBackground(WHITE);
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
right = (j + 1) % 20; // 19->0 2
left = ((j - 1) + 20) % 20; // 0->19 1->20
up = ((i - 1) + 20) % 20; // 0->19 1->20
down = (i + 1) % 20; // 20-->1 19-->0
// add 8 dir
life = mat[down][j] + mat[i][left] + mat[up][left] + mat[up][j] + mat[up][right] + mat[i][right] + mat[down][right] + mat[down][left];
if (mat[i][j] == 1 && (life < 2 || life > 3))
{
buffer[i][j] = 0;
}
else if (mat[i][j] == 1 && (life == 2 || life == 3))
{
buffer[i][j] = 1;
}
else if (mat[i][j] == 0 && life == 3)
{
buffer[i][j] = 1;
}
}
}
if (IsKeyDown(KEY_A))
{
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
mat[i][j] = buffer[i][j];
}
}
}
// BeginDrawing();
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
if (mat[i][j] == 0)
{
DrawRectangleLines(i * cellWidth, j * cellHeight, cellWidth, cellHeight, WHITE);
}
else
{
DrawRectangle(i * cellWidth, j * cellHeight, cellWidth, cellHeight, BLACK);
}
DrawRectangleLines(i * cellWidth, j * cellHeight, cellWidth, cellHeight, GRAY);
}
}
EndDrawing();
}
}
}
CloseWindow();
return 0;
}
The screen is not rendering in the last version and I am trying to solve this.
The mouse position is reported in screen units, you should divide them to get cell coordinates and you should test that the resulting coordinates are inside the matrix to avoid undefined behavior writing beyond the array boundaries. Also swap the index variables used for the matrix coordinates as
y
is a row number andx
a column:The logic is somewhat flawed: in the event loop you should
track mouse clicks and change the cell value. Beware that your code has rows and columns swapped;
track space bar presses and toggle the game running state using a a boolean
game_on
initially false;run the game logic only if the game is running;
fix the x and y coordinate computations when drawing the game for consistency with the source code matrix.
Here is a modified version for you to study: