Game of life c ++

0

Hello! I'm developing the game of life and I'm not getting the rules applied correctly (I do not know if it's because of the function it prints or the function that checks the rules). For those who do not know, I'll tell you a bit about the game below.

From an initial configuration of cells placed in a 2D tray, 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, these being all cells adjacent. In addition they have two possible states: dead or alive. These states are modified at each moment of the game, in order to be always in accordance with the rules. Each update of the state of all cells in one 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 less 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 live neighbors is still in the same state for the next generation.

Below are codes only from functions that check / print the array in the .hpp and matrix.cpp files. If necessary, I'll 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";
}
}
    
asked by anonymous 08.09.2017 / 20:49

0 answers