|
1 | 1 | import random
|
2 | 2 | import time
|
| 3 | +import sys |
| 4 | +import os |
| 5 | +from typing import List, Dict |
3 | 6 |
|
4 |
| -# Word lists for different difficulty levels |
5 |
| -easy_words = ['cat', 'dog', 'sun', 'book', 'tree', 'car', 'bird'] |
6 |
| -medium_words = ['elephant', 'giraffe', 'balloon', 'umbrella', 'vacation'] |
7 |
| -hard_words = ['substitution', 'enlightenment', 'interrogation', 'psychological', 'astronomy'] |
| 7 | +class TypingGame: |
| 8 | + def __init__(self): |
| 9 | + self.words: Dict[str, List[str]] = { |
| 10 | + 'Easy': [ |
| 11 | + 'cat', 'dog', 'run', 'jump', 'book', 'desk', 'lamp', 'fish', |
| 12 | + 'bird', 'tree', 'house', 'door', 'chair', 'table' |
| 13 | + ], |
| 14 | + 'Medium': [ |
| 15 | + 'elephant', 'giraffe', 'computer', 'keyboard', 'mountain', |
| 16 | + 'butterfly', 'telephone', 'umbrella', 'calendar', 'dictionary' |
| 17 | + ], |
| 18 | + 'Hard': [ |
| 19 | + 'extraordinary', 'sophisticated', 'revolutionary', 'parliamentary', |
| 20 | + 'congratulations', 'archaeological', 'meteorological', |
| 21 | + 'philosophical', 'unprecedented', 'entrepreneurial' |
| 22 | + ] |
| 23 | + } |
| 24 | + self.score = 0 |
| 25 | + self.round = 1 |
| 26 | + self.total_rounds = 10 |
| 27 | + self.accuracy_stats = [] |
| 28 | + self.time_stats = [] |
8 | 29 |
|
9 |
| -# Function to select a random word based on difficulty |
10 |
| -def generate_word(level): |
11 |
| - if level == 'Easy': |
12 |
| - return random.choice(easy_words) |
13 |
| - elif level == 'Medium': |
14 |
| - return random.choice(medium_words) |
15 |
| - elif level == 'Hard': |
16 |
| - return random.choice(hard_words) |
| 30 | + def clear_screen(self): |
| 31 | + os.system('cls' if os.name == 'nt' else 'clear') |
17 | 32 |
|
18 |
| -# Function to adjust difficulty based on score |
19 |
| -def adjust_difficulty(score): |
20 |
| - if score >= 50 and score < 100: |
21 |
| - return 'Medium' |
22 |
| - elif score >= 100: |
23 |
| - return 'Hard' |
24 |
| - else: |
25 |
| - return 'Easy' |
| 33 | + def get_difficulty(self) -> str: |
| 34 | + if self.score < 50: |
| 35 | + return "Easy" |
| 36 | + elif self.score < 100: |
| 37 | + return "Medium" |
| 38 | + else: |
| 39 | + return "Hard" |
26 | 40 |
|
27 |
| -# Function to run the typing game |
28 |
| -def start_game(): |
29 |
| - score = 0 |
30 |
| - level = 'Easy' |
31 |
| - |
32 |
| - print("Welcome to the AI-Powered Typing Game!\n") |
33 |
| - print("Instructions:") |
34 |
| - print("Type the given word correctly to score points.") |
35 |
| - print("Difficulty will increase as your score increases.\n") |
36 |
| - |
37 |
| - # Main game loop |
38 |
| - for round_num in range(10): # Number of rounds (10 in this example) |
39 |
| - print(f"\nRound {round_num + 1}: Difficulty Level - {level}") |
40 |
| - word_to_type = generate_word(level) |
41 |
| - print(f"Type this word: {word_to_type}") |
42 |
| - |
43 |
| - start_time = time.time() # Start the timer |
44 |
| - user_input = input("Your input: ") |
| 41 | + def get_word(self, difficulty: str) -> str: |
| 42 | + return random.choice(self.words[difficulty]) |
| 43 | + |
| 44 | + def calculate_wpm(self, time_taken: float, word_length: int) -> float: |
| 45 | + # Calculate words per minute (WPM) |
| 46 | + characters_per_word = 5 # Standard measure |
| 47 | + words = word_length / characters_per_word |
| 48 | + minutes = time_taken / 60 |
| 49 | + return words / minutes if minutes > 0 else 0 |
| 50 | + |
| 51 | + def display_stats(self): |
| 52 | + if not self.accuracy_stats: |
| 53 | + return |
45 | 54 |
|
46 |
| - # Check if the user typed the correct word |
47 |
| - if user_input.lower() == word_to_type.lower(): |
48 |
| - time_taken = time.time() - start_time |
49 |
| - score += 10 # Increase score for correct input |
50 |
| - print(f"Correct! You took {time_taken:.2f} seconds.") |
51 |
| - print(f"Your score: {score}") |
| 55 | + avg_accuracy = sum(self.accuracy_stats) / len(self.accuracy_stats) |
| 56 | + avg_time = sum(self.time_stats) / len(self.time_stats) |
| 57 | + avg_wpm = sum(wpm for _, wpm in self.time_stats) / len(self.time_stats) |
| 58 | + |
| 59 | + print("\n=== Game Statistics ===") |
| 60 | + print(f"Average Accuracy: {avg_accuracy:.2f}%") |
| 61 | + print(f"Average Time per Word: {avg_time:.2f} seconds") |
| 62 | + print(f"Average WPM: {avg_wpm:.2f}") |
| 63 | + |
| 64 | + def get_feedback(self) -> str: |
| 65 | + if self.score >= 100: |
| 66 | + return "🏆 Typing master! You're absolutely amazing!" |
| 67 | + elif self.score >= 50: |
| 68 | + return "👍 Good job! You're making great progress!" |
52 | 69 | else:
|
53 |
| - print("Incorrect! Try harder next time.") |
54 |
| - |
55 |
| - # Adjust the difficulty based on score |
56 |
| - level = adjust_difficulty(score) |
57 |
| - |
58 |
| - print("\nGame Over!") |
59 |
| - print(f"Your final score: {score}") |
60 |
| - if score >= 100: |
61 |
| - print("You're a typing master!") |
62 |
| - elif score >= 50: |
63 |
| - print("Good job! Keep practicing!") |
64 |
| - else: |
65 |
| - print("Keep trying! You'll get better.") |
| 70 | + return "💪 Keep practicing! You'll get better with time!" |
| 71 | + |
| 72 | + def display_progress_bar(self): |
| 73 | + progress = (self.round - 1) / self.total_rounds |
| 74 | + bar_length = 30 |
| 75 | + filled = int(bar_length * progress) |
| 76 | + bar = '█' * filled + '░' * (bar_length - filled) |
| 77 | + print(f"\nProgress: [{bar}] {progress*100:.1f}%") |
| 78 | + |
| 79 | + def run_game(self): |
| 80 | + self.clear_screen() |
| 81 | + print("╔══════════════════════════════════════╗") |
| 82 | + print("║ AI-Powered Typing Game v2.0 ║") |
| 83 | + print("╚══════════════════════════════════════╝") |
| 84 | + print("\nInstructions: Type the given word correctly to score points.") |
| 85 | + print("The difficulty increases as your score improves.") |
| 86 | + input("\nPress Enter to start...") |
| 87 | + |
| 88 | + while self.round <= self.total_rounds: |
| 89 | + self.clear_screen() |
| 90 | + difficulty = self.get_difficulty() |
| 91 | + word = self.get_word(difficulty) |
| 92 | + |
| 93 | + self.display_progress_bar() |
| 94 | + print(f"\nRound {self.round}/{self.total_rounds}") |
| 95 | + print(f"Difficulty Level: {difficulty}") |
| 96 | + print(f"Current Score: {self.score}") |
| 97 | + print(f"\nType this word: {word}") |
| 98 | + |
| 99 | + start_time = time.time() |
| 100 | + try: |
| 101 | + user_input = input("Your input: ").strip() |
| 102 | + except KeyboardInterrupt: |
| 103 | + print("\nGame terminated by user.") |
| 104 | + sys.exit() |
| 105 | + |
| 106 | + elapsed_time = time.time() - start_time |
| 107 | + wpm = self.calculate_wpm(elapsed_time, len(word)) |
| 108 | + |
| 109 | + # Calculate accuracy |
| 110 | + accuracy = sum(a == b for a, b in zip(word.lower(), user_input.lower())) |
| 111 | + accuracy = (accuracy / len(word)) * 100 if word else 0 |
| 112 | + |
| 113 | + if user_input.lower() == word.lower(): |
| 114 | + self.score += 10 |
| 115 | + print(f"\n✨ Correct! ✨") |
| 116 | + print(f"Time: {elapsed_time:.2f} seconds") |
| 117 | + print(f"WPM: {wpm:.2f}") |
| 118 | + print(f"Accuracy: {accuracy:.2f}%") |
| 119 | + else: |
| 120 | + print(f"\n❌ Incorrect! The word was: {word}") |
| 121 | + print(f"Accuracy: {accuracy:.2f}%") |
| 122 | + |
| 123 | + self.accuracy_stats.append(accuracy) |
| 124 | + self.time_stats.append((elapsed_time, wpm)) |
| 125 | + |
| 126 | + self.round += 1 |
| 127 | + input("\nPress Enter to continue...") |
| 128 | + |
| 129 | + self.clear_screen() |
| 130 | + print("\n🎮 Game Over! 🎮") |
| 131 | + print(f"Final Score: {self.score}") |
| 132 | + print(self.get_feedback()) |
| 133 | + self.display_stats() |
66 | 134 |
|
67 |
| -# Run the game |
68 |
| -start_game() |
| 135 | +if __name__ == "__main__": |
| 136 | + game = TypingGame() |
| 137 | + game.run_game() |
0 commit comments