Advertisement
Guest User

Untitled

a guest
Jan 8th, 2015
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include<fstream>
  2. #include<iostream>
  3. #include<unistd.h>
  4.  
  5. #include"twodboolarray.hh"
  6.  
  7. // Basisklassen
  8. class Regel
  9. {
  10.   public:
  11.   typedef TwoDBoolArray Daten;
  12.   virtual void anwenden(Daten& daten) = 0;
  13. };
  14.  
  15. class BoundaryCondition
  16. {
  17.   public:
  18.   typedef TwoDBoolArray Daten;
  19.   virtual bool boundary(Daten& daten, int i, int j) = 0;
  20. };
  21.  
  22. class TorusCondition : public BoundaryCondition
  23. {
  24.   // Implementieren Sie hier eine Randbedingung, welche das
  25.   // Gebiet als doppelt periodisch annimmt.
  26. };
  27.  
  28. class AliveCondition : public BoundaryCondition
  29. {
  30.   // Implementieren Sie hier eine Randbedingung welche annimmt,
  31.   // dass rund um das Rechengebiet lebende Zellen sitzen.
  32. };
  33.  
  34. class DeadCondition : public BoundaryCondition
  35. {
  36.   // Implementieren Sie hier eine Randbedingung welche annimmt,
  37.   // dass rund um das Rechengebiet tote Zellen sitzen.
  38. };
  39.  
  40. class GameOfLifeRules : public Regel
  41. {
  42.   public:
  43.   typedef TwoDBoolArray Daten;
  44.  
  45.   GameOfLifeRules(BoundaryCondition& _bc) : bc(_bc) {}
  46.  
  47.   // Implementieren Sie hier die Regeln des Game of Life
  48.  
  49.   private:
  50.   BoundaryCondition& bc;
  51. };
  52.  
  53. // Ein zellulärer Automat, der Regeln und Datenstrukturen von außen bekommt
  54. class Automat
  55. {
  56.   public:
  57.   typedef TwoDBoolArray Daten;
  58.   Automat(Daten& daten, Regel& regel) : _daten(daten), _regel(regel)
  59.   {}
  60.  
  61.   // mache n Schritte
  62.   void step(int n=1)
  63.   {
  64.     for (int i=0; i<n; ++i)
  65.     {
  66.       // Linux-spezifische Art und Weise den Inhalt der Konsole zu löschen
  67.       // und den Cursor nach oben links zu setzen.
  68.       std::cout << "\x1B[2J\x1B[H" << "Step " << i << std::endl << _daten;
  69.       // Das Wiedergeben der Lösung soll immer 10 Sekunden (=1e7 Mikrosekunden)
  70.       // dauern. Sie können diesen Wert auch ändern.
  71.       usleep(1.e7/n);
  72.       _regel.anwenden(_daten);
  73.     }
  74.   }
  75.  
  76.   private:
  77.   Daten& _daten;
  78.   Regel& _regel;
  79. };
  80.  
  81. int main(int argc, char** argv)
  82. {
  83.   if (argc != 2)
  84.   {
  85.     std::cout << "Usage: ./<progname> <txt-file>" << std::endl;
  86.     return 1;
  87.   }
  88.  
  89.   // Initialisiere die Datenstruktur
  90.   TwoDBoolArray a;
  91.   std::ifstream file;
  92.   file.open(argv[1]);
  93.   if (file.good())
  94.     file >> a;
  95.   else
  96.   {
  97.     std::cout << "Cannot read file" << std::endl;
  98.     return 1;
  99.   }
  100.  
  101.   // Wähle Randbedingung
  102.   TorusCondition bc;
  103.  
  104.   // Wähle ein Regelsystem
  105.   GameOfLifeRules rules(bc);
  106.  
  107.   // Initialisiere den zellulären Automaten
  108.   Automat automat(a,rules);
  109.  
  110.   // Experimentieren Sie hier mit Ihrem Automaten.
  111.  
  112.  
  113.   return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement