From 5da5776d5c768ba2b6ff493b4fb35a16e5aa2364 Mon Sep 17 00:00:00 2001 From: kai Date: Sun, 15 Jun 2025 15:05:16 +0200 Subject: [PATCH] succesfully migrated super_matrix --- lab11/game_of_life/super_grid.h | 13 +++++++++++-- lab11/game_of_life/test.cpp | 24 ++++++++++++++---------- lab11/game_of_life/utils.cpp | 9 +++++++++ lab11/game_of_life/utils.h | 1 + 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/lab11/game_of_life/super_grid.h b/lab11/game_of_life/super_grid.h index 5bb3e28..97135f4 100644 --- a/lab11/game_of_life/super_grid.h +++ b/lab11/game_of_life/super_grid.h @@ -21,10 +21,19 @@ private: Matrix grid_; }; -inline SuperGrid::SuperGrid(const Matrix &other) : grid_(other) {} +inline SuperGrid::SuperGrid(const Matrix &other) + : grid_( + Matrix::zeros(other.rows() + 2, other.cols() + 2)) // initialize grid_ +{ + for (int i = 0; i < other.rows(); i++) { + for (int j = 0; j < other.cols(); j++) { + grid_(i + 1, j + 1) = other(i, j); // copy into grid_ directly + } + } +} inline SuperGrid SuperGrid::zeros(int rows, int cols) { - return SuperGrid(Matrix::zeros(rows + 2, cols + 2)); + return SuperGrid(Matrix::zeros(rows, cols)); } inline double &SuperGrid::operator()(int i, int j) { diff --git a/lab11/game_of_life/test.cpp b/lab11/game_of_life/test.cpp index 0cebaea..f2bea97 100644 --- a/lab11/game_of_life/test.cpp +++ b/lab11/game_of_life/test.cpp @@ -2,35 +2,33 @@ #include "common.h" #include "game_of_life.h" #include "matrix.h" +#include "super_grid.h" #include "utils.h" -Matrix init_step(Matrix start) { +Matrix init_step(SuperGrid start) { GameOfLife gol = GameOfLife(start, MPIGridSize()); gol.step(); return gol.getGrid(); } TEST(test_underpopulation) { - Matrix start = Matrix::zeros(10); + SuperGrid start = SuperGrid::zeros(10, 10); start(0, 0) = 1; Matrix end = init_step(start); check(end(0, 0), 0); } TEST(test_survive_2) { - Matrix start = Matrix::zeros(10); + SuperGrid start = SuperGrid::zeros(10, 10); start(0, 0) = 1; start(0, 1) = 1; start(1, 0) = 1; - - print(start); Matrix end = init_step(start); - print(end); check(end(0, 0), 1); } TEST(test_survive_3) { - Matrix start = Matrix::zeros(10); + SuperGrid start = SuperGrid::zeros(10, 10); start(0, 0) = 1; start(1, 1) = 1; start(0, 1) = 1; @@ -40,7 +38,7 @@ TEST(test_survive_3) { } TEST(test_overpopulation) { - Matrix start = Matrix::zeros(10); + SuperGrid start = SuperGrid::zeros(10, 10); start(1, 1) = 1; start(0, 1) = 1; start(1, 0) = 1; @@ -51,7 +49,7 @@ TEST(test_overpopulation) { } TEST(test_reproduction) { - Matrix start = Matrix::zeros(10); + SuperGrid start = SuperGrid::zeros(10, 10); start(0, 1) = 1; start(1, 0) = 1; start(2, 1) = 1; @@ -60,7 +58,7 @@ TEST(test_reproduction) { } TEST(test_survive_edge) { - Matrix start = Matrix::zeros(10); + SuperGrid start = SuperGrid::zeros(10, 10); start(0, 0) = 1; start(9, 0) = 1; start(0, 9) = 1; @@ -68,4 +66,10 @@ TEST(test_survive_edge) { check(end(0, 0), 1); } +TEST(test_super_grid) { + SuperGrid su_grid = SuperGrid::zeros(10, 10); + check(su_grid.rows(), 10); + check(su_grid.cols(), 10); +} + int main() { return 0; } diff --git a/lab11/game_of_life/utils.cpp b/lab11/game_of_life/utils.cpp index c6f711b..0ac4122 100644 --- a/lab11/game_of_life/utils.cpp +++ b/lab11/game_of_life/utils.cpp @@ -90,3 +90,12 @@ void print(Matrix &grid) { std::cout << std::endl; } } + +void print(const Matrix &grid) { + for (int i = 0; i < grid.rows(); ++i) { + for (int j = 0; j < grid.cols(); ++j) { + std::cout << ((grid(i, j) == 1) ? "X " : ". "); + } + std::cout << std::endl; + } +} diff --git a/lab11/game_of_life/utils.h b/lab11/game_of_life/utils.h index 776c0c8..d1cf14f 100644 --- a/lab11/game_of_life/utils.h +++ b/lab11/game_of_life/utils.h @@ -32,5 +32,6 @@ void storeAnimation(const std::string &foldername, const Matrix &initstate, void print(const GameOfLife &game); void print(Matrix &matrix); +void print(const Matrix &matrix); #endif // UTILS_H