From 8bb52a989896d60e0a84f574c9a48ae0f1655b20 Mon Sep 17 00:00:00 2001 From: kulisak12 Date: Fri, 23 Sep 2022 13:54:20 +0200 Subject: [PATCH] =?UTF-8?q?Strategick=C3=A1:=20Fix=20modulen=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- klient/strategy.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/klient/strategy.cpp b/klient/strategy.cpp index d5f9d85..a12bdbd 100644 --- a/klient/strategy.cpp +++ b/klient/strategy.cpp @@ -225,6 +225,12 @@ State* state; // Algoritmy +// modulení jako v Pythonu +int py_mod(int a, int b) +{ + return (a % b + b) % b; +} + // otoč směr podle jiného otočení Direction combine_directions(Direction a, Direction b) { @@ -246,33 +252,27 @@ Direction invert_direction(Direction a) // další políčko v daném směru Field* get_neighbour_field(Field* f, Direction direction) { - int neighbour_i, neighbour_j; + int neighbour_i = f->i; + int neighbour_j = f->j; switch (direction) { case UP: neighbour_i = f->i - 1; - neighbour_j = f->j; break; case DOWN: neighbour_i = f->i + 1; - neighbour_j = f->j; break; case LEFT: - neighbour_i = f->i; neighbour_j = f->j - 1; break; case RIGHT: - neighbour_i = f->i; neighbour_j = f->j + 1; break; - default: - neighbour_i = f->i; - neighbour_j = f->j; } // zajisti, aby souřadnice byly v rozsahu herní plochy - neighbour_i %= state->world.size(); - neighbour_j %= state->world[0].size(); + neighbour_i = py_mod(neighbour_i, state->world.size()); + neighbour_j = py_mod(neighbour_j, state->world[0].size()); return state->world[neighbour_i][neighbour_j]; }