Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,975 Bytes
b5ce381 |
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
import random
from collections import Counter
class WordleGame:
# A more extensive word list with common 5-letter words
WORD_LIST = [
"apple",
"beach",
"crane",
"doubt",
"eagle",
"flame",
"grape",
"house",
"igloo",
"joker",
"knife",
"lemon",
"mango",
"night",
"ocean",
"piano",
"queen",
"river",
"stone",
"tiger",
"vivid",
"waste",
"yacht",
"zebra",
"about",
"above",
"actor",
"adapt",
"admit",
"adopt",
"after",
"again",
"album",
"alert",
"alike",
"alive",
"allow",
"alone",
"along",
"alter",
"among",
"anger",
"angle",
"angry",
"ankle",
"apart",
"apple",
"apply",
"arena",
"argue",
"arise",
"armor",
"array",
"arrow",
"asset",
"avoid",
"award",
"aware",
"awful",
"bacon",
"badge",
"badly",
"basic",
"basis",
"beach",
"beard",
"beast",
"begin",
"being",
"below",
"bench",
"birth",
"black",
"blade",
"blame",
"blank",
"blast",
"bleed",
"blend",
"bless",
]
def __init__(self):
"""Initialize the game state."""
self.target_word = None
self.guesses = []
self.feedbacks = []
self.game_over = True
self.won = False
self.max_attempts = 6
self.difficulty = "normal" # Can be "easy", "normal", or "hard"
def new_game(self, difficulty="normal"):
"""Start a new game by resetting state and picking a random word."""
self.difficulty = difficulty
self.target_word = random.choice(self.WORD_LIST)
self.guesses = []
self.feedbacks = []
self.game_over = False
self.won = False
# Adjust max attempts based on difficulty
if difficulty == "easy":
self.max_attempts = 8
elif difficulty == "hard":
self.max_attempts = 4
else: # normal
self.max_attempts = 6
return f"New game started ({self.difficulty} mode). Guess a five-letter word. You have {self.max_attempts} attempts."
def generate_feedback(self, guess):
"""Generate HTML feedback for a guess (green, yellow, gray)."""
target_count = Counter(self.target_word)
feedback = [""] * 5
# Mark correct letters in correct positions (green)
for i in range(5):
if guess[i] == self.target_word[i]:
feedback[i] = (
f'<span style="color: green; font-weight: bold">{guess[i].upper()}</span>'
)
target_count[guess[i]] -= 1
# Mark letters in wrong positions (yellow) or not in word (gray)
for i in range(5):
if feedback[i] == "":
if guess[i] in target_count and target_count[guess[i]] > 0:
feedback[i] = (
f'<span style="color: orange; font-weight: bold">{guess[i].upper()}</span>'
)
target_count[guess[i]] -= 1
else:
feedback[i] = f'<span style="color: gray">{guess[i].upper()}</span>'
return "".join(feedback)
def submit_guess(self, guess):
"""Process a player's guess and update game state."""
if self.game_over:
return "Please start a new game."
if len(guess) != 5:
return "Please enter a five-letter word."
guess = guess.lower()
# Check if the guess is a valid word (optional validation)
if self.difficulty == "hard" and guess not in self.WORD_LIST:
return "Not in word list. Try again."
feedback = self.generate_feedback(guess)
self.guesses.append(guess)
self.feedbacks.append(feedback)
if guess == self.target_word:
self.game_over = True
self.won = True
attempts = len(self.guesses)
message = f"Congratulations! You guessed the word in {attempts}/{'attempt' if attempts == 1 else 'attempts'}."
elif len(self.guesses) >= self.max_attempts:
self.game_over = True
message = f"Game over. The word was {self.target_word.upper()}."
else:
message = f"You have {self.max_attempts - len(self.guesses)} guesses left."
# Give hints in easy mode
if self.difficulty == "easy" and len(self.guesses) >= 3:
# Find a letter position that hasn't been guessed correctly yet
for i in range(5):
if all(g[i] != self.target_word[i] for g in self.guesses):
message += (
f" Hint: Letter {i + 1} is '{self.target_word[i].upper()}'."
)
break
return message
def get_feedback_history(self):
"""Return the history of guesses and feedbacks as an HTML string."""
if not self.feedbacks:
return "No guesses yet."
history = []
for i, fb in enumerate(self.feedbacks):
history.append(f"Guess {i + 1}/{self.max_attempts}: {fb}")
return "<br>".join(history)
def get_game_stats(self):
"""Return current game statistics."""
return {
"target_word": self.target_word if self.game_over else "???",
"guesses_made": len(self.guesses),
"max_attempts": self.max_attempts,
"won": self.won,
"game_over": self.game_over,
"difficulty": self.difficulty,
}
|