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;
}
|