Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define LIVING '#'
- #define DEAD '_'
- #define TEMPDEAD 'R'
- #define TEMPLIVING 'T'
- #define BOUND '|'
- #define SIZE 22
- int mod (int a, int b)
- {
- if(b < 0)
- return mod(-a, -b);
- int result = a % b;
- if(result < 0)
- result+=b;
- return result;
- }
- char *setCell(char grid [], int index, char state)
- {
- grid[mod(index,SIZE)]=state;
- }
- char getCell(char grid [], int index)
- {
- return grid[mod(index,SIZE)];
- }
- char *initializeGrid(char grid [])
- {
- for(int i = 0; i < SIZE; i++)
- {
- grid[i] = DEAD;
- }
- setCell(grid,2,LIVING);
- setCell(grid,3,LIVING);
- setCell(grid,4,LIVING);
- setCell(grid,6,LIVING);
- setCell(grid,7,LIVING);
- setCell(grid,9,LIVING);
- setCell(grid,11,LIVING);
- setCell(grid,13,LIVING);
- setCell(grid,15,LIVING);
- setCell(grid,18,LIVING);
- setCell(grid,0,BOUND);
- setCell(grid,SIZE-1,BOUND);
- }
- void endProgram()
- {
- cout << "Press enter to continue..." << endl;
- string input = "";
- getline(cin,input);//keeps console open
- }
- bool checkifgameover(char grid [], char lastgrid [], int gen)
- {
- if(gen>1)
- {
- for(int i = 0; i < SIZE; i++)
- {
- if(grid[i]!=lastgrid[i])
- {
- return false;
- }
- }
- return true;
- }
- }
- char *updateGrid(char grid [])
- {
- for(int i = 0; i < SIZE; i++)
- {
- if(getCell(grid,i)==LIVING)
- {
- if(getCell(grid,i+1) !=LIVING & getCell(grid,i+1) !=BOUND)
- {
- if(getCell(grid,i+2) ==LIVING)
- {
- setCell(grid,i+1,TEMPLIVING);//WHERE DOES THE POLLEN GO?
- }
- }
- if((getCell(grid,i-1)!=LIVING & getCell(grid,i-1)!=TEMPDEAD ) & (getCell(grid,i+1)!=LIVING & getCell(grid,i-1)!=TEMPDEAD))
- {
- setCell(grid,i,TEMPDEAD);//DEATH BY LONELINESS
- }
- if((getCell(grid,i-1)==LIVING | getCell(grid,i-1)==TEMPDEAD) & (getCell(grid,i+1)==LIVING | getCell(grid,i+1)==TEMPDEAD))
- {
- setCell(grid,i,TEMPDEAD);//DEATH BY OVERPOPULATION
- }
- }
- }
- for(int i = 0; i < SIZE; i++)
- {
- if(getCell(grid,i)==TEMPLIVING)//So no logic is acted on Living cells until after next generation
- {
- setCell(grid,i,LIVING);
- }
- if(getCell(grid,i)==TEMPDEAD)
- {
- setCell(grid,i,DEAD);
- }
- }
- }
- int main()
- {
- char output [SIZE];
- char lastoutput [SIZE];
- initializeGrid(output);
- int generation = 0;
- while(!checkifgameover(output,lastoutput,generation))
- {
- for(int i = 0; i < SIZE; i++)
- {
- lastoutput[i] = output[i];
- }
- generation++;
- cout<< "Current Gen:" << generation;
- for(int i = 0; i < SIZE; i++)
- {
- cout << output[i];
- }
- cout << endl;
- updateGrid(output);
- }
- endProgram();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement