Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

126 wiersze
3.5KB

  1. #include <iostream>
  2. #include <unistd.h> // needed for usleep
  3. #include <string>
  4. #include <fstream>
  5. #include "board.hh"
  6. // Basisklassen
  7. struct Regel
  8. {
  9. virtual void anwenden(Board& board) = 0;
  10. };
  11. class TorusCondition : public BoundaryCondition
  12. {
  13. bool boundary(Board& board, int i, int j) {
  14. if (i == board.height() - 1) {
  15. return board.isLiving(0, j);
  16. } else if (i == -1) {
  17. return board.isLiving(board.height() - 1, j);
  18. } else if (j == board.width() - 1) {
  19. return board.isLiving(i, 0);
  20. } else if (j == -1) {
  21. return board.isLiving(i, board.width() - 1);
  22. }
  23. }
  24. };
  25. class AliveCondition : public BoundaryCondition
  26. {
  27. bool boundary(Board& board, int i, int j) {
  28. return true;
  29. }
  30. };
  31. class DeadCondition : public BoundaryCondition
  32. {
  33. bool boundary(Board& board, int i, int j) {
  34. return false;
  35. }
  36. };
  37. class GameOfLifeRules : public Regel
  38. {
  39. public :
  40. //void anwenden(Board& board) {
  41. // // copy new board from old board
  42. // Board updated = Board(board.width(), board.height());
  43. // for (int i = 0; i < board.height(); i++) {
  44. // for (int j = 0; i < board.width(); i++) {
  45. // int n = board.livingCells(i, j);
  46. // int self = board.isLiving(i, j);
  47. // if (self && n < 2) {
  48. // updated.updateField(i, j, false);
  49. // } else if (!self && n == 3) {
  50. // updated.updateField(i, j, true);
  51. // } else if (self && n > 3) {
  52. // updated.updateField(i, j, false);
  53. // } else {
  54. // updated.updateField(i, j, self);
  55. // }
  56. // }
  57. // }
  58. // board = updated;
  59. //}
  60. };
  61. // Ein zellulaerer Automat, der Regeln und Datenstrukturen von aussen bekommt
  62. class Automat
  63. {
  64. public :
  65. // der Datentyp fuer das 2D Bool Array
  66. Automat(Board& board, Regel& regel)
  67. : _board(board)
  68. , _regel(regel)
  69. {}
  70. // mache n Schritte
  71. void doSteps(int n=1)
  72. {
  73. for (int i=0; i<n; ++i)
  74. {
  75. // Linux-spezifische Art und Weise den Inhalt der Konsole zu loeschen
  76. // und den Cursor nach oben links zu setzen.
  77. //std::cout << "\x1B[2J\x1B[H" << "Step " << i << std::endl << _board;
  78. // Das Wiedergeben der Loesung soll immer 10 Sekunden (=1e7 Mikrosekunden)
  79. // dauern. Sie koennen diesen Wert auch aendern.
  80. usleep(1.e7/n);
  81. //_regel.anwenden(_board);
  82. }
  83. }
  84. private :
  85. Board& _board;
  86. Regel& _regel;
  87. };
  88. int main(int argc, char** argv)
  89. {
  90. if (argc != 2)
  91. {
  92. std::cout << "Usage: ./<progname> <txt-file>" << std::endl;
  93. return 1;
  94. }
  95. Board board = Board(2, 2, AliveCondition);
  96. board.updateField(1,1, true);
  97. board.livingCells(1,1);
  98. // Initialisiere Datenstruktur des 2D Bool Array
  99. // TODO
  100. // Ueberlegen Sie sich wie Sie einen Startzustand in das 2D Bool Array bekommen
  101. // Lesen Sie ggf.
  102. // (1) eine solche Textdatei mittels Filestream ein
  103. // (2) ueberladen Sie Operatoren operator<< oder operator>>
  104. // TODO
  105. // - legen Sie eine Instanz der Randbedingung an
  106. // - legen Sie eine Instanz des Regelsystems an
  107. // - Initialisieren Sie den zellulaeren Automaten
  108. // Experimentieren Sie hier mit Ihrem Automaten
  109. return 0;
  110. }