-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle.py
105 lines (86 loc) · 3.22 KB
/
wordle.py
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
"""Module Contains Utility Functions."""
import random
from collections import Counter
from termcolor import colored
def secret_word(word_list) -> str:
"""Function choses a word from list."""
chosen_word = random.choice(word_list)
return chosen_word
def secret_dash(chosen_word) -> str:
"""Function prepares an empty list for guessing."""
dash = []
for _ in chosen_word:
dash.append("_")
return "".join(dash)
def valid_word_checker(input_guess, words_list) -> bool:
"""Function checks if a valid word is passed in."""
if input_guess.lower() in words_list:
valid_word = True
else:
valid_word = False
return valid_word
def compute_clue(guess_word, chosen_word) -> list[str]:
"""Function checks letters passed in against chosen word."""
dash = secret_dash(chosen_word)
clue = list(dash)
guess = guess_word
chosen = chosen_word
chosen_word_letter_count = Counter(chosen_word)
skip_index = [False] * len(guess_word)
for i in range(len(guess_word)):
if guess[i] == chosen[i]:
clue[i] = colored(guess[i].upper(), "green")
chosen_word_letter_count[guess[i]] -= 1
skip_index[i] = True
for i in range(len(guess_word)):
if skip_index[i]:
continue
elif guess[i] in chosen_word and chosen_word_letter_count[guess[i]] > 0:
clue[i] = colored(guess[i].upper(), "yellow")
chosen_word_letter_count[guess[i]] -= 1
else:
clue[i] = colored(guess[i].upper(), "red")
return clue
def main(words_list, seed_value):
"""Main Function."""
random.seed(seed_value)
play = True
total_count = 0
while play:
tries = 0
curr_count = 0
chosen_word = secret_word(words_list).casefold()
while tries < 6:
tries += 1
guess_word = input("Make a guess: ").casefold()
valid_word = valid_word_checker(guess_word, words_list)
if guess_word == chosen_word:
curr_count += 1
total_count += curr_count
print(colored(guess_word.upper(), "green"), sep="")
break
if len(guess_word) > 5:
print("Do Not Exceed 5 letters")
tries -= 1
elif not valid_word:
print("Word not in dictionary - try again...")
tries -= 1
else:
curr_count += 1
clue = compute_clue(guess_word, chosen_word)
print(*clue, sep="")
if guess_word == chosen_word:
print(
f"Congratulations, your wordle score for this game is {curr_count}\n"
+ f"Your overall score is {total_count}"
)
else:
total_count += 10
print(
f"Sorry, you did not guess the word: {chosen_word.upper()}\n"
+ f"Your overall score is {total_count}"
)
retry = input("Would you like to play again (Y or N)?\n")
if retry in ("n", "N"):
play = False
print("Thanks for playing!")