Added logic for playing the game
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use std::{borrow::Borrow, thread, time::Duration};
|
use std::{io, io::prelude::*, thread, time::Duration};
|
||||||
|
|
||||||
const COLORS: [&str; 5] = [
|
const COLORS: [&str; 5] = [
|
||||||
"\x1b[42m", // Green background
|
"\x1b[42m", // Green background
|
||||||
@@ -9,10 +9,8 @@ const COLORS: [&str; 5] = [
|
|||||||
"\x1b[47m", // White background
|
"\x1b[47m", // White background
|
||||||
];
|
];
|
||||||
|
|
||||||
// Define the reset color escape code
|
|
||||||
const RESET: &str = "\x1b[0m";
|
const RESET: &str = "\x1b[0m";
|
||||||
|
|
||||||
// Define the squares content
|
|
||||||
const SQUARES: [[&str; 3]; 4] = [
|
const SQUARES: [[&str; 3]; 4] = [
|
||||||
[" ", " 1 ", " "],
|
[" ", " 1 ", " "],
|
||||||
[" ", " 2 ", " "],
|
[" ", " 2 ", " "],
|
||||||
@@ -21,31 +19,61 @@ const SQUARES: [[&str; 3]; 4] = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let turns: Vec<i8> = gen_turn_array();
|
let mut turns: Vec<i8> = gen_turn_array();
|
||||||
let mut amnt: i8 = 0;
|
let mut amnt: i8 = 0;
|
||||||
let mut good_answer: bool = true;
|
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 playing {
|
||||||
while good_answer {
|
while good_answer {
|
||||||
show_game(amnt, turns.clone());
|
show_game(amnt, turns.clone());
|
||||||
amnt += 1;
|
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" {
|
if response.trim() == "q" {
|
||||||
println!("Goodbye!");
|
println!("Goodbye!");
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
if process_response(amnt, turns.clone(), response) {
|
if process_response(amnt, turns.clone(), response) {
|
||||||
println!("Good job! Now we add a turn.");
|
println!("Good job! Now we add a turn.");
|
||||||
|
println!("Press enter to continue...");
|
||||||
|
let _ = io::stdin().read(&mut [0u8]).unwrap();
|
||||||
} else {
|
} else {
|
||||||
|
if high_score < amnt {
|
||||||
|
high_score = amnt;
|
||||||
|
}
|
||||||
println!("Sorry mate, wrong sequence.");
|
println!("Sorry mate, wrong sequence.");
|
||||||
println!("Turns: {}", amnt);
|
println!("Turns: {}", amnt);
|
||||||
|
println!("High score: {}", high_score);
|
||||||
good_answer = false;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_turn_array() -> Vec<i8> {
|
fn gen_turn_array() -> Vec<i8> {
|
||||||
@@ -59,7 +87,7 @@ fn gen_turn_array() -> Vec<i8> {
|
|||||||
return vec;
|
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 {
|
for i in 0..amnt {
|
||||||
if let Some(ch) = response.chars().nth(i as usize) {
|
if let Some(ch) = response.chars().nth(i as usize) {
|
||||||
if let Some(num) = ch.to_digit(10) {
|
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>) {
|
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
|
print!("{esc}[2J{esc}[1;1H", esc = 27 as char); //Clear the screen
|
||||||
println!("============== Simon Game ==============");
|
println!("============== Simon Game ==============");
|
||||||
let turn: i8 = turns[i as usize];
|
let turn: i8 = turns[i as usize];
|
||||||
|
|||||||
Reference in New Issue
Block a user