Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Francisco Berto Melniski

jogo da vida

Recommended Posts

Boa noite pessoal

estou com dificuldades em implementar o jogo da vida em C

 

Este "jogo" é na realidade um jogo sem jogador, o que quer dizer que sua evolução é determinada pelo seu estado inicial, não necessitando de nenhuma entrada de jogadores humanos. Ele é jogado em um conjunto de células quadradas que seguem ao infinito em todas as direções. Cada célula tem oito "vizinhos", que são as células adjacentes, incluindo as diagonais. Cada célula pode estar em dois estados: "viva" ou "morta". O estado do tabuleiro evolui e se modifica em pequenas passagens de tempo. Os estados de todas as células em um instante são considerados para calcular o estado de todas as células no instante seguinte. Todas as células são atualizadas simultaneamente. As transições dependem apenas do número de vizinhos vivos.

 

As regras são simples:

1. Qualquer célula viva com menos de dois vizinhos vivos morre de solidão.. 

2. Qualquer célula viva com mais de três vizinhos vivos morre de superpopulação.

3. Qualquer célula morta com exatamente três vizinhos vivos se torna uma célula viva.

4. Qualquer célula viva com dois ou três vizinhos vivos continua no mesmo estado para a próxima geração.

É importante entender que todos os nascimentos e mortes ocorrem simultaneamente. Juntos eles constituem uma geração ou, como podemos chamá-los, um "instante" na história da vida completa da configuração inicial.

 

#include <stdio.h>
#define LIN 20
#define COL 20
#define VIVAS 1
#define MORTAS 0


typedef int TableType[LIN][COL];

void printTable(TableType table) {
	int alt, larg;

	for (alt = 0; alt < LIN; alt++) {
		for (larg = 0; larg < COL; larg++) {
			if (table[alt][larg] == VIVAS) {
				printf("0");
			} else {
				printf(".");
			}
		}
		printf("\n");
	}
	printf("\n");
}


// you already have a printTable, no need for this
// clear was a better name
void clearTable(TableType table) {
	int alt, larg;
	for (alt = 0; alt < LIN; alt++) {
		for (larg = 0; larg < COL; larg++) {
			table[alt][larg] = MORTAS;
		}
	}
}

int getNeighborValue(TableType table, int row, int col) {
	if (row < 0 || row >= LIN || col < 0 || col >= COL || table[row][col] != VIVAS )
	{
		return 0;
	} else {
		return 1;
	}
}


int getNeighborCount(TableType table, int row, int col) {
	int neighbor = 0;

	neighbor += getNeighborValue(table, row - 1, col - 1);
	neighbor += getNeighborValue(table, row - 1, col);
	neighbor += getNeighborValue(table, row - 1, col + 1);
	neighbor += getNeighborValue(table, row, col - 1);
	neighbor += getNeighborValue(table, row, col + 1);
	neighbor += getNeighborValue(table, row + 1, col - 1);
	neighbor += getNeighborValue(table, row + 1, col);
	neighbor += getNeighborValue(table, row + 1, col + 1);

	return neighbor;
}

void calculate(TableType tableA) {
	TableType tableB;
	int neighbor, alt, larg;

	for (alt = 0; alt < LIN; alt++) {
		for (larg = 0; larg < COL; larg++) {
			neighbor = getNeighborCount(tableA, alt, larg);
			// change this arund to remove the ? : notation
			if (neighbor==3) {
				tableB[alt][larg] = VIVAS;
			} else if (neighbor == 2 && tableA[alt][larg] == VIVAS) {
				tableB[alt][larg] = VIVAS;
			} else {
				tableB[alt][larg] = MORTAS;
			}
		}
	}
	// used to be swap
	for (alt = 0; alt < LIN; alt++) {
		for (larg = 0; larg < COL; larg++) {
			tableA[alt][larg] = tableB[alt][larg];
		}
	}
}

// user entry is a pain for testing
// here's some code to load test data
void loadTestData(TableType table) {
	// toggle
	table[3][4] = VIVAS;
	table[3][5] = VIVAS;
	table[3][6] = VIVAS;

	// glider
	table[10][4] = VIVAS;
	table[10][5] = VIVAS;
	table[10][6] = VIVAS;
	table[11][6] = VIVAS;
	table[12][5] = VIVAS;
}

int main(void) {
	TableType table;
	clearTable(table);
	loadTestData(table);
	printTable(table);

	for( ; ; ) {
		calculate(table);
		printTable(table);

		system("cls");

	}
}

Alguém ai consegue me dar uma luz?

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.