-
Notifications
You must be signed in to change notification settings - Fork 0
/
displays.py
59 lines (50 loc) · 2.33 KB
/
displays.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
import time
class SummaryDisplay(object):
def __init__(self):
super(SummaryDisplay, self).__init__()
self.scores = []
self.highest_tile = []
self.game_durations = []
self.game_start_time = None
def initialize(self, initial_state):
self.game_start_time = time.time()
def update_state(self, new_state, action, opponent_action):
if new_state.done:
game_end_time = time.time()
game_duration = round(game_end_time - self.game_start_time, 3)
print("score: %s\nhighest tile: %s\ngame_duration: %s" % (new_state.score, new_state.board.max(),
game_duration))
self.scores.append(new_state.score)
self.highest_tile.append(new_state.board.max())
self.game_durations.append(game_duration)
def mainloop_iteration(self):
pass
def print_stats(self):
win_rate = len(list(filter(lambda x: x >= 2048, self.highest_tile))) / len(self.highest_tile)
counts = dict()
for i in self.highest_tile:
counts[i] = counts.get(i, 0) + 1
print("="*30)
print("scores: %s" % self.scores)
print("highest tile: %s" % self.highest_tile)
print("game_durations: %s" % self.game_durations)
print("avg score: %s" % round((sum(self.scores) / len(self.scores))))
print("cnt highest tile: %s" % counts)
print("avg game duration: %s" % round((sum(self.game_durations) / len(self.game_durations)), 3))
print("win rate: %s" % win_rate)
# print("=" * 30)
def __repr__(self):
win_rate = len(list(filter(lambda x: x >= 2048, self.highest_tile))) / len(self.highest_tile)
counts = dict()
for i in self.highest_tile:
counts[i] = counts.get(i, 0) + 1
return f"scores: {self.scores}" \
f"highest tile: {self.highest_tile}" \
f"game_durations: {self.game_durations}" \
f"avg score: {round((sum(self.scores) / len(self.scores)))}" \
f"cnt highest tile: {counts}" \
f"avg game duration: {round((sum(self.game_durations) / len(self.game_durations)), 3)}" \
f"win rate: {win_rate}" \
"=" * 30
def __str__(self):
return self.__repr__()