Skip to content

Commit

Permalink
display previous game statistics
Browse files Browse the repository at this point in the history
tested locally

C:/Users/kushal/RustroverProjects/helloworld/target/debug/helloworld.exe
Guess the number!
Remember, you can update your consent by running this application with the --update-consent flag.
Please input your guess.
7
You guessed: 7
Too small!
The sum of your guess and the secret number is: 40
The product of your guess and the secret number is: 231
The greatest common divisor of your guess and the secret number is: 1
Please input your guess.
30
You guessed: 30
Too small!
The sum of your guess and the secret number is: 63
The product of your guess and the secret number is: 990
The greatest common divisor of your guess and the secret number is: 3
Please input your guess.
31
You guessed: 31
Too small!
The sum of your guess and the secret number is: 64
The product of your guess and the secret number is: 1023
The greatest common divisor of your guess and the secret number is: 1
Please input your guess.
32
You guessed: 32
Too small!
The sum of your guess and the secret number is: 65
The product of your guess and the secret number is: 1056
The greatest common divisor of your guess and the secret number is: 1
Please input your guess.
33
You guessed: 33
You win!
Game Statistics:
Attempts: [7, 30, 31, 32, 33]
Secret Number: 33
Guesses: [7, 30, 31, 32, 33]
All Games History:
Game 1: Attempts: [12, 28], Secret Number: 28, Guesses: [12, 28]
Game 2: Attempts: [1, 17, 18], Secret Number: 18, Guesses: [1, 17, 18]
Game 3: Attempts: [3, 33, 32, 31, 30, 29], Secret Number: 29, Guesses: [3, 33, 32, 31, 30, 29]
Game 4: Attempts: [7, 30, 31, 32, 33], Secret Number: 33, Guesses: [7, 30, 31, 32, 33]
Press Enter to exit...
  • Loading branch information
9034725985 committed Aug 8, 2024
1 parent 9ee7abc commit f045093
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ fn play_guessing_game(analytics_consent: bool) -> Result<(), GameError> {

save_game_stats(&game_stats)?;

// Display the entire game history
let game_history = read_game_history()?;
println!("All Games History:");
for (index, game) in game_history.games.iter().enumerate() {
println!("Game {}: Attempts: {:?}, Secret Number: {}, Guesses: {:?}", index + 1, game.attempts, game.secret_number, game.guesses);
}

println!("Press Enter to exit...");
let mut exit = String::new();
io::stdin().read_line(&mut exit).expect("Failed to read line");
Expand Down Expand Up @@ -161,3 +168,21 @@ fn fetch_hello_world() -> Result<(), GameError> {
}
Ok(())
}

fn read_game_history() -> Result<GameHistory, GameError> {
let strategy = choose_app_strategy(AppStrategyArgs {
top_level_domain: "org".to_string(),
author: "Kushal Hada".to_string(),
app_name: "KusGuessingGame".to_string(),
}).unwrap();

let stats_path = strategy.data_dir().join("game_stats.json");

if stats_path.exists() {
let stats_data = fs::read_to_string(&stats_path)?;
let game_history: GameHistory = serde_json::from_str(&stats_data)?;
Ok(game_history)
} else {
Ok(GameHistory { games: Vec::new() })
}
}

0 comments on commit f045093

Please sign in to comment.