#include #include #define MAX_H 20 #define MAX_W 79 using namespace std; class Life_Board { friend ostream &operator<<(ostream& os, const Life_Board& lb); public: Life_Board(int width=MAX_W, int height=MAX_H);\ ~Life_Board(); void Set(int w, int h, bool v=true); void Run(void); protected: bool** World; int W; int H; bool Live(int w, int h); }; Life_Board::Life_Board(int width, int height) { W = width; H = height; World = new bool*[width]; for(int w=0; w < width; w++) { World[w] = new bool[height]; for(int h=0; h < height; h++) World[w][h] = false; } } Life_Board::~Life_Board() { int w; for(w=0; w < W; w++) delete [] World[w]; delete [] World; } void Life_Board::Set(int w, int h, bool v) { if ((w < W) && (h < H)) World[w][h] = v; } void Life_Board::Run(void) { bool** new_world; int h, w; new_world = new bool*[W]; for(w=0; w < W; w++) new_world[w] = new bool[H]; for(w=0; w < W; w++) for(h=0; h < H; h++) new_world[w][h] = Live(w,h); for(w=0; w < W; w++) for(h=0; h < H; h++) Set(w, h, new_world[w][h]); for(w=0; w < W; w++) delete [] new_world[w]; delete [] new_world; } bool Life_Board::Live(int w, int h) { int count = 0; int u, l, r, d; if (w == 0) { l = W-1; r = w+1; } else if (w == W-1) { l = w-1; r = 0; } else { l = w-1; r = w+1; } if (h == 0) { u = H-1; d = h+1; } else if (h == H-1) { u = h-1; d = 0; } else { u = h-1; d = h+1; } /* if (w > 0) { if (World[w-1][h]) count++; if (h > 0) { if (World[w-1][h-1]) count++; } else { if (World[w-1][H-1]) count++; } } else { if (World[W-1][h]) count++; if (h > 0) { if (World[W-1][h-1]) count++; } else { if (World[W-1][H-1]) count++; } } if (w < W-1) { if (World[w+1][h]) count++; if (h < H-1) { if (World[w+1][h+1]) count++; } else { if (World[w+1][0]) count++; } } else { if (World[0][h]) count++; if (h < H-1) { if (World[0][h+1]) count++; } else { if (World[0][0]) count++; } } if (h > 0) { if (World[w][h-1]) count++; if (w < W-1) { if (World[w+1][h-1]) count++; } else { if (World[0][h-1]) count++; } } else { if (World[w][H-1]) count++; if (w < W-1) { if (World[w+1][H-1]) count++; } else { if (World[0][H-1]) count++; } } if (h < H-1) { if (World[w][h+1]) count++; if (w > 0) { if (World[w-1][h+1]) count++; } else { if (World[W-1][h+1]) count++; } } else { if (World[w][0]) count++; if (w > 0) { if (World[w-1][0]) count++; } else { if (World[W-1][0]) count++; } } */ if (World[l][u]) count++; if (World[w][u]) count++; if (World[r][u]) count++; if (World[l][h]) count++; if (World[r][h]) count++; if (World[l][d]) count++; if (World[w][d]) count++; if (World[r][d]) count++; // if (World[w][h]) DON'T COUNT CURRENT CELL // count++; if ((count == 3) || ((count == 2) && (World[w][h]))) return true; else return false; } ostream &operator<<(ostream& os, const Life_Board& lb) { os << "W = " << lb.W << " H = " << lb.H << endl; for(int h=0; h < lb.H; h++) { for(int w=0; w < lb.W; w++) if (lb.World[w][h] == true) os << "*"; else os << " "; os << endl; } return os; } void main (void) { Life_Board lb; int i; srand((unsigned) time(NULL)); for(i=0; i < 200; i++) lb.Set(rand() % MAX_W, rand() % MAX_H); for(i=1; true; i++) { system("cls"); cout << lb; cout << "Round = " << i << " "; system("pause"); lb.Run(); } }