-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_simulation.py
69 lines (54 loc) · 1.66 KB
/
game_simulation.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
# April 2022
# Implements the simulated logic of the game.
import random
from game import CoinGame
class CoinGameSimulation(CoinGame):
def __init__(self):
super().__init__()
self.opponent = Opponent()
def reset_game(self):
self.score = 0
self.flips_left = self.start_flips
self.new_opponent()
self.done = False
def new_opponent(self):
self.opponent = Opponent()
self.heads = 0
self.tails = 0
def observe(self) -> tuple:
return self.heads, self.tails, self.flips_left
def flip_one_coin(self):
if self.flips_left > 0:
if self.opponent.flip():
self.heads += 1
else:
self.tails += 1
self.flips_left -= 1
def flip_five_coins(self):
for _ in range(5):
self.flip_one_coin()
def label_fair(self):
self._label(0)
def label_cheater(self):
self._label(1)
def _label(self, label: int):
if label == self.opponent.ground_truth_label:
self.score += 1
self.flips_left += self.correct_label_bonus
else:
self.flips_left += self.incorrect_label_penalty
self.new_opponent()
if self.flips_left < 0:
self.done = True
class Opponent:
def __init__(self) -> None:
# 0: fair, 1: cheater
if random.random() > 0.5:
self.p = 0.75
self.ground_truth_label = 1
else:
self.p = 0.5
self.ground_truth_label = 0
def flip(self) -> bool:
# True: heads, False: tails
return random.random() < self.p