From fb8996a72ded8154c881c8f33fef019c741de770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?U=C4=9Fur=20K=C3=BCpeli?= Date: Wed, 4 May 2022 03:21:28 +0300 Subject: [PATCH] init temp commit -- does not compile --- 62_Mugwump/rust/Cargo.toml | 9 ++ 62_Mugwump/rust/src/main.rs | 173 ++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 62_Mugwump/rust/Cargo.toml create mode 100644 62_Mugwump/rust/src/main.rs diff --git a/62_Mugwump/rust/Cargo.toml b/62_Mugwump/rust/Cargo.toml new file mode 100644 index 00000000..3b1d02f5 --- /dev/null +++ b/62_Mugwump/rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" diff --git a/62_Mugwump/rust/src/main.rs b/62_Mugwump/rust/src/main.rs new file mode 100644 index 00000000..fadea906 --- /dev/null +++ b/62_Mugwump/rust/src/main.rs @@ -0,0 +1,173 @@ +#![allow(dead_code)] + +struct Game { + coords: Vec, + tries: u8, + mugwumps: Vec, + pub state: GameState, +} + +impl Game { + fn new() -> Self { + let mut coords = Vec::new(); + + let mut x = 0; + let mut y: i8 = 9; + + while y >= 0 { + for _ in 0..10 { + println!("current pos: {:?}", (x, y)); + coords.push(Coordinate::new((x, y as usize), false)); + + x += 1; + + if (x % 10) == 0 { + break; + } + } + + x = 0; + y -= 1; + } + + let mut mugwumps = Vec::new(); + let mut coords_clone = coords.clone(); + + use rand::prelude::IteratorRandom; + + for _ in 0..4 { + let (i, &mut mut out) = coords_clone + .iter_mut() + .enumerate() + .choose(&mut rand::thread_rng()) + .unwrap(); + + coords_clone.remove(i); + out.state = CoordState::HasMugwump; + mugwumps.push(out); + } + + println!("{:#?}", mugwumps); + + Game { + coords, + tries: 0, + mugwumps, + state: GameState::Playing, + } + } + + fn draw_board(&self) { + let draw_top_bottom = |is_top: bool| { + let (mut left, mut right) = ("╔", "╗"); + + if !is_top { + (left, right) = ("╚", "╝"); + } + + for i in 0..11 { + if i == 0 { + print!("{}══", left); + } else if i == 10 { + print!("═══{}", right) + } else { + print!("══"); + } + } + println!(""); + }; + + println!("coords length: {}", self.coords.len()); + + draw_top_bottom(true); + + // Draw points + let mut y: i8 = 9; + + print!("║ {} ", y); + + for (i, c) in self.coords.iter().enumerate() { + let mut char = '-'; + + match c.state { + CoordState::Normal => (), + CoordState::HasMugwump => char = '𑗌', + CoordState::Checked => char = '*', + } + + print!("{} ", char); + + if (i % 10) == 0 { + print!("║"); + println!(""); + print!("║ {} ", y); + y -= 1; + } + } + + print!("║ 𑗌 "); + for i in 0..10 { + print!("{} ", i); + + if i == 9 { + print!("║"); + } + } + println!(""); + + draw_top_bottom(false); + } +} + +enum GameState { + Playing, + Win, + Lose, +} + +#[derive(Clone, Copy, Debug)] +struct Coordinate { + x: usize, + y: usize, + state: CoordState, +} + +impl Coordinate { + fn new(pos: (usize, usize), has_mugwump: bool) -> Self { + let state = if has_mugwump { + CoordState::HasMugwump + } else { + CoordState::Normal + }; + + Coordinate { + x: pos.0, + y: pos.1, + state, + } + } +} + +#[derive(Clone, Copy, Debug)] +enum CoordState { + Normal, + HasMugwump, + Checked, +} + +fn main() { + println!("\n\nMUGWUMP"); + println!("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n"); + + println!("THE OBJECT OF THIS GAME IS TO FIND FOUR MUGWUMPS"); + println!("HIDDEN ON A 10 BY 10 GRID. HOMEBASE IS POSITION 0,0."); + println!("ANY GUESS YOU MAKE MUST BE TWO NUMBERS WITH EACH"); + println!("NUMBER BETWEEN 0 AND 9, INCLUSIVE. FIRST NUMBER"); + println!("IS DISTANCE TO RIGHT OF HOMEBASE AND SECOND NUMBER"); + println!("IS DISTANCE ABOVE HOMEBASE!"); + println!("YOU GET 10 TRIES. AFTER EACH TRY, I WILL TELL"); + println!("YOU HOW FAR YOU ARE FROM EACH MUGWUMP.\n"); + + let game = Game::new(); + game.draw_board(); +}