Ir para conteúdo

POWERED BY:

Arquivado

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

Baf0z

codigo do jogo da vida em c

Recommended Posts

Boas...eu precisava urgentemente do codigo em c do jogo da vida...vejo na net muitos mas tudo em c++ preciso mesmo em c...se alguem poder ajudar agredecia...

Compartilhar este post


Link para o post
Compartilhar em outros sites

Voce já fez algo? Quais as suas duvidas?

Compartilhar este post


Link para o post
Compartilhar em outros sites

A kate (##c - freenode) colocou no site dela uma versão:

 

/* $Id: life.c 503 2010-03-15 14:18:03Z kate $ */

/*
 * John Conway's Game of Life.
 *
 * This is written for POSIX, using Curses, in C99. Resizing of the terminal is
 * not supported.
 *
 * By convention in this program, x is the horizontal coordinate and y is
 * vertical. There correspond to the width and height respectively.
 */

#include <curses.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>

unsigned int cellw;
unsigned int cellh;

int cursor;	/* cursor visibility */

static void printcells(bool cells[cellw][cellh], unsigned long int g) {
	unsigned int x;
	unsigned int y;

	for (y = 0; y < cellh; y++) {
		for (x = 0; x < cellw; x++) {
			mvaddch(y, x, cells[x][y] ? 'O' : ' ');
		}
	}

	/* Display generation */
	mvprintw(0, 0, "%ld ", g);

	refresh();
}

static void fillcells(bool cells[cellw][cellh], FILE *fp) {
	unsigned int x;
	unsigned int y;
	unsigned int i;

	/* Empty the board */
	for (y = 0; y < cellh; y++) {
		for (x = 0; x < cellw; x++) {
			cells[x][y] = false;
		}
	}

	/*
	 * With no input file given, default to the r-pentomino.
	 */
	if (fp == NULL) {
		struct {
			int x;
			int y;
		} rpent[] = {
			          { 1, 0 }, { 2, 0 },
			{ 0, 1 }, { 1, 1 },
			          { 1, 2 }
		};

		for (i = 0; i < sizeof rpent / sizeof *rpent; i++) {
			x = rpent[i].x + cellw / 2;
			y = rpent[i].y + cellh / 2;

			/* TODO bounds-check */
			cells[x][y] = true;
		}

		return;
	}


	/*
	 * Read the given file onto the board. This is the widely-used RLE format.
	 */
	int c;

	/* Skip header line */
	while ((c = getc(fp)) != EOF) {
		if (c == '\n') {
			break;
		}
	}

	x = y = 0;
	i = 0;
	while ((c = getc(fp)) != EOF) {
		switch (c) {
		case '!':	/* EOF */
			return;

		case '$':	/* EOL */
			if (i == 0) {
				i = 1;
			}

			y += i;
			x = 0;
			i = 0;
			continue;

		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			i *= 10;
			i += c - '0';
			continue;

		case 'b':	/* blank */
		case 'o': case 'x': case 'y': case 'z':
			if (i == 0) {
				i = 1;
			}

			do {
				if (x >= cellw || y >= cellh) {
					fprintf(stderr, "Out of bounds\n");
					exit(EXIT_FAILURE);
				}

				cells[x][y] = c != 'b';
				x++;
			} while (i-- > 1);
			continue;

		default:	/* unrecognised, or whitespace */
			continue;
		}
	}
}

static unsigned int wrap(unsigned int range, int i) {
	int n;

	if (i < 0) {
		i += range;
	}
	n = i % range;

	return n;
}

static void updatecells(bool cells[cellw][cellh]) {
	unsigned int x;
	unsigned int y;
	bool newcells[cellw][cellh];

	/* The Moore neighbourhood */
	struct {
		int x;
		int y;
	} moore[] = {
		{ -1, -1 }, {  0, -1 }, {  1, -1 },
		{ -1,  0 },             {  1,  0 },
		{ -1,  1 }, {  0,  1 }, {  1,  1 }
	};

	for (y = 0; y < cellh; y++) {
		for (x = 0; x < cellw; x++) {
			unsigned int n;
			unsigned int count = 0;

			/* Count neighbouring cells which are set */
			for (n = 0; n < sizeof moore / sizeof *moore; n++) {
				unsigned int nx = wrap(cellw, x + moore[n].x);
				unsigned int ny = wrap(cellh, y + moore[n].y);

				count += cells[nx][ny];
			}

			if (count < 2) {
				/* Die of loneliness */
				newcells[x][y] = false;
			} else if (count > 3) {
				/* Die of overcrowding */
				newcells[x][y] = false;
			} else if (count == 3) {
				/* Birth */
				newcells[x][y] = true;
			} else {
				/* Stay as-is */
				newcells[x][y] = cells[x][y];
			}
		}
	}

	/* Write the new generation over the previous cells */
	memcpy(cells, newcells, sizeof newcells);
}

static void cleanup(void) {
	curs_set(cursor);
	/* TODO anything else to clean up here? */
	echo();
}

int main(int argc, char *argv[]) {
	unsigned long int g;	/* generations */
	FILE *fp = NULL;

	/* TODO cli options to run to a specific generation, etc */
	/* TODO "-" or no filename, for stdin */

	if (argc > 2) {
		fprintf(stderr, "usage: life [<file>]\n");
		return EXIT_FAILURE;
	}

	if (argc == 2) {
		fp = fopen(argv[1], "r");
		if (fp == NULL) {
			perror("fopen");
			return EXIT_FAILURE;
		}
	}

	/* TODO anything else to init here? */
	initscr();
	cellw = (unsigned int) COLS;
	cellh = (unsigned int) LINES;

	noecho();
	cursor = curs_set(0);

	atexit(cleanup);

	bool cells[cellw][cellh];
	fillcells(cells, fp);

	/* TODO noecho? */

	/* We make use of this to sleep for a short period of time in getch() */
	timeout(100);
	cbreak();
	clear();

	for (g = 0; g <= ULONG_MAX; g++) {
		printcells(cells, g);

		/* TODO space to pause */
		/* TODO q to quit */

		if (getch() != ERR) {
			return EXIT_SUCCESS;
		}

		updatecells(cells);
	}

	return EXIT_SUCCESS;
}

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.