Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SemenMartynov committed Feb 4, 2023
0 parents commit 6594bb7
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
75 changes: 75 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "combination_lock"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand="0.8.4"
53 changes: 53 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_must_use)]

use rand::{thread_rng, Rng};
use std::io::stdin;

enum State {
Locked,
Failed,
Unlocked
}

fn main() {
let mut rng = thread_rng();
let x: u32 = rng.gen_range(0..10000);
let code = String::from(x.to_string());
let mut state = State::Locked;
let mut entry = String::new();

loop {
match state {
State::Locked => {
let mut input = String::new();
match stdin().read_line(&mut input) {
Ok(_) => {
entry.push_str(&input.trim_end());
}
Err(_) => continue
}

if entry == code {
state = State::Unlocked;
continue;
}

if !code.starts_with(&entry) {
state = State::Failed;
}
}
State::Failed => {
println!("Failed");
entry.clear();
state = State::Locked;
continue;
}
State::Unlocked => {
println!("UNLOCKED");
return;
}
}
}
}

0 comments on commit 6594bb7

Please sign in to comment.