-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayAndSolve.py
More file actions
105 lines (98 loc) · 3.88 KB
/
Copy pathplayAndSolve.py
File metadata and controls
105 lines (98 loc) · 3.88 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import random
word_list = []
with open("word_list/all_answers.txt", "r") as wordFile:
for wordAnswer in wordFile.readlines():
word_list.append(wordAnswer[:5])
is_in_word = []
is_in_position = {}
wrong_position = {}
not_in_word = []
curr_words = []
winner = random.choice(word_list)
# print(f"Winner is {winner}")
has_won = False
number_of_tries = 0
while len(is_in_position) < 5:
while not has_won:
number_of_tries += 1
if word_list:
# user_choice = random.choice(word_list)
user_choice = input("Enter word: ")
# print(f"Computer chose: {user_choice}")
else:
has_won = True
break
attempt = user_choice
if attempt == winner:
has_won = True
clue = ["1", "1", "1", "1", "1"]
win_hold = winner
for i in range(5):
if attempt[i] == winner[i]:
clue[i] = "3"
first_char = winner.find(attempt[i])
winner = winner[:first_char] + ' ' + winner[first_char + 1:]
for i in range(5):
if attempt[i] in winner and clue[i] == "1":
clue[i] = "2"
first_char = winner.find(attempt[i])
winner = winner[:first_char] + ' ' + winner[first_char + 1:]
clueFin = ""
winner = win_hold
for curclue in clue:
clueFin += curclue
user_choice_positions = clueFin
print(user_choice_positions)
for posi, num in enumerate(user_choice_positions):
match num:
case "3":
if user_choice[posi] not in is_in_word:
is_in_word.append(user_choice[posi])
is_in_position[posi] = user_choice[posi]
if user_choice[posi] in not_in_word:
not_in_word.remove(user_choice[posi])
for posi, num in enumerate(user_choice_positions):
match num:
case "2":
if user_choice[posi] not in is_in_word:
is_in_word.append(user_choice[posi])
if user_choice[posi] in not_in_word:
not_in_word.remove(user_choice[posi])
wrong_position[posi] = user_choice[posi]
for posi, num in enumerate(user_choice_positions):
match num:
case "1":
if user_choice[posi] in is_in_word:
wrong_position[posi] = user_choice[posi]
elif user_choice[posi] not in not_in_word:
not_in_word.append(user_choice[posi])
for word in word_list:
if user_choice_positions == "11111":
curr_words.append(word)
elif user_choice == word and user_choice_positions != "33333":
if word in curr_words:
curr_words.remove(word)
else:
for letter in is_in_word:
if letter in word:
if word not in curr_words:
curr_words.append(word)
for pos_in in is_in_position:
if word[pos_in] != is_in_position[pos_in]:
if word in curr_words:
curr_words.remove(word)
for pos_out in wrong_position:
if word[int(pos_out)] == wrong_position[int(pos_out)]:
if word in curr_words:
curr_words.remove(word)
for not_in_letter in not_in_word:
if not_in_letter in word:
if word in curr_words:
curr_words.remove(word)
for allets in is_in_word:
if allets not in word:
if word in curr_words:
curr_words.remove(word)
word_list = curr_words
curr_words = []
print(number_of_tries)