-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
executable file
·179 lines (151 loc) · 4.68 KB
/
cli.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
#!/usr/bin/env python
import random
from enum import Enum
import inquirer # type: ignore
import algorithms.search
import algorithms.sort
import stats
class StatFilterEnum(Enum):
TEAM = "TEAM"
ALGORITHM = "ALGORITHM"
ALL = "ALL"
def stats_command():
stats_kind = inquirer.list_input(
message="Quelles stats ?",
choices=[
("Stats par équipe", StatFilterEnum.TEAM),
("Stats par algorithme", StatFilterEnum.ALGORITHM),
("Toutes les stats", StatFilterEnum.ALL),
],
)
filter = {}
if stats_kind == StatFilterEnum.TEAM:
all_teams = stats.all_teams()
team = inquirer.list_input(message="Quelle équipe ?", choices=all_teams)
filter["team"] = team
elif stats_kind == StatFilterEnum.ALGORITHM:
all_algorithms = stats.all_algorithms()
algorithm = inquirer.list_input(
message="Quel algorithme ?", choices=all_algorithms
)
filter["algorithm"] = algorithm
print(stats.all_stats(**filter))
def start_game_command(team):
questions = [
inquirer.Text(
"max",
message="Borne supérieure (minimum 100)",
default=100,
validate=lambda _, c: int(c) >= 100,
),
]
answers = inquirer.prompt(questions)
max = int(answers["max"])
game = algorithms.search.Game(max)
guessing_loop(game)
stat = stats.Stat(team=team, algorithm="Humain", n=max, result=game.guesses)
stats.insert_stat(stat)
text_for_outcome = {
-1: "Plus petit",
0: "Bingo!",
1: "Plus grand",
}
def guessing_loop(game: algorithms.search.Game):
while True:
answer = inquirer.text(
message=f"Essai {game.guesses}. Quel nombre proposez-vous ?"
)
try:
n = int(answer)
except ValueError:
print("'{answer}' n'est pas un nombre")
continue
outcome = game.guess(n)
print(text_for_outcome[outcome])
if outcome == 0:
break
CHOICES_OF_MAX = [
10,
100,
200,
300,
400,
500,
600,
700,
1_000,
1_500,
2_000,
3_000,
5_000,
10_000,
50_000,
100_000,
500_000,
1_000_000,
]
def run_search_algorithm_command(team):
algorithm_name = inquirer.list_input(
message="Algorithme ?", choices=algorithms.search.registry.keys()
)
max = inquirer.list_input(
message="Borne supérieure ?",
choices=[(f"{c:_}", c) for c in CHOICES_OF_MAX],
)
algorithm = algorithms.search.registry[algorithm_name]
game = algorithm.search.Game(max)
def oracle(n):
outcome = game.guess(n)
print(f"Essai {game.guesses:_}.\t Proposition {n:_}")
return outcome
algorithm(max=max, oracle=oracle)
print(
f"\nBorne supérieure: {game.max:_}. Secret {game.secret:_}. Trouvé en {game.guesses:_} essais.\n"
)
stat = stats.Stat(team=team, algorithm=algorithm_name, n=max, result=game.guesses)
stats.insert_stat(stat)
def run_sort_algorithm_command(team):
algorithm_name = inquirer.list_input(
message="Algorithme ?", choices=algorithms.sort.registry.keys()
)
list_size = inquirer.list_input(
message="Taille de la liste à trier ?",
choices=[(f"{c:_}", c) for c in CHOICES_OF_MAX],
)
algorithm = algorithms.sort.registry[algorithm_name]
lst = list(range(list_size))
random.shuffle(lst)
swaps = algorithm(lst)
print(
f"\nTri d'une liste de taille: {list_size:_} avec {algorithm_name} a demandé {swaps:_} échanges.\n"
)
stat = stats.Stat(team=team, algorithm=algorithm_name, n=list_size, result=swaps)
stats.insert_stat(stat)
def main():
team = inquirer.text(message="Votre nom d'équipe")
while True:
print()
operation = inquirer.list_input(
message="Que voulez-vous faire",
choices=[
("Nouvelle partie", "START_GAME"),
("Faire jouer un algorithme de recherche", "RUN_SEARCH_ALGORITHM"),
("Exécuter un algorithme de tri", "RUN_SORT_ALGORITHM"),
("Voir les statistiques", "SEE_STATS"),
("Quitter", "QUIT"),
],
)
if operation == "START_GAME":
start_game_command(team)
elif operation == "RUN_SEARCH_ALGORITHM":
run_search_algorithm_command(team)
elif operation == "RUN_SORT_ALGORITHM":
run_sort_algorithm_command(team)
elif operation == "SEE_STATS":
stats_command()
elif operation == "QUIT":
break
else:
print("Unknown operation", operation)
if __name__ == "__main__":
main()