Skip to main content

tui/
tui.rs

1// Simple text-based user interface
2
3use slot_machine::game::Game;
4use std::thread::sleep;
5use std::time::Duration;
6
7const BALANCE: u32 = 1000;
8const BET_SIZE: u32 = 1;
9const BET_MIN: u32 = 1;
10const BET_MAX: u32 = 10;
11
12fn main() {
13    println!("Greetings!");
14
15    println!("Your balance: {} credits", BALANCE);
16    println!("Bet size: {}", BET_SIZE);
17    print_help();
18
19    let mut game = Game::new(BALANCE, BET_SIZE, BET_MIN, BET_MAX).unwrap();
20
21    loop {
22        let mut command = String::new();
23
24        std::io::stdin()
25            .read_line(&mut command)
26            .expect("Failed to read command!");
27
28        match command.trim().to_uppercase().as_str() {
29            "BALANCE" => println!("Your balance: {} credits.", game.credits()),
30            "BET" => println!("Current bet: {} credits.", game.bet()),
31            "BET PLUS" => match bet_plus(&mut game) {
32                Ok(val) => println!("Bet size: {}.", val),
33                Err(e) => println!("{}", e),
34            },
35            "BET MINUS" => match bet_minus(&mut game) {
36                Ok(val) => println!("Bet size: {}.", val),
37                Err(e) => println!("{}", e),
38            },
39            "SPIN" => spin(&mut game),
40            val if val.starts_with("AUTOSPIN") => {
41                let split = val.split(" ");
42
43                let number_spins = split.last().unwrap().parse::<u32>().unwrap();
44
45                for _ in 0..number_spins {
46                    spin(&mut game);
47                    sleep(Duration::from_secs(1));
48                }
49            }
50            "PAYOUTS" => {
51                println!("3 Jackpot = x1666");
52                println!("3 Seven = x300");
53                println!("3 TripleBar = x100");
54                println!("3 DoubleBar = x50");
55                println!("3 Bar = x25");
56                println!("3 of any Bar = x12");
57                println!("3 Cherry = x12");
58                println!("2 Cherry = x6");
59                println!("1 Cherry = x3");
60            }
61            "HELP" => print_help(),
62            _ => println!("Invalid command!"),
63        }
64    }
65}
66
67fn spin(game: &mut Game) {
68    let symbols = game.spin();
69    match symbols {
70        Ok(val) => {
71            println!("{:?}", val);
72            println!("You win {} credits", game.win());
73        }
74        Err(e) => println!("{}", e.to_owned()),
75    }
76}
77
78// Increase bet size
79fn bet_plus(game: &mut Game) -> Result<u32, String> {
80    let bet_size = match game.bet() {
81        1 => 2,
82        2 => 3,
83        3 => 5,
84        5 => 10,
85        BET_MAX => return Err("Max bet size!".to_owned()),
86        _ => return Err("Invalid bet size!".to_owned()),
87    };
88
89    game.set_bet(bet_size).unwrap();
90
91    Ok(bet_size)
92}
93
94// Decrease bet size
95fn bet_minus(game: &mut Game) -> Result<u32, String> {
96    let bet_size = match game.bet() {
97        10 => 5,
98        5 => 3,
99        3 => 2,
100        2 => 1,
101        BET_MIN => return Err("Min bet size!".to_owned()),
102        _ => return Err("Invalid bet size!".to_owned()),
103    };
104
105    game.set_bet(bet_size).unwrap();
106
107    Ok(bet_size)
108}
109
110// Prints help text
111fn print_help() {
112    println!("To get a balance, put the `balance`.");
113    println!("To get a bet size, put the `bet`.");
114    println!("To increase or decrease the size of the bet, put `bet plus` or `bet minus`.");
115    println!("To activate auto-spin, put `autospin <NUMBER>` where NUMBER is the number of spins.");
116    println!("To show the payout table, put `payouts`.");
117}