diff options
| author | 2026-11-08 02:11:35 +0000 | |
|---|---|---|
| committer | 2026-11-08 02:11:35 +0000 | |
| commit | 08fcd58893b5ea754ebddbf63b2745dde34d4830 (patch) | |
| tree | 6e121100ec1aceccd2a6032c90bf11d1dd16ef8f /src/cellular-automaton.c | |
| download | lab-01-master.tar.gz | |
Diffstat (limited to 'src/cellular-automaton.c')
| -rw-r--r-- | src/cellular-automaton.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/cellular-automaton.c b/src/cellular-automaton.c new file mode 100644 index 0000000..183684a --- /dev/null +++ b/src/cellular-automaton.c | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include "cellular-automaton.h" | ||
| 3 | #include "utils.h" | ||
| 4 | |||
| 5 | void | ||
| 6 | cellular_automaton_init (struct cellular_automaton *cellular_automaton, | ||
| 7 | uint32_t function, uint32_t rows, uint32_t cols) | ||
| 8 | { | ||
| 9 | board_init (&cellular_automaton->board, rows, cols); | ||
| 10 | cellular_automaton->back_board_bitarray = | ||
| 11 | malloc (cellular_automaton->board.bitarray.size); | ||
| 12 | cellular_automaton->function = function; | ||
| 13 | } | ||
| 14 | |||
| 15 | void | ||
| 16 | cellular_automaton_progress (struct cellular_automaton *cellular_automaton) | ||
| 17 | { | ||
| 18 | // uint32_t alive = 0; | ||
| 19 | for (int32_t row = 0; row < cellular_automaton->board.rows; ++row) { | ||
| 20 | for (int32_t col = 0; col < cellular_automaton->board.cols; ++col) { | ||
| 21 | struct board *board = &cellular_automaton->board; | ||
| 22 | uint8_t state = | ||
| 23 | board_get (board, row - 1, col + 0) << 3 | | ||
| 24 | board_get (board, row + 0, col - 1) << 1 | | ||
| 25 | board_get (board, row + 0, col + 0) << 0 | | ||
| 26 | board_get (board, row + 0, col + 1) << 2 | | ||
| 27 | board_get (board, row + 1, col + 0) << 4; | ||
| 28 | |||
| 29 | // if (board_get (board, row, col)) { | ||
| 30 | // // printf ("%d/%d, ", row, col); | ||
| 31 | // ++alive; | ||
| 32 | // } | ||
| 33 | |||
| 34 | setbit (cellular_automaton->back_board_bitarray, | ||
| 35 | row*board->cols + col, | ||
| 36 | cellular_automaton->function >> state & 1); | ||
| 37 | } | ||
| 38 | } // printf ("%u\n", alive); | ||
| 39 | |||
| 40 | SWAP (cellular_automaton->back_board_bitarray, | ||
| 41 | cellular_automaton->board.bitarray.bytes); | ||
| 42 | } | ||
| 43 | |||
| 44 | void | ||
| 45 | cellular_automaton_free (struct cellular_automaton *cellular_automaton) | ||
| 46 | { | ||
| 47 | board_free (&cellular_automaton->board); | ||
| 48 | free (cellular_automaton->back_board_bitarray); | ||
| 49 | } | ||
