1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
// Crates
use rand::Rng;
use std::time::{Duration, Instant};
use tui::{
backend::TermionBackend,
layout::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
widgets::{Block, Borders, Paragraph, Span},
Terminal,
};
// Constants
const PARAGRAPH: &str = "The quick brown fox jumps over the lazy dog.";
// Enums
enum AppState {
Playing(GameState),
Stats(GameState, Duration),
Quit,
}
// Structs
struct GameState {
paragraph: String,
user_input: String,
start_time: Instant,
difficulty: Difficulty,
}
enum Difficulty {
Easy,
Medium,
Hard,
}
// Implementations
impl GameState {
fn new(difficulty: Difficulty) -> Self {
let mut rng = rand::thread_rng();
let paragraph = PARAGRAPH.to_owned();
let user_input = String::new();
let start_time = Instant::now();
Self {
paragraph,
user_input,
start_time,
difficulty,
}
}
fn wpm(&self) -> f64 {
// ...
}
fn accuracy(&self) -> f64 {
// ...
}
fn elapsed_time(&self) -> Duration {
// ...
}
fn advance(&mut self) -> bool {
// ...
}
fn check_end_condition(&self) -> bool {
// ...
}
}
// Functions
fn main() {
// ...
}
/*
* The following code defines a typing game in Rust.
*
* In main, the game loop is started by calling `run_game()`.
*
* The `GameState` struct represents the state of the game, including the difficulty level,
* the current paragraph to type, the player's progress, and various methods for manipulating
* and querying the game state. These methods include:
*
* - `new(difficulty: Difficulty)`: creates a new game state with the given difficulty.
* - `get_paragraph(difficulty: Difficulty)`: returns a random paragraph of the specified difficulty.
* - `wpm(&self)`: calculates the current words per minute of the player.
* - `accuracy(&self)`: calculates the current accuracy of the player.
* - `check_end_condition(&self)`: checks if the game has ended.
* - `advance(&mut self)`: advances the game to the next character.
* - `reset(&mut self)`: resets the game state to its initial state.
*
* The `Difficulty` enum represents the difficulty level of the game, and includes a method to create
* an enum variant from a string:
*
* - `from_str(s: &str) -> Result<Self, &'static str>`: creates a difficulty enum variant from a string.
*
* The `App` struct represents the application as a whole, and includes methods for handling input,
* updating the game state, setting the game difficulty, resetting the game, and setting the current
* application state. These methods include:
*
* - `new()`: creates a new `App` instance.
* - `handle_input(&mut self, input: Key)`: handles keyboard input.
* - `update(&mut self, elapsed: Duration)`: updates the game state based on the elapsed time.
* - `set_difficulty(&mut self, difficulty: Difficulty)`: sets the game difficulty.
* - `reset_game(&mut self)`: resets the game state to its initial state.
* - `set_state(&mut self, state: AppState)`: sets the current application state.
*
* The `AppState` enum represents the state of the application, including whether the game is being played,
* whether the player is viewing their stats, or whether the game is quitting. The enum variants include:
*
* - `Playing(GameState)`: represents the game state while the game is being played.
* - `Stats(GameState, Instant)`: represents the game state and start time while displaying stats.
* - `Quit`: represents the state where the game is quitting.
*/
|