Added timer so you can have a highscore rustsweeper
This commit is contained in:
parent
8ed27b637a
commit
87af9b73a0
@ -2,9 +2,11 @@ use std::io::{stdin,stdout,Write};
|
||||
use rand::Rng;
|
||||
use colored::*;
|
||||
use console::Term;
|
||||
use std::time::Instant;
|
||||
|
||||
fn output_board(board: &Vec<Vec<i64>>, state: &Vec<Vec<char>>, x_hovered: i64, y_hovered: i64) {
|
||||
print!("{}[2J", 27 as char);
|
||||
let mut output: String = "".to_string();
|
||||
|
||||
for y in 0..board.len() {
|
||||
for x in 0..board[y].len() {
|
||||
let background_color: u8;
|
||||
@ -17,52 +19,54 @@ fn output_board(board: &Vec<Vec<i64>>, state: &Vec<Vec<char>>, x_hovered: i64, y
|
||||
}
|
||||
|
||||
if x == x_hovered as usize && y == y_hovered as usize {
|
||||
print!("{}", "X".truecolor(0,0,0).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"X".truecolor(0,0,0).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if state[y][x] == 'u' {
|
||||
if board[y][x] == -1 {
|
||||
print!("{}","▲".truecolor(128,64,0).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"▲".truecolor(128,64,0).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if board[y][x] == 0 {
|
||||
print!("{}"," ".on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&" ".on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if board[y][x] == 1 {
|
||||
print!("{}", "1".truecolor(1,0,254).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"1".truecolor(1,0,254).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if board[y][x] == 2 {
|
||||
print!("{}", "2".truecolor(1,127,1).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"2".truecolor(1,127,1).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if board[y][x] == 3 {
|
||||
print!("{}", "3".truecolor(254,0,0).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"3".truecolor(254,0,0).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if board[y][x] == 4 {
|
||||
print!("{}", "4".truecolor(1,0,128).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"4".truecolor(1,0,128).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if board[y][x] == 5 {
|
||||
print!("{}", "5".truecolor(129,1,2).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"5".truecolor(129,1,2).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if board[y][x] == 6 {
|
||||
print!("{}", "6".truecolor(0,128,129).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"6".truecolor(0,128,129).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if board[y][x] == 7 {
|
||||
print!("{}", "7".truecolor(0,0,0).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"7".truecolor(0,0,0).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if board[y][x] == 8 {
|
||||
print!("{}", "8".truecolor(128,128,128).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"8".truecolor(128,128,128).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
}
|
||||
else if state[y][x] == 'm' {
|
||||
print!("{}", "⚑".truecolor(228,16,32).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"⚑".truecolor(228,16,32).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else if state[y][x] == '?' {
|
||||
print!("{}", "?".truecolor(0,0,0).on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&"?".truecolor(0,0,0).on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
else {
|
||||
print!("{}"," ".on_truecolor(background_color,background_color,background_color));
|
||||
output.push_str(&" ".on_truecolor(background_color,background_color,background_color).to_string());
|
||||
}
|
||||
}
|
||||
println!();
|
||||
output.push_str("\n");
|
||||
}
|
||||
print!("{}[2J", 27 as char);
|
||||
println!("{}",output);
|
||||
}
|
||||
|
||||
fn input() -> String{
|
||||
@ -217,7 +221,7 @@ fn uncover(board: &Vec<Vec<i64>>, state: &mut Vec<Vec<char>>, x_hovered: usize,
|
||||
}
|
||||
}
|
||||
|
||||
fn detect_win(state: &Vec<Vec<char>>, number_of_nails: i64) {
|
||||
fn detect_win(state: &Vec<Vec<char>>, number_of_nails: i64, start: Instant) {
|
||||
let mut number_of_covered_cells: i64 = 0;
|
||||
for y in 0..state.len() {
|
||||
for x in 0..state[y].len() {
|
||||
@ -228,6 +232,8 @@ fn detect_win(state: &Vec<Vec<char>>, number_of_nails: i64) {
|
||||
}
|
||||
if number_of_covered_cells == number_of_nails {
|
||||
println!("Congratulations, you won!!!");
|
||||
let end = std::time::Instant::now();
|
||||
println!("In {:?}", end-start);
|
||||
std::process::exit(0);
|
||||
}
|
||||
}
|
||||
@ -238,9 +244,11 @@ fn main() {
|
||||
let mut board: Vec<Vec<i64>> = Vec::new();
|
||||
let mut state: Vec<Vec<char>> = Vec::new(); // u = uncovered, c = covered, m = marked
|
||||
|
||||
let height: i64 = 10;
|
||||
let width: i64 = 30;
|
||||
let number_of_nails: i64 = 35;
|
||||
let height: i64 = 8;
|
||||
let width: i64 = 8;
|
||||
let number_of_nails: i64 = 10;
|
||||
//let percentage_of_cells_are_nails: i64 = 15;
|
||||
//let number_of_nails: i64 = ((percentage_of_cells_are_nails / 100) as i64) * ((width * height) as i64) as i64;
|
||||
|
||||
let mut x_hovered: i64 = 0;
|
||||
let mut y_hovered: i64 = 0;
|
||||
@ -256,6 +264,8 @@ fn main() {
|
||||
//board[x as usize][y as usize] = -1;
|
||||
}
|
||||
}
|
||||
let start = std::time::Instant::now();
|
||||
|
||||
place_nails(&mut board, number_of_nails, width, height);
|
||||
determine_nails_in_range(&mut board);
|
||||
output_board(&board, &state, x_hovered, y_hovered);
|
||||
@ -263,10 +273,10 @@ fn main() {
|
||||
'gameloop: loop {
|
||||
if let Ok(character) = stdout.read_char() {
|
||||
match character {
|
||||
'w' => if y_hovered > 0 { y_hovered -= 1 },
|
||||
'a' => if x_hovered > 0 { x_hovered -= 1},
|
||||
's' => if y_hovered < height-1 { y_hovered += 1 },
|
||||
'd' => if x_hovered < width-1 {x_hovered += 1},
|
||||
'w' | 'k' => if y_hovered > 0 { y_hovered -= 1 },
|
||||
'a' | 'h' => if x_hovered > 0 { x_hovered -= 1},
|
||||
's' | 'j' => if y_hovered < height-1 { y_hovered += 1 },
|
||||
'd' | 'l' => if x_hovered < width-1 {x_hovered += 1},
|
||||
'u' => uncover(&board, &mut state, x_hovered as usize, y_hovered as usize, &mut alive),
|
||||
'm' => {
|
||||
if state[y_hovered as usize][x_hovered as usize] == 'm' {
|
||||
@ -290,7 +300,7 @@ fn main() {
|
||||
}
|
||||
if alive {
|
||||
output_board(&board, &state, x_hovered, y_hovered);
|
||||
detect_win(&state, number_of_nails);
|
||||
detect_win(&state, number_of_nails, start);
|
||||
}
|
||||
else if alive == false {
|
||||
println!("GAME OVER!\nYou got Tetanus!");
|
||||
|
Loading…
x
Reference in New Issue
Block a user