-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (75 loc) · 2.6 KB
/
Copy pathmain.py
File metadata and controls
92 lines (75 loc) · 2.6 KB
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
import sys
import random
from termcolor import colored
def guess_wordle(attempts, wordlist):
print("\n")
print("Enter your guess (", attempts, "remaining ): ")
guess = input()
length = len(guess)
while length != 5 or guess not in wordlist:
print("Please enter a valid guess: ")
guess = input()
length = len(guess)
guess = guess.upper()
return guess
def evaluate_attempt(guess, word):
result = [" "] * 5
# * < letter is correctly placed
# + < letter is in word, but incorrectly placed
# - < letter is not in word
word_a = list(word)
guess_a = list(guess)
for position, letter in enumerate(guess):
if letter == word[position]:
if letter in word:
result[position] = "*"
guess_a[position] = "0"
word_a.remove(letter)
for position, letter in enumerate(guess_a):
if letter != "0":
if letter in word_a:
result[position] = "+"
guess_a[position] = "0"
word_a.remove(letter)
else:
result[position] = "-"
for position, letter in enumerate(guess):
if result[position] == "-":
print(letter, end="")
elif result[position] == "+":
print(colored(letter, "yellow"), end="")
else:
print(colored(letter, 'green'), end="")
def game_loop(wordlist):
print("\n")
print("Selecting a random 5-letter word...")
word = random.choice(wordlist).upper()
attempts = 6
while attempts != 0:
guess = guess_wordle(attempts, wordlist)
evaluate_attempt(guess, word)
attempts -= 1
if guess == word:
attempts = 0
print("\n")
print("You guessed correctly!")
elif attempts == 0:
print("\n")
print("Out of guesses, game over!")
print("The word was", word)
print("Play again? Y/N")
replay = input().upper()
while (replay != "Y" and replay != "N"):
print("Please enter a valid option - Y or N")
replay = input().upper()
if replay == "N":
return False
print("Welcome to Dossy's Wordle!")
print("You have 6 attempts to guess the 5-letter word")
print("If a letter in your guess appears green, then it appears in the word and is in the correct position")
print("If a letter in your guess appears yellow, then it appears in the word but is in the wrong position")
wordlist = open("wordlist.txt", "r").read().split('\n')
while(1):
play = game_loop(wordlist)
if play == False:
break