This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_test.py
132 lines (97 loc) · 3.93 KB
/
state_test.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
import unittest
from house import House
from state import State, Player, Role, Partition
from communicator import Communicator
PLAYERS = [
(0,0,"a", "villager"),
(1,1,"b", "villager"),
(2,2,"c", "villager"),
(3,3,"d", "villager"),
(4,4,"e", "villager"),
(5,5,"f", "villager"),
(6,6,"g", "villager"),
(7,7,"h", "vampire"),
(8,8,"i", "vampire"),
(9,9,"j", "doctor"),
]
def tuple_to_player(player: tuple) -> dict:
return {
"ip": player[0],
"id": player[1],
"name": player[2],
"role": player[3],
"key": ""
}
class TestState(unittest.TestCase):
def setUp(self):
self.state = State([Player(tuple_to_player(player)) for player in PLAYERS])
def test_kill(self):
self.state.change_state(Partition.night)
self.state.kill(self.state.players[0])
self.state.kill(self.state.players[1])
self.state.change_state(Partition.day)
self.state.kill(self.state.players[2])
self.assertFalse(self.state.alive[self.state.players[0]])
self.assertFalse(self.state.alive[self.state.players[1]])
self.assertTrue(self.state.alive[self.state.players[2]])
def test_protect(self):
self.state.protect(self.state.players[0])
self.state.change_state(Partition.night)
self.state.kill(self.state.players[0])
self.assertTrue(self.state.alive[self.state.players[0]])
self.assertTrue(self.state.saved[self.state.players[0]])
def test_is_over_vampire(self):
# game is not over in this round
self.state.change_state(Partition.night)
self.state.kill(self.state.players[0])
self.state.kill(self.state.players[1])
self.state.change_state(Partition.day)
self.state.kill(self.state.players[2])
is_over, side = self.state.is_over()
self.assertFalse(is_over)
self.assertTrue(side == None)
# game is over in this round
self.state.change_state(Partition.night)
# kill all villagers except remain one
for i in range(2, 6):
self.state.kill(self.state.players[i])
self.state.change_state(Partition.day)
is_over, side = self.state.is_over()
self.assertTrue(is_over)
self.assertTrue(side == Role.vampire)
def test_is_over_villager(self):
# game is not over in this round
self.state.change_state(Partition.night)
self.state.kill(self.state.players[0])
self.state.kill(self.state.players[1])
self.state.change_state(Partition.day)
self.state.kill(self.state.players[2])
is_over, side = self.state.is_over()
self.assertFalse(is_over)
self.assertTrue(side == None)
# game is over in this round
self.state.change_state(Partition.night)
# kill all vampires except remain none
for i in range(7, 9):
self.state.kill(self.state.players[i])
self.state.change_state(Partition.day)
is_over, side = self.state.is_over()
self.assertTrue(is_over)
self.assertTrue(side == Role.villager)
def test_cleanup(self):
self.state.protect(self.state.players[0])
self.state.change_state(Partition.night)
self.state.kill(self.state.players[0])
self.state.kill(self.state.players[1])
self.state.kill(self.state.players[2])
self.state.change_state(Partition.voting)
dump = self.state.dump_delta()
self.state.round_cleanup()
for i in range(3, 10):
self.assertTrue(self.state.alive[self.state.players[i]])
self.assertTrue(dump["saved"] == [self.state.players[0]])
self.assertTrue(dump["killed"] == [self.state.players[1], self.state.players[2]])
self.assertTrue(len(self.state.saved) == 1)
self.assertTrue(len(self.state.killed) == 2)
if __name__ == '__main__':
unittest.main()