From 01d4df646900c9581a9680dad80f846864938f91 Mon Sep 17 00:00:00 2001 From: kartofen Date: Fri, 12 Aug 2022 15:04:07 +0300 Subject: optimize code --- src/main.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main.c b/src/main.c index 59a1fc0..13c2ed1 100644 --- a/src/main.c +++ b/src/main.c @@ -63,32 +63,31 @@ void collapse_this(int i) exit(EXIT_FAILURE); } - // this bad - int n = 0; - do { - n = rand() % TILES; - if(!(is_set(i, n))) - continue; + small_t possibilities[TILES]; + size_t psz = 0; - collapse(i, n); - } while(!(has_collapsed(i))); + for(size_t n = 0; n < TILES; n++) + if(is_set(i, n)) possibilities[psz++] = n; + + size_t t = possibilities[rand() % psz]; + collapse(i, t); // collapse a random tile // apply a bitmask, based on the direction if(i / SWIDTH != 0) // up if(!(has_collapsed(i-SWIDTH))) - mask(i-SWIDTH, n, 0); + mask(i-SWIDTH, t, 0); if(i % SWIDTH != (SWIDTH - 1)) // right if(!(has_collapsed(i+1))) - mask(i+1, n, 1); + mask(i+1, t, 1); if(i % SWIDTH != 0) // left if(!(has_collapsed(i-1))) - mask(i-1, n, 2); + mask(i-1, t, 2); if(i / SWIDTH != (SHEIGHT - 1)) // down if(!(has_collapsed(i+SWIDTH))) - mask(i+SWIDTH, n, 3); + mask(i+SWIDTH, t, 3); } void manage_arguments(int argc, char **argv) @@ -132,6 +131,7 @@ error: int main(int argc, char **argv) { + // SEED = 4; SEED = time(0); manage_arguments(argc, argv); -- cgit v1.2.3