Skip to content

Commit

Permalink
feat: number tower with input
Browse files Browse the repository at this point in the history
  • Loading branch information
Woife5 committed Jan 17, 2024
1 parent 96b9b5c commit f890a42
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.4.3

- Number tower now has to be filled out instead of being calculated automatically.
- Number tower now counts seconds.

# 0.4.2

- update `self_update` dependency, which is now able to replace running files on Windows.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "kopfrechner"
authors = ["Wolfgang Schwendtbauer"]
repository = "https://github.com/Woife5/kopfrechner"
version = "0.4.2"
version = "0.4.3"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/modes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn get_number_input(prompt: String) -> Option<usize> {
_ => match input_text.parse::<usize>() {
Ok(v) => return Some(v),
Err(_) => {
println!("\rNot a valid number, try again.");
println!("\rNot a valid number, try again. (Enter 'q' to quit)");
continue;
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/modes/multiplication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ pub fn run(range: RangeInclusive<u16>) {
}

println!("\n{}", "Results:".bold());
println!("{}: {}", "Correct".green().italic(), correct,);
println!("{}: {}", "Incorrect".red().italic(), incorrect,);
println!("{}: {}", "Correct".green().italic(), correct);
println!("{}: {}", "Incorrect".red().italic(), incorrect);
println!(
"{}: {:.2}s",
"Average time".italic(),
Expand Down
40 changes: 36 additions & 4 deletions src/modes/number_tower.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use colored::Colorize;
use colored::{ColoredString, Colorize};

use super::get_number_input;

Expand All @@ -10,20 +10,52 @@ pub fn prepare_and_run() {
}
}

fn run_iteration(current: &usize, padding: ColoredString) -> Result<(), ()> {
let input = match get_number_input("".to_string()) {
Some(v) => v,
None => return Err(()),
};

let input_str = format!("{:>20}", input);
let result_text = if input == *current {
"Correct!".green()
} else {
format!("Wrong! ({})", current).red()
};

// Move cursor up one line and clear the line
print!("\x1B[1A\x1B[2K\r");
println!("{:>20} {} {}", input_str, padding, result_text);
Ok(())
}

pub fn run(start: usize) {
let start_time = std::time::Instant::now();
let mut current = start;

for i in 2..=9 {
let padding = format!("| × {}", 2).yellow();
println!("{:>20} {}", current, padding);
current *= 2;

for i in 3..=9 {
let padding = format!("| × {}", i).yellow();
println!("{:>20} {}", current, padding);
match run_iteration(&current, padding) {
Err(_) => return,
_ => (),
};
current *= i;
}

for i in 2..=9 {
let padding = format!("| ÷ {}", i).yellow();
println!("{:>20} {}", current, padding);
match run_iteration(&current, padding) {
Err(_) => return,
_ => (),
};
current /= i;
}

println!("{:>20} {}", current, "| Result".yellow());
let elapsed = start_time.elapsed();
println!("took {}", elapsed.as_secs());
}

0 comments on commit f890a42

Please sign in to comment.