Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <vector>
- #include <sstream>
- #include <fstream>
- #include <string>
- #include <stdlib.h>
- using namespace std;
- class Automata
- {
- public:
- void Menu();
- void print();
- void generation();
- void saveToTxt();
- int height;
- int width;
- int rules[8];
- vector<int> cell;
- vector<int> oldCell;
- int rule[8];
- int execRules(int x, int y, int z);
- void setRules(int dRule);
- void nLine(int a,int b,int c,int i);
- };
- void Automata::generation()
- {
- ofstream outfile("CA.txt");
- char save;
- char choice;
- int rule;
- int r;
- cout << "Choose the width for your automaton: "<<endl;
- cin >> width;
- cout << "Choose the height for your automaton: "<<endl;
- cin >> height;
- cout << "Do you wish to randomly select a rule? (Yes/No): "<<endl;
- cin >> choice;
- if(choice == 'y')
- {
- r = rand() % 256;
- }
- else if(choice == 'n')
- {
- //the random automata funtion does not work correctly i struglled to fix it
- cout << "Enter the rule for your automaton: ";
- cin >> rule;
- }
- else
- {
- cout << "Input not recognised!";
- generation();
- }
- cout << "The automaton would be saved to a .txt file. (Yes/No)"<<endl;
- cin >> save;
- if(save != 'y' && save != 'n')
- {
- cout << "Input not recognised!";
- generation();
- }
- cout << " Automata generated with the rule: " << rule << "" << endl;
- setRules(rule);
- oldCell.resize(width+1);
- cell.resize(width+1);
- int previous = 0, middle = 0, next = 0;
- for (int i = 0; i <= width; i++)
- {
- cell[i] = 0;
- }
- if (width%2 == 0)
- {
- cell[width/2] = 1;
- }
- else
- cell[(width+1)/2] = 1;
- print();
- if ( save == 'y' )
- {
- for(int k = 0; k < cell.size(); k++)
- {
- if(cell.at(k) == 1)outfile << "*";
- else outfile<<" ";
- }
- }
- outfile<<"\n";
- for(int j=0; j<height; j++)
- {
- cell.swap(oldCell);
- for (int i=0; i<=oldCell.size()-1; i++)
- {
- if(i!=0)previous = oldCell.at(i-1);
- else previous = oldCell.at(oldCell.size()-1);
- middle = oldCell.at(i);
- if(i != oldCell.size()-1)next = oldCell.at(i+1);
- else next = oldCell.at(0);
- nLine(previous,middle,next,i);
- }
- if(save == 'y')
- {
- for(int k = 0; k < cell.size(); k++)
- {
- if(cell.at(k) == 1)outfile << "*";
- else outfile<<" ";
- }
- outfile<<"\n";
- print();
- }
- else
- print();
- }
- }
- void Automata::setRules(int dRule)
- {
- int set[8] = {128,64,32,16,8,4,2,1};
- for(int i = 0; i < 8; i++)
- {
- if(set[i] <= dRule)
- {
- rules[i] = 1;
- dRule = dRule - set[i];
- }
- else rules[i] = 0;
- }
- }
- int Automata::execRules(int x, int y, int z)
- {
- if (x == 1 && y == 1 && z == 1) return rules[0];
- else if (x == 1 && y == 1 && z == 0) return rules[1];
- else if (x == 1 && y == 0 && z == 1) return rules[2];
- else if (x == 1 && y == 0 && z == 0) return rules[3];
- else if (x == 0 && y == 1 && z == 1) return rules[4];
- else if (x == 0 && y == 1 && z == 0) return rules[5];
- else if (x == 0 && y == 0 && z == 1) return rules[6];
- else return rules[7];
- }
- void Automata::nLine(int a, int b, int c, int i)
- {
- cell[i] = execRules(a,b,c);
- }
- void Automata::print()
- {
- for(int i=0; i<cell.size(); i++)
- {
- if(cell.at(i) == 1) cout << "*";
- else cout << " ";
- }
- cout << "\n";
- }
- void Automata::Menu()
- {
- string option;
- cout << " "<<endl;
- cout << "Press S to start automaton" <<endl;
- cout << "Press C to clear the screen"<<endl;
- cout << "Press Q to quit program"<<endl;
- //cin >> option;
- do
- {
- getline( cin, option);
- switch (option[0])
- {
- case 'S':
- case 's':
- generation();
- Menu();
- break;
- case 'C':
- case 'c':
- cout << string(50, '\n');
- Menu();
- break;
- case 'Q':
- case 'q':
- cout << " Goodbye "<<endl;
- break;
- default:
- break;
- }
- }while (option[0] != 'q');
- }
- int main()
- {
- Automata autom;
- autom.Menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment