Game Of Life: display iteration

hey guys
I've recently made a game of life program. And it works. But I have problem of displaying the iteration. I can display the generations one by one, but I am not able to display in one screen. I'm using Linux.

what i can display is for example:

first generation:
- - - - -
- - * - -
- - * - -
- - * - -
- - - - -


second generation:
- - - - -
- * * * -
- - - - -
- - - - -

what i actually want is display these generation like in an animation.

thx in advance.
When I made a thing like this in console, I outputted to a text file. =/

SDL or something would be handy to learn here. You can't really do animation in console mode.
You need to use curses.

Here is some documentation http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

The following demo program should also give you an idea, compile it with g++ filename.cc -lncurses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <curses.h>
#include <vector>
#include <unistd.h>

class array
{
private:
	std::vector<int> v;
	int width, height;
public:
	int &operator()(int x, int y) {
		return v[x+y*width];
	}
	int operator()(int x, int y) const{
		return v[x+y*width];
	}

	const int w() const { return width; }
	const int h() const { return height; }

	array(int w, int h) : v(w*h), width(w), height(h) {}
};

void printArray(const array &a) {
	for(int y=0;y!=a.h();++y) {
		for(int x=0;x!=a.w();++x) {
			mvaddch(y,x,'0'+a(x,y));
		}
	}
	refresh();
}

void updateArray(array &a) {
	array b(a.w(), a.h());

	for(int y=0;y!=a.h();++y) {
		for(int x=0;x!=a.w();++x) {
			bool yes = (a(x,y)==1);
			if(yes) b(x,y) = 0;
			if(yes && x>0) b(x-1,y) = 1;
			if(yes && x<a.w()-1) b(x+1,y) = 1;
			if(yes && y>0) b(x,y-1) = 1;
			if(yes && y<a.h()-1) b(x,y+1) = 1;
		}
	}

	a = b;
}

int main() {
	array a(10,10);

	a(3,4) = 1;

	initscr();

	printArray(a);
	while(true) {
		sleep(1);
		updateArray(a);
		printArray(a);
	}

	endwin();

	return 0;
}
If you really, really want to do the animation in a console you could use ncurses. But it would probably be easier to just find the easiest to use graphics lib you can find and display it with that.
thx for the reply guys. @kev 82. I cant compile ur program. it says


fatal error: curses.h: No such file or directory
compilation terminated.

do i need to download it?
Last edited on
You could try #include <ncurses.h> instead. If that doesn't work you would need to install the appropriate development package, which distribution of Linux is it?
With Ubuntu you could try
sudo apt-get install libncurses5-dev
.
If you use apt (Debian-like):

sudo apt-get install libncurses5-dev

And everything is put into place (hopefully (probably)).
thx everybody! i've made it move by using "system (clear)". so before every new generation, screen is cleared and new generation is displayed and i have used an infinite loop for it.
Last edited on
That's probably the worst solution, but if it works for you...
Topic archived. No new replies allowed.