Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- const int ROWS = 22;
- const int COLS = 42;
- const int GAME_ROWS(20);
- const int GAME_COLS(40);
- const char LIVE = 'X'; // Living Cells
- const char DEAD = '.'; // Dead cells
- char NewBoard[ROWS][COLS];
- char board[ROWS][COLS];
- void LifeBoard(char board[][COLS]);
- void copy(char board[][COLS]);
- void NextStage(char board[][COLS]);
- void fillBlinker(int startRow, int startCol);
- void fillGlider(int startRow, int startCol);
- void fillCannon(int startRow, int startCol);
- int main () {
- int startRow;
- int startCol;
- int shape;
- std::cout << "This is Conway's Game of Life! " << std::endl;
- std::cout << "Please enter your starting row (1-20): " << std::endl;
- std::cin >> startRow;
- std::cout << "Please Enter your starting column (1-40): " << std::endl;
- std::cin >> startCol;
- std::cout << "Please select your starting shape." << std::endl;
- std::cout << "Enter 1 for Blinker." << std::endl;
- std::cout << "Enter 2 for Glider." << std::endl;
- std::cout << "Enter 3 for Cannon." << std::endl;
- std::cin >> shape;
- if(shape == 1)
- {
- fillBlinker(startRow, startCol);
- }
- if(shape == 2)
- {
- fillGlider(startRow, startCol);
- }
- if(shape == 3)
- {
- fillCannon(startRow, startCol);
- }
- do {
- NextStage(board);
- LifeBoard(board);
- usleep(250000);
- } while (true);
- return 0;
- }
- void LifeBoard(char board[][COLS])
- {
- for(int i = 1; i <= ROWS-2; i++)
- {
- for(int j=1; j <= COLS-2; j++)
- {
- if(board[i][j] == LIVE)
- {
- std::cout << 'X';
- }
- else
- {
- std::cout << '.';
- }
- //board[i][j] = DEAD;
- //std::cout << board[i][j];
- }
- std::cout << std::endl;
- }
- //std::cout << std::endl;
- }
- void NextStage(char board[][COLS])
- {
- for(int i = 0; i < ROWS; i++)
- {
- for(int j =0; j < COLS; j++)
- {
- int total = 0;
- if(board[i][j+1] == LIVE)
- { total++; }
- if(board[i+1][j+1] == LIVE)
- { total++; }
- if(board[i+1][j] == LIVE)
- { total++; }
- if(board[i+1][j-1] == LIVE)
- { total++; }
- if(board[i][j-1] == LIVE)
- { total++;}
- if(board[i-1][j-1] == LIVE)
- { total++; }
- if(board[i-1][j] == LIVE)
- { total++; }
- if(board[i-1][j+1] == LIVE)
- { total++; }
- NewBoard[i][j] = DEAD;
- if(board[i][j] == LIVE && total < 2) // rule 1
- { NewBoard[i][j] = DEAD; }
- else if(board[i][j] == LIVE && (total==2 || total == 3)) // rule 2
- { NewBoard[i][j] = LIVE; }
- else if(board[i][j] == LIVE && total > 3) // rule 3
- { NewBoard[i][j] == DEAD; }
- else if(board[i][j] == DEAD && total == 3) // rule 4
- { NewBoard[i][j] = LIVE; }
- }
- }
- for (int i = 0; i < ROWS; i++)
- {
- for(int j = 0; j < COLS; j++)
- {
- board[i][j] = NewBoard[i][j];
- }
- }
- }
- void fillBlinker(int rows, int cols)
- {
- board[rows][cols] = LIVE;
- board[rows][cols+1] = LIVE;
- board[rows][cols+2] = LIVE;
- }
- void fillGlider(int rows, int cols)
- {
- board[rows][cols] = LIVE;
- board[rows+1][cols] = LIVE;
- board[rows][cols+1] = LIVE;
- board[rows+3][cols+2] = LIVE;
- board[rows+2][cols+3] = LIVE;
- board[rows+3][cols+3] = LIVE;
- }
- void fillCannon(int rows, int cols)
- {
- board[rows][cols] = LIVE;
- board[rows+1][cols] = LIVE;
- board[rows+1][cols-2]= LIVE;
- board[rows+5][cols]= LIVE;
- board[rows+6][cols] = LIVE;
- board[rows+2][cols-3] = LIVE;
- board[rows+2][cols-4] = LIVE;
- board[rows+3][cols-4] = LIVE;
- board[rows+3][cols-3] = LIVE;
- board[rows+4][cols-3] = LIVE;
- board[rows+4][cols-4] = LIVE;
- board[rows+5][cols-2] = LIVE;
- board[rows+2][cols+10] = LIVE;
- board[rows+3][cols+10] = LIVE;
- board[rows+2][cols+11] = LIVE;
- board[rows+3][cols+11] = LIVE;
- board[rows+5][cols-7] = LIVE;
- board[rows+4][cols-8] = LIVE;
- board[rows+5][cols-8] = LIVE;
- board[rows+6][cols-8] = LIVE;
- board[rows+3][cols-9] = LIVE;
- board[rows+7][cols-9] = LIVE;
- board[rows+5][cols-10] = LIVE;
- board[rows+2][cols-11] = LIVE;
- board[rows+8][cols-11] = LIVE;
- board[rows+2][cols-12] = LIVE;
- board[rows+8][cols-12] = LIVE;
- board[rows+3][cols-13] = LIVE;
- board[rows+7][cols-13] = LIVE;
- board[rows+4][cols-14] = LIVE;
- board[rows+5][cols-14] = LIVE;
- board[rows+6][cols-14] = LIVE;
- board[rows+4][cols-23] = LIVE;
- board[rows+5][cols-23] = LIVE;
- board[rows+4][cols-24] = LIVE;
- board[rows+5][cols-24] = LIVE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement