Created
January 22, 2020 03:14
-
-
Save mustafat0k/d98c82ee6a6e402ab24f4a43c0b5270f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Conway's Game of Life | |
// The universe of Conway's Game of Life is an infinite, two-dimensional orthogonal grid of cells, each of which is in one of two possible states: alive or dead. Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent (upper left, top, upper right, right, lower right, bottom, bottom left, left). At each step in time, the following transitions occur: | |
// Any live cell with fewer than two live neighbors dies due to under-population. | |
// Any live cell with two or three live neighbors lives on to the next generation (no change). | |
// Any live cell with more than three live neighbors dies due to overpopulation. | |
// Any dead cell with exactly three live neighbors becomes a live cell by reproduction. | |
// The initial pattern constitutes the seed if the system (starts it off). The first generation is created by applying the above rules simultaneously to every cell in the seed; births and deaths occur simultaneously, and the discrete moment at which this happens is called a "tick". Each generation is a pure function of the preceding one, and the rules continue to be applied repeatedly to create further generations. | |
//Samuel Sandoval Section 17 | |
#include <chrono> | |
#include <ctime> | |
#include <curses.h> | |
#include <iostream> | |
#include <stdlib.h> | |
#include <thread> | |
using namespace std; | |
const int ROWS = 48; | |
const int COLUMNS = 160; | |
const char ALIVE = '*'; | |
const char DEAD = ' '; | |
void InitializeCurses() { | |
initscr(); | |
cbreak(); | |
noecho(); | |
clear(); | |
} | |
void PrintExitInstructions() { | |
mvaddstr(ROWS + 1, 0, "Press ctrl+c to quit "); | |
refresh(); | |
} | |
void PrintRow(string row_to_print, int row) { | |
mvaddstr(row, 0, row_to_print.c_str()); | |
refresh(); | |
this_thread::sleep_for(chrono::milliseconds(1)); | |
} | |
void set_world(int current_gen[ROWS][COLUMNS]){ | |
for(int row = 0; row < ROWS; row++){ | |
for(int col = 0; col < COLUMNS; col++){ | |
current_gen[row][col] = 0; | |
} | |
} | |
} | |
void init_glider(int current_gen[ROWS][COLUMNS]) { | |
current_gen[23][80] = 1; | |
current_gen[24][81] = 1; | |
for(int col = 79; col < 82; col++) { | |
current_gen[25][col] = 1; | |
} | |
} | |
void build_world(int current_gen[ROWS][COLUMNS]) { | |
for(int row = 0; row < ROWS; row++) { | |
string row_string; | |
for(int col = 0; col < COLUMNS; col++) { | |
if(current_gen[row][col] == 0) { | |
row_string += DEAD; | |
} | |
else { | |
row_string += ALIVE; | |
} | |
} | |
PrintRow(row_string, row); | |
} | |
} | |
int col_neigh(int board[ROWS][COLUMNS], int row, int column) { | |
int neighbor_total = 0; | |
if(board[row][(column+1) % 160] == 1) { | |
neighbor_total += 1; | |
} | |
if(board[row][((column-1) + 160) % 160] == 1) { | |
neighbor_total += 1; | |
} | |
return neighbor_total; | |
} | |
int row_neigh(int board[ROWS][COLUMNS], int row, int column){ | |
int neighbor_total = 0; | |
if(board[(row+1) % 48][column] == 1) { | |
neighbor_total += 1; | |
} | |
if(board[((row-1) + 48) % 48][column] == 1) { | |
neighbor_total += 1; | |
} | |
return neighbor_total; | |
} | |
int diag_neigh(int board[ROWS][COLUMNS], int row, int column){ | |
int neighbor_total = 0; | |
if(board[((row-1) + 48) % 48][(column+1) % 160] == 1) { | |
neighbor_total += 1; | |
} | |
if(board[((row-1) + 48) % 48][((column-1) + 160) % 160] == 1) { | |
neighbor_total += 1; | |
} | |
if(board[(row+1) % 48][(column+1) % 160] == 1) { | |
neighbor_total += 1; | |
} | |
if(board[(row+1) % 48][((column-1) + 160) % 160] == 1) { | |
neighbor_total += 1; | |
} | |
return neighbor_total; | |
} | |
void Determine_Cell_Status(int board[ROWS][COLUMNS], int neighbor_total, int row, int column) { | |
if(neighbor_total < 2) { | |
board[row][column] = 0; | |
} | |
else if(neighbor_total > 3) { | |
board[row][column] = 0; | |
} | |
else if(neighbor_total == 3) { | |
board[row][column] = 1; | |
} | |
} | |
void check_neighbors(int read_board[ROWS][COLUMNS], int write_board[ROWS][COLUMNS]) { | |
for(int row = 0; row < ROWS; row++) { | |
for(int col = 0; col < COLUMNS; col++) { | |
int neighbor_total = 0; | |
neighbor_total += col_neigh(read_board, row, col); | |
neighbor_total += row_neigh(read_board, row, col); | |
neighbor_total += diag_neigh(read_board, row, col); | |
Determine_Cell_Status(write_board, neighbor_total, row, col); | |
} | |
} | |
} | |
void copy_world(int read_board[ROWS][COLUMNS], int write_board[ROWS][COLUMNS]) { | |
for(int row = 0; row < ROWS; row++) { | |
for(int col = 0; col < COLUMNS; col++) { | |
write_board[row][col] = read_board[row][col]; | |
} | |
} | |
} | |
int main(int argc, char* argv[]) { | |
InitializeCurses(); | |
PrintExitInstructions(); | |
int current_gen[ROWS][COLUMNS]; | |
int next_gen[ROWS][COLUMNS]; | |
set_world(current_gen); | |
init_glider(current_gen); | |
// TODO: Implement Conway's Game of Life. | |
while (true) { | |
build_world(current_gen); | |
copy_world(current_gen, next_gen); | |
check_neighbors(current_gen, next_gen); | |
copy_world(next_gen, current_gen); | |
} | |
endwin(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment