repl.it
@carloscarcamo/

rust-piedra-papel-tijeras

Rust

Rust Lab - Mozilla Activate 2019

fork
loading
Files
  • main.rs
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::io;
//use rand::Rng;

fn main() {
    println!("**********************************************************");
    println!("*                     RUST LAB                           *");
    println!("*          PIEDRA PAPEL Y TIJERAS EN RUST                *");
    println!("**********************************************************");
    loop {
        println!("User 1 Selecccione: Piedra(p), Papel(pp), Tijeras(t)");

        let mut choice = String::new();

        io::stdin().read_line(&mut choice).unwrap();

        let weapon: Weapon = get_user_weapon(choice.to_lowercase().trim());
        
        println!("Bot Selecccione: Piedra(p), Papel(pp), Tijeras(t)");
        
        let mut bot_choice = String::new();

        io::stdin().read_line(&mut bot_choice).unwrap();

        let bot_weapon: Weapon = get_user_weapon(bot_choice.to_lowercase().trim());
        
        //let bot_weapon: Weapon = get_bot_weapon();


        play_round(&weapon, &bot_weapon);
    }
}

fn play_round(user_weapon: &Weapon, bot_weapon: &Weapon) {
    fn handle_rock(bot_weapon: &Weapon) -> ResultType {
        match bot_weapon {
            Weapon::PIEDRA => ResultType::TIE,
            Weapon::PAPEL => ResultType::LOSS,
            Weapon::TIJERAS => ResultType::WIN,
        }
    }

    fn handle_paper(bot_weapon: &Weapon) -> ResultType {
        match bot_weapon {
            Weapon::PIEDRA => ResultType::WIN,
            Weapon::PAPEL => ResultType::TIE,
            Weapon::TIJERAS => ResultType::LOSS,
        }
    }

    fn handle_scissors(bot_weapon: &Weapon) -> ResultType {
        match bot_weapon {
            Weapon::PIEDRA => ResultType::LOSS,
            Weapon::PAPEL => ResultType::WIN,
            Weapon::TIJERAS => ResultType::TIE,
        }
    }

    let result = match user_weapon {
        Weapon::PIEDRA => handle_rock(bot_weapon),
        Weapon::PAPEL => handle_paper(bot_weapon),
        Weapon::TIJERAS => handle_scissors(bot_weapon),
    };

    match result {
        ResultType::WIN => println!("Ganaste! Elegiste: {:?} y el bot eligió: {:?}", user_weapon, bot_weapon),
        ResultType::LOSS => println!("Perdiste! Elegiste: {:?} y el bot eligió: {:?}", user_weapon, bot_weapon),
        ResultType::TIE => println!("Empate! Elegiste: {:?} y el bot eligió: {:?}", user_weapon, bot_weapon),
    }

}


enum ResultType {
    WIN,
    LOSS,
    TIE
}

// The `derive` attribute automatically creates the implementation
// required to make this `struct` printable with `fmt::Debug`.
#[derive(Debug)]
enum Weapon {
    PIEDRA,
    PAPEL,
    TIJERAS
}

fn get_user_weapon(player_choice: &str) -> Weapon {
    match player_choice {
        "piedra" => Weapon::PIEDRA,
        "p" => Weapon::PIEDRA,
        "papel" => Weapon::PAPEL,
        "pp" => Weapon::PAPEL,
        "tijeras" => Weapon::TIJERAS,
        "t" => Weapon::TIJERAS,
        _ => panic!("Error"),
    }
}

//pub fn get_bot_weapon() -> Weapon {
//    let secret_number = rand::thread_rng().gen_range(1, 4);
//    match secret_number {
//        1 => Weapon::PIEDRA,
//        2 => Weapon::PAPEL,
//        3 => Weapon::TIJERAS,
//       _ => panic!("No deberia entrar aca!"),
//    }
//}
Fetching token
?