Recently I have been making Conway's Game of Life in c++. When I was finishing GUI of the game in Qt, I realised that I'm not able to make assymetric array (I mean number of rows != number of cols). When I try, program closes immediately with error of getting out of the array. Interesting is fact that if I set dimensions of array in my Board class (backend), row = 15 and cols = 15 and I try to increase number of cols in GUI- nothing happens, but if I increase number of rows and it cross 15- program crashes (if number of rows < 15, nothing happens).
With that knowledge, I came back to backend classes which i wrote few months ago and started setting different combinations of number of rows and cols printing it in cmd. I discovered that if i set number of rows bigger than number of cols, my function
check_index(int x,int y)
display error of indexing, BUT program for some reasons works correctly. If i set number of rows smaller than number of cols, program will compile but display nothing.
Class Board.h:
class Board
{
private:
int m_height;
int m_width;
bool** m_gameboard;
bool** m_temp_gameboard;
bool check_index(int x, int y)const
{
if (x >= 0 && x < m_width && y >= 0 && y < m_height) return true;
else return false;
}
void allocate_gameboard(int width, int height)
{
if (m_gameboard == nullptr)
{
m_gameboard = new bool* [width];
for (int i = 0; i < width; i++)
m_gameboard[i] = new bool[height];
}
for (int i = 0; i < m_width; i++)
{
for (int j = 0; j < m_height; j++)
m_gameboard[i][j] = false;
}
}
void allocate_temp_gameboard(int width, int height)
{
if (m_temp_gameboard == nullptr)
{
m_temp_gameboard = new bool* [width];
for (int i = 0; i < width; i++)
m_temp_gameboard[i] = new bool[height];
}
for (int i = 0; i < m_width; i++)
{
for (int j = 0; j < m_height; j++)
m_temp_gameboard[i][j] = false;
}
}
void deallocate_gameboard()
{
if (m_gameboard != nullptr)
{
for (int i = 0; i < m_width; i++)
delete[] m_gameboard[i];
delete[] m_gameboard;
}
}
void deallocate_temp_gameboad()
{
if (m_temp_gameboard != nullptr)
{
for (int i = 0; i < m_width; i++)
delete[] m_temp_gameboard[i];
delete[] m_temp_gameboard;
}
}
public:
Board(int, int);
Board();
void set_random() const;
void print_gameboard() const;
void print_temp_gameboard() const;
void add_cell(int, int);
void temp_add_cell(int, int);
void remove_cell(int, int);
void temp_remove_cell(int, int);
void copy_GameBoard_to_Temp();
void copy_Temp_to_Gameboard();
void reset_array(char);
int check_neighborhood(int, int);
int GetHeight();
int GetWidth();
bool** GetGameBoard();
~Board();
};
Board.cpp:
Constructors:
Board::Board(int height, int width) : m_width(width), m_height(height)
{
allocate_gameboard(width, height);
allocate_temp_gameboard(width, height);
}
Board::Board()
{
m_width = 10;
m_height = 10;
allocate_gameboard(m_width, m_height);
allocate_temp_gameboard(m_width, m_height);
}
adding and removing functions:
void Board::add_cell(int x, int y)
{
if (check_index(x, y)) m_gameboard[x][y] = true;
else std::cout << "Add_cell:\t invalid cell index\t x: " << x << " y: " << y << std::endl;
}
void Board::temp_add_cell(int x, int y)
{
if (check_index(x, y)) m_temp_gameboard[x][y] = true;
else std::cout << "Add_temp_cell:\t invalid cell index\t x: " << x << " y: " << y << std::endl;
}
void Board::remove_cell(int x, int y)
{
if (check_index(x, y)) m_gameboard[x][y] = false;
else std::cout << "remove_cell:\t invalid cell index\t x: " << x << " y: " << y << std::endl;
}
void Board::temp_remove_cell(int x, int y)
{
if (check_index(x, y)) m_temp_gameboard[x][y] = false;
else std::cout << "remove_temp_cell:\t invalid cell index\t x: " << x << " y: " << y << std::endl;
}
check_neighborhood function:
int Board::check_neighborhood(int x, int y)
{
if (check_index(x, y) == true)
{
int counter_of_true_states = 0;
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
if (check_index(x + i, y + j) == true)
{
if (m_gameboard[x + i][y + j] == true and (j or i) != 0) counter_of_true_states++;
}
else
continue;
}
}
return counter_of_true_states;
}
}
Class GameController.cpp:
NextGeneration fnction:
void GameController::NextGeneration()
{
m_GameBoard.copy_GameBoard_to_Temp();
bool** active_states_detector = m_GameBoard.GetGameBoard();
for (int i = 0; i < m_GameBoard.GetHeight(); i++)
{
for (int j = 0; j < m_GameBoard.GetWidth(); j++)
{
if (active_states_detector[i][j] == true)
{
if (m_GameBoard.check_neighborhood(i, j) != 3 and m_GameBoard.check_neighborhood(i, j) != 2)
m_GameBoard.temp_remove_cell(i, j);
}
else if (active_states_detector[i][j] == false)
{
if (m_GameBoard.check_neighborhood(i, j) == 3)
m_GameBoard.temp_add_cell(i, j);
}
}
}
m_GameBoard.print_temp_gameboard();
m_GameBoard.reset_array('G');
m_GameBoard.copy_Temp_to_Gameboard();
m_GameBoard.reset_array('T');
std::cout << std::endl << "=====================" << std::endl << std::endl;
}
thanks in advance!