Last active
May 5, 2023 09:01
-
-
Save matt-dray/0f4fba9a7e57bdb093d0b9433a983c61 to your computer and use it in GitHub Desktop.
A bit of fun experimenting with tile-based 'gravity' in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m <- matrix(rep(".", 30), 5, 6) | |
m[4, c(2:4, 6)] <- "X" | |
m[3, 2] <- "X" | |
m[5, 4:6] <- "X" | |
m[1, 4] <- "o" | |
matrix(1:140, 20, 7) | |
m <- pixeltrix::click_pixels(20, 7) | |
m[which(m == 0)] <- "." | |
m[which(m == 1)] <- "X" | |
attributes(m)["colours"] <- NULL | |
m[1, 4] <- "o" | |
m | |
repeat { | |
cat("\014") | |
for (row in seq(nrow(m))) { | |
cat(m[row, ], "\n", sep = "") | |
} | |
Sys.sleep(1) | |
droplet_i <- which(m == "o") | |
droplet_i_save <- droplet_i | |
below_i <- droplet_i + 1 | |
if (m[below_i] == ".") { | |
m[droplet_i] <- "." | |
m[below_i] <- "o" | |
} | |
if (m[below_i] == "X") { | |
left_i <- droplet_i - nrow(m) | |
right_i <- droplet_i + nrow(m) | |
is_left_open <- FALSE | |
is_right_open <- FALSE | |
if (m[left_i] == ".") is_left_open <- TRUE | |
if (m[right_i] == ".") is_right_open <- TRUE | |
if (is_left_open & is_right_open) { | |
sampled_direction_i <- sample(c(left_i, right_i), 1) | |
m[droplet_i] <- "." | |
m[sampled_direction_i] <- "o" | |
} | |
if (is_left_open & !is_right_open) { | |
m[droplet_i] <- "." | |
m[left_i] <- "o" | |
} | |
if (!is_left_open & is_right_open) { | |
m[droplet_i] <- "." | |
m[right_i] <- "o" | |
} | |
} | |
droplet_i <- which(m == "o") | |
if (droplet_i == droplet_i_save) break | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment