-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
187 lines (173 loc) · 7.7 KB
/
test.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
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
#!/usr/bin/python
import requests
import time
import string
import re
URL = "http://upe.42069.fun/piL60"
seq = "etaoinshrdlcumwfgypbvkjxqz"
alphabet = [] #alphabet frequency list. -1 means the letter has been guessed already. Any number greater denotes how many times it shows up in possible words
file = open('file.txt', 'r')
for num in range (0, 25): #initialize alphabet frequency list
alphabet.append(0)
while True:
for num in range (0, 25): #reset frequency to 0 for new game
alphabet[num] = 0
level = 0 #reset level (initial state) to 0
gameOver = 0
i = 0
r = requests.get(url = URL)
while True:
data = r.json()
state = data["state"]
status = data["status"]
remaining_guesses = data["remaining_guesses"]
print("STATE:%s\nSTATUS:%s\nREMAINING GUESSES:%d\n"
%(state, status, remaining_guesses))
if status == ("DEAD") or status == ("FREE"):
rate = data["win_rate"]
games = data["games"]
lyrics = data["lyrics"]
print("WIN RATE:%f\n"
%(rate))
print("GAMES PLAYED:%f\n"
%(games))
print("LYRICS:%s\n"
%(lyrics))
break
if remaining_guesses == 2: #if the last guess was wrong, switch to level 1
if gameOver == 0:
level = 1
time.sleep(1)
if level == 0: #start with first letter not already guessed
while alphabet[ord(seq[i])-97] == -1:
i += 1
char = seq[i]
alphabet[ord(char)-97] = -1
i += 1
else: #determine all possible words
for num in range (0, 30):
if alphabet[num] >= 0:
alphabet[num] = 0
incomplete = 0
exp = "^"
for letter in state:
if letter == '_':
incomplete = 1
exp += "."
elif not letter.isalphabet():
if incomplete == 1:
exp += "$"
file = open('file.txt', 'r')
uncertain = 0
for line in file:
if re.search(r""+exp, line, re.IGNORECASE):
valid = 1
for let in line:
if alphabet[ord(let)-97] < 0:
check = 0
for baselet in exp:
if let == baselet:
check = 1
break
if check == 0:
valid = 0
break
if valid == 1:
uncertain += 1
file = open('file.txt', 'r')
for line in file:
if re.search(r""+exp, line, re.IGNORECASE):
valid = 1
for let in line:
if alphabet[ord(let)-97] < 0:
check = 0
for baselet in exp:
if let == baselet:
check = 1
break
if check == 0:
valid = 0
break
if valid == 0:
continue
for let in line:
if alphabet[ord(let)-97] >= 0: #add weights when less than 5 words are possible
if uncertain == 1:
alphabet[ord(let)-97] += 200
if uncertain == 2:
alphabet[ord(let)-97] += 100
if uncertain == 3:
alphabet[ord(let)-97] += 50
if uncertain == 4:
alphabet[ord(let)-97] += 25
alphabet[ord(let)-97] += 1
exp = "^"
incomplete = 0
else:
exp += letter
if incomplete == 1:
exp += "$"
file = open('file.txt', 'r')
uncertain = 0
for line in file:
if re.search(r""+exp, line, re.IGNORECASE):
valid = 1
for let in line:
if alphabet[ord(let)-97] < 0:
check = 0
for baselet in exp:
if let == baselet:
check = 1
break
if check == 0:
valid = 0
break
if valid == 1:
uncertain += 1
file = open('file.txt', 'r')
for line in file:
if re.search(r""+exp, line, re.IGNORECASE):
valid = 1
for let in line:
if alphabet[ord(let)-97] < 0:
check = 0
for baselet in exp:
if let == baselet:
check = 1
break
if check == 0:
valid = 0
break
if valid == 0:
continue
for let in line:
if alphabet[ord(let)-97] >= 0:
if uncertain == 1:
alphabet[ord(let)-97] += 200
if uncertain == 2:
alphabet[ord(let)-97] += 100
if uncertain == 3:
alphabet[ord(let)-97] += 50
if uncertain == 4:
alphabet[ord(let)-97] += 25
alphabet[ord(let)-97] += 1
maxWeight = 0
c = 0
for k in range(0,25):
if alphabet[k] > maxWeight: #go through list of frequencies and find highest one
maxWeight = alphabet[k]
c = k
if maxWeight == 0:
gameOver = 1
level = 0
while alphabet[ord(seq[i])-97] == -1:
i += 1
char = seq[i]
alphabet[ord(seq[i])-97] = -1
i += 1
alphabet[c] = -1 #mark guessed one as -1 in frequency list
char = chr( c + 97 )
print ("Guessed:", char)
data = {"guess" : char}
r = requests.post(url = URL, data = data) #submit guessed letter
time.sleep(2)