first commit

This commit is contained in:
Justine Pelletreau 2023-05-26 17:59:08 +02:00
commit 018918c92a
5 changed files with 255 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

84
Cargo.lock generated Normal file
View File

@ -0,0 +1,84 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[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.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hangman"
version = "0.1.0"
dependencies = [
"random_word",
]
[[package]]
name = "libc"
version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[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 = "random_word"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b2bb830d03b36582fd6723a57d2451b9db74574d21c34db9d7122c96b24fa0"
dependencies = [
"rand",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "hangman"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
random_word = "0.3.0"

69
README.md Normal file
View File

@ -0,0 +1,69 @@
# What ?
A hangman. I was bored and needed to get into Rust again.
# How ?
just cargo run it.
```
IM A BORED SYSADMIN
Try to find the word, he
e
Letter > NO
===> ['e'], LIVES 9
_____
A
Letter > NO
===> ['e', 'a'], LIVES 8
_____
i
Letter > YES
===> ['e', 'a', 'i'], LIVES 8
_i___
u
Letter > NO
===> ['e', 'a', 'i', 'u'], LIVES 7
_i___
o
Letter > YES
===> ['e', 'a', 'i', 'u', 'o'], LIVES 7
_i_o_
p
Letter > NO
===> ['e', 'a', 'i', 'u', 'o', 'p'], LIVES 6
_i_o_
n
Letter > NO
===> ['e', 'a', 'i', 'u', 'o', 'p', 'n'], LIVES 5
_i_o_
s
Letter > YES
===> ['e', 'a', 'i', 'u', 'o', 'p', 'n', 's'], LIVES 5
_i_os
l
Letter > NO
===> ['e', 'a', 'i', 'u', 'o', 'p', 'n', 's', 'l'], LIVES 4
_i_os
t
Letter > NO
===> ['e', 'a', 'i', 'u', 'o', 'p', 'n', 's', 'l', 't'], LIVES 3
_i_os
d
Letter > NO
===> ['e', 'a', 'i', 'u', 'o', 'p', 'n', 's', 'l', 't', 'd'], LIVES 2
_i_os
v
Letter > NO
===> ['e', 'a', 'i', 'u', 'o', 'p', 'n', 's', 'l', 't', 'd', 'v'], LIVES 1
_i_os
b
Letter > NO
===> ['e', 'a', 'i', 'u', 'o', 'p', 'n', 's', 'l', 't', 'd', 'v', 'b'], LIVES 0
_i_os
The word was "giros"
You died, sucker
```

92
src/main.rs Normal file
View File

@ -0,0 +1,92 @@
const LATIN_ALPHABET: &str = "abcdefghijklmnopqrstuvwxyz";
use std::io;
use std::io::*;
use std::process;
use random_word;
#[derive(Debug)]
struct GameData<'a> {
word: String,
lives: u16,
alphabet: &'a str,
letters_tried: Vec<char>,
letters_found: Vec<char>,
}
impl GameData<'static> {
fn new() -> GameData<'static> {
Self {
word: String::from(random_word::gen()),
lives: 10,
alphabet: LATIN_ALPHABET,
letters_tried: Vec::new(),
letters_found: Vec::new(),
}
}
fn play(&mut self, letter: &char) {
if self.letters_tried.contains(letter) {
println!("Already tried this letter !");
} else {
self.letters_tried.push(letter.clone());
if self.word.chars().collect::<Vec<char>>().contains(letter) {
println!("YES");
self.letters_found.push(letter.clone());
} else {
println!("NO");
self.lives -= 1;
}
}
}
fn end_turn(&self) {
let mut printme = String::new();
for letter in self.word.chars() {
if self.letters_found.contains(&letter) {
printme.push(letter);
} else {
printme.push('_');
}
}
println!("{}", printme);
if !printme.chars().collect::<Vec<char>>().contains(&'_') {
println!("You won ! You're soooo smart.");
process::exit(0);
}
}
}
fn main() {
let mut my_game = GameData::new();
println!("IM A BORED SYSADMIN\n\nTry to find the word, he\n\n");
let stdin = io::stdin();
loop {
//Dead ?
if &my_game.lives < &1 {
println!("The word was {:?}", &my_game.word);
println!("You died, sucker");
process::exit(0);
}
//Alive !
let mut user_input = String::new();
print!("Letter > ");
stdin.read_line(&mut user_input).unwrap();
let tried = user_input
.to_lowercase()
.chars()
.collect::<Vec<char>>()
.first()
.unwrap_or(&'a')
.clone();
*&mut my_game.play(&tried);
println!("===> {:?}, LIVES {}", my_game.letters_tried, my_game.lives);
my_game.end_turn();
}
}