-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
119 lines (100 loc) · 3.79 KB
/
play.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
from argparse import ArgumentParser
from termcolor import colored
from itertools import count, compress
# ###############################################
from src.game_config import GameConfig
from src.algorithm import Algorithm
from src.game import Game
from src.utils.symbols import Symbols
from src.utils.custom_input import CustomInput
# ###############################################
def grid_colormap(level: float) -> str:
if level >= 0.66: return 'green'
if level >= 0.33: return 'yellow'
if level >= 0.01: return 'blue'
return 'grey'
# ###############################################
def show_grid(symbols: Symbols, algo: Algorithm,
prefix='', suffix='', short=True):
grid = algo.summary_grid()
if short:
used = grid.sum(1) > 0
grid = grid[used]
symbols = compress(symbols, used)
grid = grid / grid.sum(0) # normalise columns
for symbol, row in zip(symbols, grid):
print('%s%s%s' % (
prefix,
''.join([
colored(symbol, grid_colormap(col))
for col in row
]),
suffix
))
# ###############################################
def play_one_game(game_id: int,
symbols: Symbols, game: Game,
algo: Algorithm, input: CustomInput) -> bool:
game.new()
algo.reset(game)
print('#' * 50)
print('New Game (%d) !' % game_id)
print('Write your guess as a string of %d symbols in %s with no spaces' % (
game.cfg.code_length, symbols))
print('> Ctrl+C or "exit" to show solution and exit')
print('> "help" for the computer to play something for you')
print('> "grid" to show an helping table')
while game:
guess_sym = input()
if guess_sym == 'exit':
break
elif guess_sym == 'grid':
show_grid(symbols, algo,
colored(' ' * 15 + '|', 'yellow'),
colored('|', 'yellow'))
continue
elif guess_sym == 'help':
guess = algo.next_guess()
helped = True
else:
guess = symbols.from_sym(guess_sym)
helped = False
step = game.guess(guess)
algo.update(step)
color = 'yellow' if helped else 'blue'
print('Step %2d, guess: %s, %s, %s, %s' % (
step.id,
colored(symbols.to_sym(step.guess.value), color),
colored('%d well placed' % step.hash.wp, 'green'),
colored('%3d ill-placed' % step.hash.ip, 'yellow'),
'%d remaining hypothesis' % len(algo.C)
))
if not game:
print(colored('You won in %d steps' % len(game), 'green'))
return True
else:
print(colored('Solution was: %s' % game.state.solution, 'yellow'))
return False
# ###############################################
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('-s', '--symbols', type=str, default='123456')
parser.add_argument('-l', '--code_length', type=int, default=4)
parser.add_argument('--no_doublon', action='store_true')
parser.add_argument('-n', '--nb_games', type=int, default=None)
args = parser.parse_args()
# ########################################
symbols = Symbols(args.symbols)
config = GameConfig(
code_length = args.code_length,
symbol_count = len(symbols),
doublons = not args.no_doublon
)
game = Game(config)
algo = Algorithm()
input = CustomInput(config, symbols, {'help', 'grid'}, 'exit')
# ########################################
for gid in range(args.nb_games) if args.nb_games else count():
keep_playing = play_one_game(gid + 1, symbols, game, algo, input)
if not keep_playing: break
# ########################################