Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

139 lignes
3.4KB

  1. #include <string>
  2. struct BoundaryCondition
  3. {
  4. virtual bool boundary(Board& board, int i, int j) = 0;
  5. };
  6. class Board {
  7. public :
  8. // Konstruktor, erzeuge bool* _fields
  9. Board(int width, int height, BoundaryCondition cond);
  10. // Desktruktor, raeume bool* _fields auf
  11. ~Board();
  12. // copy constructor, intializes by copying from 'other'
  13. Board(Board& other);
  14. // assignment operator
  15. Board& operator=(Board& other);
  16. // gibt die Breite des Bretts zurueck
  17. double width();
  18. // gibt die Hoehe des Bretts zurueck
  19. double height();
  20. // schreibe value an den Pixel (i,j)
  21. // Ueberlegen Sie wie aus (i,j) den flachen Index bei row-major bekommen
  22. void updateField(int i, int j, bool value);
  23. // Zugang zum Pixel (i,j) im 1D Array
  24. // Ueberlegen Sie wie aus (i,j) den flachen Index bei row-major bekommen
  25. int operator()(int i, int j);
  26. // Gibt zurueck ob Feld am Rand liegt
  27. bool touchesBoundary(int i, int j);
  28. // Gibt Zahl der lebenden Zellen zurueck, die das Feld umgeben
  29. int livingCells(int i, int j);
  30. // Prueft ob Zelle lebt oder nicht
  31. bool isLiving(int i, int j);
  32. // Gibt die BoundaryCondition zurueck
  33. BoundaryCondition boundaryCondition();
  34. private :
  35. int _width;
  36. int _height;
  37. bool* _fields;
  38. void copy(Board& other);
  39. BoundaryCondition _boundCond;
  40. };
  41. Board::Board(int width, int height, BoundaryCondition cond) {
  42. // initialize all values
  43. _width = width;
  44. _height = height;
  45. _boundCond = cond;
  46. _fields = new bool[width * height];
  47. }
  48. Board::~Board() {
  49. _fields = 0;
  50. }
  51. void Board::copy(Board& other) {
  52. _width = other.width();
  53. _height = other.height();
  54. _boundCond = other.boundaryCondition();
  55. _fields = new bool[_width * _height];
  56. for (int i = 0; i < _height; i++) {
  57. for (int j = 0; j < _width; j++) {
  58. updateField(i, j, other.isLiving(i, j));
  59. }
  60. }
  61. }
  62. Board::Board(Board& other) {
  63. copy(other);
  64. }
  65. Board& Board::operator=(Board& other) {
  66. if (this == &other) {
  67. return *this;
  68. }
  69. copy(other);
  70. return *this;
  71. }
  72. double Board::width() {
  73. return _width;
  74. }
  75. double Board::height() {
  76. return _height;
  77. }
  78. BoundaryCondition Board::BoundaryCondition() {
  79. return _boundCond;
  80. }
  81. void Board::updateField(int i, int j, bool value) {
  82. // i = Zeile und wegen row-major Konvention muss i mit der Breite
  83. // multipliziert werden
  84. _fields[i * _width + j] = value;
  85. }
  86. bool Board::touchesBoundary(int i, int j) {
  87. return i == _height - 1 || i == -1 || j == _width - 1 || j == -1;
  88. }
  89. int Board::livingCells(int i, int j) {
  90. bool surround[8] = { isLiving(i+1, j+1), isLiving(i, j+1), isLiving(i-1, j+1)
  91. , isLiving(i+1, j), isLiving(i-1, j)
  92. , isLiving(i+1, j-1), isLiving(i, j-1), isLiving(i-1, j-1)};
  93. int numLiving = 0;
  94. for (int i = 0; i < 8; i++) {
  95. if (surround[i] == true) {
  96. numLiving += 1;
  97. }
  98. }
  99. }
  100. bool Board::isLiving(int i, int j) {
  101. if (touchesBoundary(i, j)) {
  102. return _boundCond.boundary(i, j);
  103. } else {
  104. return _fields[i * _width + j];
  105. }
  106. }
  107. int Board::operator()(int i, int j) {
  108. // i = Zeile und wegen row-major Konvention muss i mit der Breite
  109. // Pixel multipliziert werden
  110. return _fields[i * _width+ j];
  111. }