commit 9ff4aa5e46c76074b40052c68c6d74b6e2301218 Author: Alex Csengery Date: Thu Dec 12 14:34:10 2024 -0500 Starting out and tooling around Initial commit with some general finger painting. I'll be hacking on this in a terrible order and just doing things that seem interesting to me at the time. I kinda-sorta have a general idea of what the end product would look like; but this is just an exercise in messing around with Rust and trying to do some actual programming work that I find remotely interesting. This long commit message is here to remind me down the road of the origins and how far I've come. Hopefully future me is reading this as a reasonable skilled Rustacean with a fun little game to play. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..88428bf --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,140 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.159" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "syn" +version = "2.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thacko" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b15491a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "thacko" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.5" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1ad6987 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,119 @@ +use rand::prelude::*; + +fn main() { + println!("Hello, world!"); + + roll_dice(6); + + let content = vec![ + "This is some content here", + "We'll have all kinds of stuff to write", + "Many things will be here for you to see", + "However, we need to make much more progress", + "I'm sure this won't end well", + ]; + + draw_box(50, content); +} + +fn roll_dice(sides: u8) { + let mut roll: u8 = random(); + roll = roll % sides; + + // make sure we never roll a zero; that's not possible + if roll < 1 { + roll += 1; + } + + println!("Rolling dice with {} sides: {}", sides, roll); +} + +fn draw_box(width: u8, content: Vec<&str>) { + let mut text_box: Vec = Vec::new(); + + text_box.push(top_border(width, "HEALTH AND VITALS")); + for line in content { + text_box.push(create_row(width, '\u{2502}', line)); + } + text_box.push(bottom_border(width)); + + for n in text_box { + println!("{}", n); + } +} + +fn create_row(width: u8, border: char, content: &str) -> String { + let mut row_chars: Vec = Vec::new(); + + row_chars.push(border); + + let mut index = 1; + for c in content.chars() { + row_chars.push(c); + index += 1; + + if index == width - 1 { + break; + } + } + + if index < width - 1 { + for _c in index..width - 1 { + row_chars.push(' '); + } + } + + row_chars.push(border); + + let row: String = row_chars.into_iter().collect(); + + return row; +} + +fn top_border(width: u8, title: &str) -> String { + let mut row = String::new(); + + row.push('\u{250c}'); // top left corner + let mut index = 1; // we have one char in the border now + + if title != "" { + // if the requested title is longer than the top border, truncate it + // we specify "width - 2" here to account for the corner pieces + if title.len() > (width - 2).into() { + for c in title.chars() { + row.push(c); + index += 1; + if index == width - 2 { + break; + } + } + } else { + for c in title.chars() { + row.push(c); + index += 1; + } + } + } + + for _n in index..width - 1 { + row.push('\u{2500}'); + } + + row.push('\u{2510}'); // top right corner + + return row; +} + +fn bottom_border(width: u8) -> String { + let mut row = String::new(); + + row.push('\u{2514}'); // bottom left corner + + for _n in 1..width - 1 { + row.push('\u{2500}'); + } + + row.push('\u{2518}'); // bottom right corner + + return row; +}