From c61995642946273e0efbffa94ea422212cb7e196 Mon Sep 17 00:00:00 2001 From: Reality Enjoyer Date: Tue, 23 Dec 2025 09:19:54 +0000 Subject: [PATCH] slot machine --- slot-machine/Cargo.toml | 7 +++ slot-machine/src/main.rs | 106 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 slot-machine/Cargo.toml create mode 100644 slot-machine/src/main.rs diff --git a/slot-machine/Cargo.toml b/slot-machine/Cargo.toml new file mode 100644 index 0000000..06f8997 --- /dev/null +++ b/slot-machine/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "slot-machine" +version = "0.1.0" +edition = "2024" + +[dependencies] +rand = "0.9.2" diff --git a/slot-machine/src/main.rs b/slot-machine/src/main.rs new file mode 100644 index 0000000..48f5f6f --- /dev/null +++ b/slot-machine/src/main.rs @@ -0,0 +1,106 @@ +use rand::{Rng, rng}; +use std::io; + +enum SpinResult { + Ace(u32), + King(u32), + Queen(u32), + Jack(u32), + Joker(u32), +} + +fn main() { + print_rules(); + let tries = manage_balance(load_money()); + + let mut total_payout: u32 = 0; + + for _ in 0..tries { + let outcome: SpinResult = spin(); + + match outcome { + SpinResult::Ace(num) => { + println!("\tAce! +${num}"); + total_payout += num; + } + SpinResult::King(num) => { + println!("\tKing! +${num}"); + total_payout += num; + } + SpinResult::Queen(num) => { + println!("\tQueen! +${num}"); + total_payout += num; + } + SpinResult::Jack(num) => { + println!("\tJack! +${num}"); + total_payout += num; + } + SpinResult::Joker(num) => { + println!("\tJoker! +${num}"); + total_payout += num; + } + } + } + println!("\nyou walk away with ${total_payout}"); +} + +fn spin() -> SpinResult { + let random: u32 = rng().random_range(0..5); + match random { + 0 => SpinResult::Ace(10), + 1 => SpinResult::King(5), + 2 => SpinResult::Queen(2), + 3 => SpinResult::Jack(1), + 4 => SpinResult::Joker(0), + _ => todo!(), + } +} + +fn load_money() -> (bool, u32, u32, u32) { + let mut user_input_amount = String::new(); + + io::stdin() + .read_line(&mut user_input_amount) + .expect("failed to read user input!"); + + let user_input_amount: u32 = match user_input_amount.trim().parse() { + Ok(num) => num, + Err(_) => todo!(), + }; + + let tries: u32 = user_input_amount / 4; + let change: u32 = user_input_amount % 4; + + if user_input_amount >= 12 { + (true, tries, change, user_input_amount) + } else { + (false, tries, change, user_input_amount) + } +} + +fn manage_balance(user_input: (bool, u32, u32, u32)) -> u32 { + if !user_input.0 { + println!("please try again with $12 or more"); + } else { + if user_input.2 > 0 { + println!("\nhere's your balance: ${}", user_input.2); + }; + println!("you have {} spins...", user_input.1); + } + + user_input.1 +} + +fn print_rules() { + println!("welcome to atlantic city!"); + println!("here are the slot machine rules:"); + println!(" 1. $4 per spin"); + println!(" 2. you must play a minimum of 3 spins"); + println!(" 3. each spin can have 1 of 5 results:"); + println!(" * Ace - $10"); + println!(" * King - $5"); + println!(" * Queen - $2"); + println!(" * Jack - $1"); + println!(" * Joker - $0"); + println!("Please enter at least $12..."); +}