Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CSCI 1410 Programming assignment 5
- // Written by Hanyuan Li <hanyuan.li@ucdenver.edu>
- // Elementary cellular automata
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- void setup_row();
- void print_row();
- void update_row();
- int find_next_cell_state(int left_neighbor, int cell, int right_neighbor);
- int z;
- const int WIDTH=80;
- int row[WIDTH];
- int main()
- {
- setup_row();
- print_row();
- for(int i = 0; i < 42; i++)
- {
- // update_row();
- print_row();
- }
- }
- // The setup_row function creates an initial state for the cellular autonoma
- void setup_row()
- {
- for(int z = 0; z < 80; z++)
- {
- row[z] = 0;
- row[39] = 1;
- cout << row[z];
- }
- cout << endl;
- }
- // The print_row function outputs the state of the row to the user. For all
- // cells that are 0, it outputs a space (" "), for all cells that are 1, it
- // outputs a star ("*")
- void print_row()
- {
- if(row[z] == 0)
- {
- cout << " ";
- }
- else
- {
- cout << "*";
- }
- }
- // The update_row function updates the contents of the row after calculating
- // the next states using the find_next_cell_state function
- void update_row(int left_neighbor, int cell, int right_neighbor)
- {
- for(int x = 0; x < 80; x++)
- {
- left_neighbor = row[x];
- cell = row[x+1];
- right_neighbor = row[x+2];
- row[x] = find_next_cell_state(left_neighbor, cell, right_neighbor);
- }
- }
- // The find_next_cell_state function implements the Rule 30 celular autonoma rules.
- // The states for this are:
- //
- // 111 110 101 100 011 010 001 000
- // 0 0 0 1 1 1 1 0
- int find_next_cell_state(int left_neighbor, int cell, int right_neighbor)
- {
- if (0 == cell)
- {
- if (1 == (left_neighbor + right_neighbor))
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- if (1 == left_neighbor)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- }
Add Comment
Please, Sign In to add comment