Added logic for playing the game
This commit is contained in:
parent
7774668066
commit
462ae1ec82
@ -1,5 +1,5 @@
|
||||
use rand::prelude::*;
|
||||
use std::{borrow::Borrow, thread, time::Duration};
|
||||
use std::{io, io::prelude::*, thread, time::Duration};
|
||||
|
||||
const COLORS: [&str; 5] = [
|
||||
"\x1b[42m", // Green background
|
||||
@ -9,10 +9,8 @@ const COLORS: [&str; 5] = [
|
||||
"\x1b[47m", // White background
|
||||
];
|
||||
|
||||
// Define the reset color escape code
|
||||
const RESET: &str = "\x1b[0m";
|
||||
|
||||
// Define the squares content
|
||||
const SQUARES: [[&str; 3]; 4] = [
|
||||
[" ", " 1 ", " "],
|
||||
[" ", " 2 ", " "],
|
||||
@ -21,30 +19,60 @@ const SQUARES: [[&str; 3]; 4] = [
|
||||
];
|
||||
|
||||
fn main() {
|
||||
let turns: Vec<i8> = gen_turn_array();
|
||||
let mut turns: Vec<i8> = gen_turn_array();
|
||||
let mut amnt: i8 = 0;
|
||||
let mut good_answer: bool = true;
|
||||
let mut playing: bool = true;
|
||||
let mut high_score: i8 = 0;
|
||||
|
||||
show_base("Press any button to start the game".to_string());
|
||||
show_base("Press enter to start the game".to_string());
|
||||
let _ = io::stdin().read(&mut [0u8]).unwrap();
|
||||
|
||||
while good_answer {
|
||||
show_game(amnt, turns.clone());
|
||||
amnt += 1;
|
||||
while playing {
|
||||
while good_answer {
|
||||
show_game(amnt, turns.clone());
|
||||
amnt += 1;
|
||||
|
||||
let mut response: &str = "";
|
||||
let mut response: String = String::new();
|
||||
print!("Enter the sequence: ");
|
||||
let _ = io::stdout().flush();
|
||||
io::stdin()
|
||||
.read_line(&mut response)
|
||||
.expect("Failed to read from stdin");
|
||||
|
||||
if response == "q" {
|
||||
println!("Goodbye!");
|
||||
return;
|
||||
} else {
|
||||
if process_response(amnt, turns.clone(), response) {
|
||||
println!("Good job! Now we add a turn.");
|
||||
if response.trim() == "q" {
|
||||
println!("Goodbye!");
|
||||
return;
|
||||
} else {
|
||||
println!("Sorry mate, wrong sequence.");
|
||||
println!("Turns: {}", amnt);
|
||||
good_answer = false;
|
||||
if process_response(amnt, turns.clone(), response) {
|
||||
println!("Good job! Now we add a turn.");
|
||||
println!("Press enter to continue...");
|
||||
let _ = io::stdin().read(&mut [0u8]).unwrap();
|
||||
} else {
|
||||
if high_score < amnt {
|
||||
high_score = amnt;
|
||||
}
|
||||
println!("Sorry mate, wrong sequence.");
|
||||
println!("Turns: {}", amnt);
|
||||
println!("High score: {}", high_score);
|
||||
good_answer = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
print!("Wanna play again? (y/n) ");
|
||||
let mut response: String = String::new();
|
||||
let _ = io::stdout().flush();
|
||||
io::stdin()
|
||||
.read_line(&mut response)
|
||||
.expect("Failure reading input.");
|
||||
if response.trim() == "y" {
|
||||
playing = true;
|
||||
good_answer = true;
|
||||
amnt = 0;
|
||||
turns = gen_turn_array();
|
||||
} else {
|
||||
playing = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,7 +87,7 @@ fn gen_turn_array() -> Vec<i8> {
|
||||
return vec;
|
||||
}
|
||||
|
||||
fn process_response(amnt: i8, turns: Vec<i8>, response: &str) -> bool {
|
||||
fn process_response(amnt: i8, turns: Vec<i8>, response: String) -> bool {
|
||||
for i in 0..amnt {
|
||||
if let Some(ch) = response.chars().nth(i as usize) {
|
||||
if let Some(num) = ch.to_digit(10) {
|
||||
@ -79,7 +107,7 @@ fn process_response(amnt: i8, turns: Vec<i8>, response: &str) -> bool {
|
||||
}
|
||||
|
||||
fn show_game(amnt: i8, turns: Vec<i8>) {
|
||||
for i in 0..amnt {
|
||||
for i in 0..amnt + 1 {
|
||||
print!("{esc}[2J{esc}[1;1H", esc = 27 as char); //Clear the screen
|
||||
println!("============== Simon Game ==============");
|
||||
let turn: i8 = turns[i as usize];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user