-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
69 lines (53 loc) · 1.63 KB
/
server.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
from enum import Enum
class Color(Enum):
BLACK = 1
ORANGE = 2
GREEN = 3
@staticmethod
def from_char(char):
if char == 'b':
return Color.BLACK
if char == 'o':
return Color.ORANGE
if char == 'g':
return Color.GREEN
raise ValueError()
def __repr__(self):
return self.name[0]
def compute_result(actual_word, guess_word):
result = [Color.BLACK] * len(actual_word)
marked_actual = [c for c in actual_word]
for i, (guess, actual) in enumerate(zip(guess_word, actual_word)):
if guess == actual:
result[i] = Color.GREEN
marked_actual[i] = '_'
for i, guess in enumerate(guess_word):
if result[i] == Color.GREEN:
continue
try:
idx = marked_actual.index(guess)
marked_actual[idx] = '_'
result[i] = Color.ORANGE
except ValueError:
pass
return tuple(result)
class Server:
def __init__(self, word):
self.word = word
def guess(self, word_guess):
return compute_result(self.word, word_guess)
def prompt():
print(f"Enter 5-letter response to continue; b for BLACK/nomatch, g for GREEN/match, o for ORANGE/partial")
print(f"Example: bgbog")
class RemoteServer():
def guess(self, word_guess):
print(f"Try {word_guess}")
while True:
prompt()
text = input()
try:
result = tuple([Color.from_char(c) for c in text])
if len(result) == 5:
return result
except ValueError:
pass