diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-08-12 15:04:07 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-08-12 15:04:07 +0300 |
commit | 01d4df646900c9581a9680dad80f846864938f91 (patch) | |
tree | cfab035e52c808e872ae5581f14556b45771766a | |
parent | f0b443c37daa92d36ea752d837855a28b084dd3c (diff) |
optimize code
-rw-r--r-- | src/main.c | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -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); |