0
Hello ! I am developing the game of life and I am not managing to get the rules applied correctly(I do not know if it is due to the function that prints or the function that checks the rules). For those who do not know, I will talk a little about the game below.
From an initial configuration of cells placed in a 2D board, one can observe the evolution of the states of the cells through the passage of time, according to the defined rules. Each cell has 8 neighbors, all of which are adjacent cells. In addition, they have two possible states: living or dead. These states are modified at every moment of the game, so that it is always in accordance with the rules. Each state update of all cells in an instant of time constitutes one generation. From these concepts it is possible to understand the four basic rules of the Game.
Rules: Any living cell with fewer than two living neighbors dies of loneliness. Any living cell with more than three living neighbors dies of overpopulation. Any dead cell with exactly three living neighbors becomes a living cell. Any living cell with two or three living neighbors remains in the same state for the next generation.
Below follow the codes only of the functions that verify/print the matrix in the.hpp matrix and.cpp matrix files. If necessary, I send the rest of the code.
#ifndef MATRIZ_HPP
#define MATRIZ_HPP
#include <iostream>
#include <string.h>
using namespace std;
class Matriz{
private:
char matriz[80][80];
char prox_geracao[80][80];
int quantidade_linhas;
int quantidade_colunas;
public:
Matriz();
int quantidade_geracoes;
int escolher_forma();
void insere_Linhas();
int getLinhas();
void insere_Colunas();
int getColunas();
void cria_Matriz();
void verifica_regras();
void imprime_matriz();
};
#endif
void Matriz::verifica_regras(){
int linhas = getLinhas();
int colunas = getColunas();
int vizinhos =0;
int i=0;
int j=0;
memcpy(prox_geracao, matriz , sizeof(prox_geracao[80]));
for(i=0;i<linhas;i++)
{
prox_geracao[i][j] = matriz[i][j];
for(j=0;j<colunas;j++)
{
vizinhos =0;
for(int k=-1;k<2;k++)
{
for(int l=-1; l<2; l++)
{
if(matriz[i+k][j+l] == 'o')
vizinhos++;
}
}
if((vizinhos < 2 || vizinhos > 3) && matriz[i][j] == 'o' )
prox_geracao[i][j] = '-';
else if(vizinhos == 3 && matriz[i][j] == '-' ){
prox_geracao[i][j] = 'o';
}
else if((vizinhos == 2 || vizinhos == 3) && matriz[i][j] == 'o'){
prox_geracao[i][j] = 'o';
}
else
prox_geracao[i][j] = matriz[i][j];
}
}
memcpy(matriz, prox_geracao , sizeof(matriz[80]));
}
void Matriz::imprime_matriz(){
int linhas = getLinhas();
int colunas = getColunas();
cout << "\n";
for(int i=0 ; i<linhas ; i++){
for(int j=0; j<colunas ; j++){
cout << prox_geracao[i][j] ;
}
cout << "\n";
}
}
And what is the doubt?
– Maniero
I don’t know where the problem is because the rules are not working properly
– Vitor Leal
What problem? In the text it is not clear what problem you speak
– Jefferson Quesado
Neighboring elements are not being counted correctly
– Vitor Leal
Code indentation is a good... Improves readability! Tip: read the Quick Guide to Good Practice in C++.
– KelvinS