Conway's game of Life using C++ with SDL


#1

Hi. I need help badly. I am using SDL to create game of life using C++. I have a few problems. I have created the coordinates in the text file. I can’t seem to migrate the coordinates into the grid. I am struggling to write the algorithm for this. I have put in ifstream to read the file but is not working. I am not sure how to migrate this. My source code is here. I have a grid but with no content. It is very urgent as the submission is nearby.

#include<SDL.h>
#include
#include
#include
#include<time.h>
#include
#include<stdlib.h>

#undef main
SDL_Window *window = NULL;
SDL_Window *surface = NULL;

// Defining some constant variables
#define WIDTH 600
#define HEIGHT 600
const int GRID_SIZE = 20;
const int alive_cells = 1;
const int dead_cells = 0;
const int Screen_size = 600;
const int CELL_SIZE = Screen_size / GRID_SIZE;
using namespace std;

//Functions needed to be implemented in order to create the conway game of life
void ReadScenario(int grid[][GRID_SIZE], int size);
void displaygrid(int grid[][GRID_SIZE], int size);
void Init(int grid[][GRID_SIZE], int size);
int count_neighbours(int grid[][GRID_SIZE], int size, int x, int y);
void handling_events(int grid[][GRID_SIZE], int size);
void Next_gen();

bool g_quit = false;

int main()
{
int grid[GRID_SIZE][GRID_SIZE];
cout << "The following are the rules of the Conway’s game of life: " << endl;
cout << “1. Any live cells with fewer than two neighbours dies” << endl;
cout << “2. Any live cells with more than three live neighbours dies” << endl;
cout << “3. Any live cells with two or three live neighbours lives to the next generation” << endl;
cout << “4. Any dead cells with three neighbours will come to alive” << endl;
ReadScenario(grid, GRID_SIZE);

//  A boolean to detect when the user quits the program
bool doquit = false;
// A structure that will capture events (mouse, keyboard, ...)
SDL_Event event;

// --------------------------------------------------------------------------
// Initialization of the GUI
// --------------------------------------------------------------------------
int ret = SDL_Init(SDL_INIT_VIDEO);

if (ret < 0)
{
	cerr << "Can't initialize SDL: " << SDL_GetError() << endl;
	exit(1);
}

// --------------------------------------------------------------------------
// Create a window
// --------------------------------------------------------------------------
SDL_Window *myWindow = SDL_CreateWindow("SDL Test ", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, 0);

if (myWindow == NULL)
{
	cerr << "Can't create a window: " << SDL_GetError() << endl;
	exit(1);
}

// --------------------------------------------------------------------------
// Create the graphics renderer
// Parameters (-1, 0) at the end means "the best available"
//                                          (hardware vs. software support)
// --------------------------------------------------------------------------
SDL_Renderer *interp = SDL_CreateRenderer(myWindow, -1, 0);
if (interp == NULL) {
	cerr << "Can't create the graphics intepreter: " << SDL_GetError() << endl;
	exit(1);
}

// --------------------------------------------------------------------------
// The following code is one-shot drawing but could be inserted in a loop
// to refresh the display
// --------------------------------------------------------------------------
// Pick a pencil color
SDL_SetRenderDrawColor(interp, /*R*/255,/*G*/ 255,/*B*/255,/*A*/255);

// Erase the window with the pencil color
SDL_RenderClear(interp);

// Draw a rectangle
SDL_Rect rectangle; /* This is a structure */
rectangle.x = 20;
rectangle.y = 40;
rectangle.w = 50;
rectangle.h = 30;

// Pick another pencil color
SDL_SetRenderDrawColor(interp, /*R*/255,/*G*/ 0,/*B*/0,/*A*/255);

// Draw a Full rectangle
SDL_RenderFillRect(interp, &rectangle);

// Draw another rectangle
rectangle.x = 80;
rectangle.y = 80;
rectangle.w = 50;
rectangle.h = 30;

// Pick a pencil color
SDL_SetRenderDrawColor(interp, /*R*/0,/*G*/ 255,/*B*/0,/*A*/255);

// Draw an Empty rectangle
SDL_RenderDrawRect(interp, &rectangle);

// Redraw the window - Required to see the changes
SDL_RenderPresent(interp);

// Wait for 500 ms - commented because not useful there but could be used
//    in the final game of life project
//SDL_Delay(500);

// --------------------------------------------------------------------------
// Loop that lets the system process its own events and wait until user quits
// --------------------------------------------------------------------------
while (!g_quit) {
	displaygrid(grid, GRID_SIZE);

	// --------------------------------------------------------------------------
	// End of program cleanup
	// --------------------------------------------------------------------------
	SDL_DestroyWindow(myWindow);
	system("Pause");
}

}

void displaygrid(int grid[][GRID_SIZE], int size)
{
SDL_Window *myWindow = SDL_CreateWindow("SDL Test ", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, 0);
SDL_Renderer *interp = SDL_CreateRenderer(myWindow, -1, 0);

//Setting colour to white for background
SDL_SetRenderDrawColor(interp, 255, 255, 255, SDL_ALPHA_OPAQUE);
//Clear Screen
SDL_RenderClear(interp);

//Setting colour to black for line
SDL_SetRenderDrawColor(interp, 0, 0, 0, SDL_ALPHA_OPAQUE);

//Drawing rows of lines
for (int i = 0; i < size; i++) {
	SDL_RenderDrawLine(interp, 0, CELL_SIZE *i, /*x1,y1*/
		Screen_size, CELL_SIZE *i /*x2,y2*/
	);
}
//Drawing columns of lines
for (int i = 0; i < size; i++) {
	SDL_RenderDrawLine(interp, CELL_SIZE*i, 0,  /*x1,y1*/
		CELL_SIZE*i, Screen_size /*x2,y2*/
	);
}
//Setting draw colour to blue for life box
SDL_SetRenderDrawColor(interp, 0, 0, 255, SDL_ALPHA_OPAQUE);


for (int x = 0; x < size; x++) {
	for (int y = 0; y < size; y++) {
		if (grid[y][x] == alive_cells) {
			SDL_Rect r = {
				x*CELL_SIZE, /*x*/
				y*CELL_SIZE, /*y*/
				CELL_SIZE, /*width*/
				CELL_SIZE /*height*/
			};
			SDL_RenderFillRect(interp, &r);
		}
	}
}
SDL_RenderPresent(interp);

}

//For dead cells
void Init(int grid[][GRID_SIZE], int size)
{
int x, y;
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
grid[y][x] = dead_cells;
}
}

}
//For living neighbours
int count_neighbours(int grid[][GRID_SIZE], int size, int x, int y)
{
int i;
int j;
int count = 0; // Count number of living neighbours
for (int i = y - 1; i <= y + 1; i++) {
for (int j = x - 1; j <= x + 1; j++) {
if (i >= 0 && j >= 0 && i < size && j < size) {
if (grid[i][j] == alive_cells) {
count++;
}
}
}
}
if (grid[y][x] != dead_cells) {
count–;
}
return count;
}

void handling_events(int grid[][GRID_SIZE], int size)
{

}

void Next_gen()
{

}

void ReadScenario(int grid[][GRID_SIZE], int size)
{

int i;
int j;

ifstream myfile("Coordinates.txt");
string line;


if (myfile.is_open()) {
	for (int i = 0; i < size; i++) {
		for (int j = 0; j < size; j++) {
			grid[j][i] = grid[i][j];
			cout << endl;
		}
	}
	while (getline(myfile, line))
		cout << line << '\n';

}
myfile.close();

}
Below are the coordinates from my text file. I need to put this in the grid but I don’t know how to do this.
00000000000000000000
00000000000010000000
00000001000010000000
00001100001100011000
00001000100010001100
00101010101010101010
01010101010101010100
00010001000100010000
01000100010011001100
00110011001100110010
00000000111011000100
01001010101010101010
01100110011001100110
00110001000111001000
01110000000000000000
01011100010001110010
00110000000101000000
00000000001000101000
00000010001000101000
00000000000000000000


#2

Uh… It may be too late, but I am gonna answer anyways.

I had some trouble understanding your code, so I am just gonna explain you how I would fill that grid with the contents of the text file.

You create two variables X and Y to represent the position where you want to put data in your array, and you set them both to 0.

Then, you start reading. You fill your grid at position (X, Y). At each char, you increment your X. At each end of line, you increment your Y and reset your X.

I hope I was clear. Here is a little code to do it(I might have done an error, it does a long time I haven’t used ifstream)

std::string line;
int x = 0, y = 0;

while(getline(myfile, line))
{
    for(int i = 0; i < line.length(); i++)
    {
    if(line[i] == '0') grid[x][y] = 0;
    else if(line[i] == '1')  grid[x][y] = 1;
    else std::cout << "Error: Invalid character(Expected 0 or 1)" << std::endl;
    x++;
    }
x = 0;
y++;
}