added turn processing
This commit is contained in:
parent
d0d7163921
commit
7774668066
@ -1,5 +1,5 @@
|
||||
use rand::prelude::*;
|
||||
use std::{thread, time::Duration};
|
||||
use std::{borrow::Borrow, thread, time::Duration};
|
||||
|
||||
const COLORS: [&str; 5] = [
|
||||
"\x1b[42m", // Green background
|
||||
@ -23,13 +23,28 @@ const SQUARES: [[&str; 3]; 4] = [
|
||||
fn main() {
|
||||
let turns: Vec<i8> = gen_turn_array();
|
||||
let mut amnt: i8 = 0;
|
||||
let good_answer: bool = true;
|
||||
let mut good_answer: bool = true;
|
||||
|
||||
show_base("Press any button to start the game".to_string());
|
||||
|
||||
while good_answer {
|
||||
show_game(amnt, turns.clone());
|
||||
amnt += 1;
|
||||
|
||||
let mut response: &str = "";
|
||||
|
||||
if response == "q" {
|
||||
println!("Goodbye!");
|
||||
return;
|
||||
} else {
|
||||
if process_response(amnt, turns.clone(), response) {
|
||||
println!("Good job! Now we add a turn.");
|
||||
} else {
|
||||
println!("Sorry mate, wrong sequence.");
|
||||
println!("Turns: {}", amnt);
|
||||
good_answer = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,6 +59,25 @@ fn gen_turn_array() -> Vec<i8> {
|
||||
return vec;
|
||||
}
|
||||
|
||||
fn process_response(amnt: i8, turns: Vec<i8>, response: &str) -> bool {
|
||||
for i in 0..amnt {
|
||||
if let Some(ch) = response.chars().nth(i as usize) {
|
||||
if let Some(num) = ch.to_digit(10) {
|
||||
let value_as_i8 = num as i8;
|
||||
if turns[i as usize] != value_as_i8 {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
println!("The char #{} is not a digit.", i);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
fn show_game(amnt: i8, turns: Vec<i8>) {
|
||||
for i in 0..amnt {
|
||||
print!("{esc}[2J{esc}[1;1H", esc = 27 as char); //Clear the screen
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user