Guest User

CA

a guest
Nov 6th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <vector>
  4. #include <sstream>
  5. #include <fstream>
  6. #include <string>
  7. #include <stdlib.h>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. class Automata
  13. {
  14.  
  15. public:
  16. void Menu();
  17. void print();
  18. void generation();
  19. void saveToTxt();
  20.  
  21. int height;
  22. int width;
  23. int rules[8];
  24. vector<int> cell;
  25. vector<int> oldCell;
  26. int rule[8];
  27. int execRules(int x, int y, int z);
  28. void setRules(int dRule);
  29. void nLine(int a,int b,int c,int i);
  30. };
  31.  
  32. void Automata::generation()
  33. {
  34.     ofstream outfile("CA.txt");
  35.     char save;
  36.     char choice;
  37.     int rule;
  38.     int r;
  39.  
  40.     cout << "Choose the width for your automaton: "<<endl;
  41.     cin >> width;
  42.     cout << "Choose the height for your automaton: "<<endl;
  43.     cin >> height;
  44.     cout << "Do you wish to randomly select a rule? (Yes/No): "<<endl;
  45.     cin >> choice;
  46.    
  47.         if(choice == 'y')
  48.     {
  49.         r = rand() % 256;
  50.     }
  51.         else if(choice == 'n')
  52.     {
  53.    
  54.     //the random automata funtion does not work correctly i struglled to fix it
  55.         cout << "Enter the rule for your automaton: ";
  56.         cin >> rule;
  57.     }
  58.         else
  59.     {
  60.         cout << "Input not recognised!";
  61.         generation();
  62.     }
  63.    
  64.     cout << "The automaton would be saved to a .txt file. (Yes/No)"<<endl;
  65.     cin >> save;
  66.    
  67.     if(save != 'y' && save != 'n')
  68.     {
  69.         cout << "Input not recognised!";
  70.         generation();
  71.     }
  72.  
  73.     cout << " Automata generated with the rule: " << rule << "" << endl;
  74.     setRules(rule);
  75.     oldCell.resize(width+1);
  76.     cell.resize(width+1);
  77.     int previous = 0, middle = 0, next = 0;
  78.    
  79.     for (int i = 0; i <= width; i++)
  80.     {
  81.         cell[i] = 0;
  82.     }
  83.    
  84.     if (width%2 == 0)
  85.     {
  86.         cell[width/2] = 1;
  87.     }
  88.         else
  89.         cell[(width+1)/2] = 1;
  90.         print();
  91.  
  92.         if ( save == 'y' )
  93.         {
  94.             for(int k = 0; k < cell.size(); k++)
  95.             {
  96.                 if(cell.at(k) == 1)outfile << "*";
  97.                 else outfile<<" ";
  98.             }
  99.         }
  100.     outfile<<"\n";
  101.     for(int j=0; j<height; j++)
  102.     {
  103.             cell.swap(oldCell);
  104.             for (int i=0; i<=oldCell.size()-1; i++)
  105.             {
  106.                 if(i!=0)previous = oldCell.at(i-1);
  107.                 else previous = oldCell.at(oldCell.size()-1);
  108.                 middle = oldCell.at(i);
  109.                 if(i != oldCell.size()-1)next = oldCell.at(i+1);
  110.                 else next = oldCell.at(0);
  111.                 nLine(previous,middle,next,i);
  112.             }
  113.  
  114.             if(save == 'y')
  115.         {
  116.             for(int k = 0; k < cell.size(); k++)
  117.             {
  118.                 if(cell.at(k) == 1)outfile << "*";
  119.                 else outfile<<" ";
  120.             }
  121.             outfile<<"\n";
  122.             print();
  123.         }
  124.         else
  125.         print();
  126.     }
  127. }
  128.  
  129. void Automata::setRules(int dRule)
  130. {
  131.     int set[8] = {128,64,32,16,8,4,2,1};
  132.     for(int i = 0; i < 8; i++)
  133.     {
  134.         if(set[i] <= dRule)
  135.         {
  136.             rules[i] = 1;
  137.             dRule = dRule - set[i];
  138.  
  139.         }
  140.         else rules[i] = 0;
  141.     }
  142. }
  143.  
  144. int Automata::execRules(int x, int y, int z)
  145. {
  146.     if      (x == 1 && y == 1 && z == 1) return rules[0];
  147.     else if (x == 1 && y == 1 && z == 0) return rules[1];
  148.     else if (x == 1 && y == 0 && z == 1) return rules[2];
  149.     else if (x == 1 && y == 0 && z == 0) return rules[3];
  150.     else if (x == 0 && y == 1 && z == 1) return rules[4];
  151.     else if (x == 0 && y == 1 && z == 0) return rules[5];
  152.     else if (x == 0 && y == 0 && z == 1) return rules[6];
  153.     else return rules[7];
  154. }
  155.  
  156. void Automata::nLine(int a, int b, int c, int i)
  157. {
  158.     cell[i] = execRules(a,b,c);
  159. }
  160.  
  161. void Automata::print()
  162. {
  163.     for(int i=0; i<cell.size(); i++)
  164.     {
  165.         if(cell.at(i) == 1) cout << "*";
  166.         else cout << " ";
  167.     }
  168.     cout << "\n";
  169. }
  170.  
  171. void Automata::Menu()
  172. {
  173.     string option;
  174.  
  175.     cout << "     "<<endl;
  176.     cout << "Press S to start automaton" <<endl;
  177.     cout << "Press C to clear the screen"<<endl;
  178.     cout << "Press Q to quit program"<<endl;
  179.     //cin >> option;
  180.  
  181.     do
  182.     {
  183.         getline( cin, option);
  184.         switch (option[0])
  185.         {
  186.             case 'S':
  187.             case 's':
  188.                 generation();
  189.                 Menu();
  190.                 break;
  191.                
  192.             case 'C':
  193.             case 'c':
  194.                 cout << string(50, '\n');
  195.                 Menu();
  196.                 break;
  197.                
  198.             case 'Q':
  199.             case 'q':
  200.                 cout << " Goodbye "<<endl;
  201.                 break;
  202.                
  203.                 default:
  204.                 break;
  205.         }
  206.        
  207.     }while (option[0] != 'q');
  208. }
  209.  
  210. int main()
  211. {
  212.     Automata autom;
  213.     autom.Menu();
  214.     return 0;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment