Last active
May 22, 2025 14:14
-
-
Save fasterthanlime/bcbc16c936bd28e3a117a505ebac8b01 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Update 2025-05-22 β ported to rand v0.9, using a struct with Display impl rather than returning a string | |
struct MarineLine; | |
impl std::fmt::Display for MarineLine { | |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
use rand::{Rng, seq::IndexedRandom}; | |
let marine_life = ["π³", "π ", "π¦", "π", "π‘", "π¬", "π", "π¦", "π"]; | |
let water = [ | |
"\x1B[38;2;0;100;200mβ\x1B[0m", | |
"\x1B[38;2;10;110;210mβ\x1B[0m", | |
"\x1B[38;2;20;120;220mβ\x1B[0m", | |
"\x1B[38;2;30;130;230mβ\x1B[0m", | |
"\x1B[38;2;40;140;240mβ\x1B[0m", | |
"\x1B[38;2;50;150;250mβ\x1B[0m", | |
"\x1B[38;2;60;160;255mβ\x1B[0m", | |
"\x1B[38;2;70;170;255mβ\x1B[0m", | |
"\x1B[38;2;80;180;255mβ\x1B[0m", | |
"\x1B[38;2;90;190;255mβ\x1B[0m", | |
"\x1B[38;2;100;200;255mβ\x1B[0m", | |
"\x1B[38;2;110;210;255mβ\x1B[0m", | |
"\x1B[38;2;120;220;255mβ\x1B[0m", | |
"\x1B[38;2;130;230;255mβ\x1B[0m", | |
"\x1B[38;2;140;240;255mβ\x1B[0m", | |
]; | |
let mut rng = rand::rng(); | |
let mut last_generated = ""; | |
for i in 0..80 { | |
let position = i as f32 / 80.0; | |
let middle_weight = 1.0 - (position - 0.5).abs() * 2.0; | |
let chance = 0.125 + middle_weight * 0.125; // Ranges from 0.125 to 0.25 | |
let set = if rng.random::<f32>() < (chance * 0.5) { | |
&marine_life[..] | |
} else { | |
&water[..] | |
}; | |
let generated = *set.choose(&mut rng).unwrap(); | |
if generated == last_generated { | |
let fallback = water.choose(&mut rng).unwrap(); | |
write!(f, "{}", fallback)?; | |
} else { | |
write!(f, "{}", generated)?; | |
last_generated = generated; | |
} | |
} | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
...and here is a Clojure version with two themes (
:desert
and:marine
):