aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs118
1 files changed, 117 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..b5a5760 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,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() {
- println!("Hello, world!");
+ // ...
}
+
+/*
+ * 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.
+ */