Skip to content

Commit 96b6620

Browse files
committed
New individual guesses and guess logging feature. Version 2.2.0.
1 parent a5e292d commit 96b6620

File tree

7 files changed

+152
-4
lines changed

7 files changed

+152
-4
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@ View on [GitHub](https://github.com/preritdas/wordle) and [PyPI](https://pypi.or
55
----
66
## Updates
77

8+
### Version 2.2.0: Individual Guessing!
9+
10+
This package can now be properly used to power the backend of a Wordle game. Initially, the `.run()` method had to be used to play the game. It would open a console, request guesses as inputs, and print out responses. That's not a viable solution to power the backend of a package user's application or game.
11+
12+
The `Wordle` class has two new submethods: `.send_guess()` and `.reset_guesses()`.
13+
14+
Instantiate an object with the answer and game options (dictionary, etc.). Then, send individual guesses and the method will return a response.
15+
16+
```python
17+
import wordle
18+
19+
# Instantiate a game object
20+
game = wordle.Wordle(word = 'grape', real_words = True)
21+
22+
# Send your object a guess
23+
response = game.send_guess('adieu')
24+
# response = a d i e u
25+
26+
# Send an invalid guess
27+
response = game.send_guess('yabadabadoo')
28+
# response = "You can't guess a word with more than 5 letters."
29+
# Note: invalid guesses don't count towards the guess log.
30+
31+
# Send a 7th guess (only 6 allowed with log_guess = True)
32+
response = game.send_guess('seven')
33+
# response = "Out of guesses."
34+
```
35+
36+
More information on how to use this new feature is below.
37+
838
### Version 2.0.0
939

1040
A new function to get a random answer based on the day. You can deploy this Wordle and have it give you a random answer automatically every day! Instead of manually changing the answer, the program (can) automatically change its answer every day.
@@ -108,4 +138,10 @@ import wordle
108138
if __name__ == "__main__":
109139
print(wordle.Wordle.__doc__)
110140
print(wordle.Wordle.run.__doc__)
111-
```
141+
```
142+
143+
### Individual Guessing
144+
145+
This new feature was introduced in the Version 2.2.0 update at the top of the read-me. Here's some more usage information.
146+
147+
The `.send_guess()` method has two parameters, one of which is optional. `guess` is a string and is necessary. `log_guess` is a boolean and is `True` by default. Log guessing is simply the program's way of tracking how many times a _valid_ guess has been sent to the object. It will only allow 6 valid guesses to be sent to it before it starts returning `"Out of guesses."` automatically. To send a guess and return a response without adding to the guesses log, use `log_guess = False`.

dist/wordle-python-2.1.4.tar.gz

-35.4 KB
Binary file not shown.

dist/wordle-python-2.2.0.tar.gz

37.3 KB
Binary file not shown.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = "2.1.4"
3+
VERSION = "2.2.0"
44
DESCRIPTION = (
55
"The popular word game recreated in Python, deployable with custom answers."
66
)

test.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
import wordle
22

3-
wordle.Wordle(random_daily = True).run()
3+
game = wordle.Wordle(word = 'grape', real_words = True)
4+
5+
response = game.send_guess(guess = 'adieu')
6+
print(response)
7+
8+
response = game.send_guess(guess = 'adieu')
9+
print(response)
10+
11+
response = game.send_guess(guess = 'adieu')
12+
print(response)
13+
14+
response = game.send_guess(guess = 'adieu')
15+
print(response)
16+
17+
response = game.send_guess(guess = 'adieu')
18+
print(response)
19+
20+
response = game.send_guess(guess = 'adieu')
21+
print(response)
22+
23+
response = game.send_guess(guess = 'adieu')
24+
print(response)
25+
26+
# as there are seven sent attempts, the final one returns...
27+
#... "Out of guesses." as expected.

wordle/wordle.py

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ def __init__(self, word: str = 'hello', real_words: bool = True, random_daily: b
99
if random_daily == True:
1010
self.word = randomanswer.random_answer(daily=True).upper()
1111

12+
# Individual guesses
13+
self.individual_guesses = []
14+
1215
def run(self):
1316
"""Run the game. Depends on bool real_words from instantiation."""
1417

@@ -94,4 +97,89 @@ def run(self):
9497
raise Exception("Fatal iteration error for largest for loop.")
9598
quit()
9699
print("You failed.")
97-
quit()
100+
quit()
101+
102+
# Individual guesses
103+
def send_guess(self, guess: str, log_guess: bool = True):
104+
"""
105+
Send individual guesses. Returns a tuple where item one is the \
106+
string response, and item two is a boolean of whether or not the \
107+
puzzle was solved..
108+
Follows dictionary checking if that's enabled on object instantiation.
109+
If a guess is invalid (too many letters, etc.) the returned response \
110+
is simply an error message. No guess is logged.
111+
Use log_guess = False to send a guess without it counting towards \
112+
the 6 guess total.
113+
"""
114+
# For duplicate checking
115+
self.word_dup = list(self.word)
116+
failed_dictionary_test = False # by default
117+
118+
# Cheating checks
119+
120+
# real_words = True
121+
if self.real_words == True and guess.lower() not in dictionary.words:
122+
failed_dictionary_test = True
123+
124+
while (
125+
failed_dictionary_test == True
126+
or " " in guess
127+
or len(guess) > len(self.word)
128+
):
129+
if " " in guess:
130+
return "You can't have multiple words in your guess."
131+
elif len(guess) > len(self.word):
132+
return f"You can't guess a word with more than {len(list(self.word))} letters."
133+
elif failed_dictionary_test == True: # Not a real word
134+
return "That's not a real word."
135+
136+
# Correct failed dictionary test if real word is guessed
137+
if guess.lower() in dictionary.words:
138+
failed_dictionary_test = False
139+
140+
# prepare response list
141+
response = ['', '', '', '', '']
142+
143+
# first correctness check
144+
for j in range(len(guess)):
145+
if guess[j] in self.word_dup and guess[j] == self.word[j]:
146+
response[j] = f"*{guess[j]}* "
147+
self.word_dup.remove(guess[j]) # Duplicates
148+
149+
# next present and absent check
150+
for j in range(len(guess)):
151+
# already response skip
152+
if response[j] != "":
153+
continue
154+
# it's present(yellow)
155+
if guess[j] in self.word_dup:
156+
response[j] = guess[j] + " "
157+
self.word_dup.remove(guess[j]) # Duplicates
158+
# other absent
159+
else:
160+
response[j] = guess[j].lower() + " "
161+
162+
responseString = ""
163+
for letter in response:
164+
responseString += letter
165+
166+
if guess == self.word:
167+
guessed_correctly = True
168+
169+
# Guess Logging
170+
if log_guess:
171+
if len(self.individual_guesses) < 6:
172+
self.individual_guesses.append(guess)
173+
else:
174+
return "Out of guesses."
175+
176+
# Return the response
177+
if guessed_correctly:
178+
return responseString, True
179+
else:
180+
return responseString, False
181+
182+
# Reset individual guesses
183+
def reset_guesses(self):
184+
"""Removes all guesses from guess logging to allow 6 more attempts."""
185+
self.individual_guesses = []

0 commit comments

Comments
 (0)