-
Notifications
You must be signed in to change notification settings - Fork 1
/
KnuhnTests.py
168 lines (121 loc) · 5.29 KB
/
KnuhnTests.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
import unittest
from KuhnPoker import KuhnPoker, Players, Moves, Results
class TestStringMethods(unittest.TestCase):
def CreatePokerEngine(self):
kn = KuhnPoker()
kn.NewRound()
return kn
def test_one(self):
kn = self.CreatePokerEngine()
kn.MakeMove(Moves.pas)
self.assertTrue(kn.moves[0] == Moves.pas.value)
self.assertTrue(not kn.IsTerminateState())
kn.MakeMove(Moves.pas)
self.assertTrue(kn.moves[1] == Moves.pas.value)
self.assertTrue(kn.IsTerminateState())
kn.cards = [1, 2, 3]
self.assertTrue(kn.GetPayoff(player=Players.one) == -1)
kn.cards = [2, 1, 3]
self.assertTrue(kn.GetPayoff(player=Players.one) == 1)
kn.cards = [3, 2, 1]
self.assertTrue(kn.GetPayoff(player=Players.one) == 1)
kn.cards = [2, 3, 1]
self.assertTrue(kn.GetPayoff(player=Players.one) == -1)
self.assertTrue(kn.GetPayoff(player=Players.two) == 1)
def test_two(self):
kn = self.CreatePokerEngine()
kn.MakeMove(Moves.pas)
self.assertTrue(not kn.IsTerminateState())
kn.MakeMove(Moves.bet)
self.assertTrue(not kn.IsTerminateState())
kn.MakeMove(Moves.pas)
self.assertTrue(kn.IsTerminateState())
kn.cards = [1, 2, 3]
self.assertTrue(kn.GetPayoff(player=Players.two) == 1)
kn.cards = [2, 1, 3]
self.assertTrue(kn.GetPayoff(player=Players.one) == -1)
def test_three(self):
kn = self.CreatePokerEngine()
kn.MakeMove(Moves.pas)
self.assertTrue(not kn.IsTerminateState())
kn.MakeMove(Moves.bet)
self.assertTrue(not kn.IsTerminateState())
kn.MakeMove(Moves.bet)
self.assertTrue(kn.IsTerminateState())
kn.cards = [1, 2, 3]
self.assertTrue(kn.GetPayoff(player=Players.one) == -2)
kn.cards = [2, 1, 3]
self.assertTrue(kn.GetPayoff(player=Players.one) == 2)
def test_four(self):
kn = self.CreatePokerEngine()
kn.MakeMove(Moves.bet)
self.assertTrue(not kn.IsTerminateState())
kn.MakeMove(Moves.pas)
self.assertTrue(kn.IsTerminateState())
kn.cards = [1, 2, 3]
self.assertTrue(kn.GetPayoff(player=Players.one) == 1)
kn.cards = [2, 1, 3]
self.assertTrue(kn.GetPayoff(player=Players.one) == 1)
def test_five(self):
kn = self.CreatePokerEngine()
kn.MakeMove(Moves.bet)
self.assertTrue(not kn.IsTerminateState())
kn.MakeMove(Moves.bet)
self.assertTrue(kn.IsTerminateState())
kn.cards = [1, 2, 3]
self.assertTrue(kn.GetPayoff(player=Players.one) == -2)
kn.cards = [2, 1, 3]
self.assertTrue(kn.GetPayoff(player=Players.one) == 2)
def test_infoset(self):
kn = self.CreatePokerEngine()
infoset = kn.GetInfoset(Players.one)
prefix1 = str(kn.cards[0]) + " | "
self.assertTrue(infoset == prefix1 + "uplayed;uplayed;uplayed")
infoset = kn.GetInfoset(Players.two)
prefix2 = str(kn.cards[1]) + " | "
self.assertTrue(infoset == prefix2 + "uplayed;uplayed;uplayed")
kn.MakeMove(Moves.bet)
infoset = kn.GetInfoset(Players.one)
self.assertTrue(infoset == prefix1 + "bet;uplayed;uplayed")
kn.MakeMove(Moves.pas)
infoset = kn.GetInfoset(Players.one)
self.assertTrue(infoset == prefix1 + "bet;pas;uplayed")
kn.MakeMove(Moves.pas)
infoset = kn.GetInfoset(Players.one)
self.assertTrue(infoset == prefix1 + "bet;pas;pas")
def CheckGetPrevInfosetRaiseIncorrectMoveId(self, kn, player):
with self.assertRaises(ValueError) as cm:
kn.GetPrevInfoset(player)
self.assertTrue('Call in incorrect currentMoveId' in str(cm.exception))
def test_prev_infoset(self):
kn = self.CreatePokerEngine()
infoset = kn.GetPrevInfoset(Players.one)
self.assertTrue(infoset =="GameTree")
self.CheckGetPrevInfosetRaiseIncorrectMoveId(kn, Players.two)
kn.MakeMove(Moves.bet)
self.CheckGetPrevInfosetRaiseIncorrectMoveId(kn, Players.one)
infoset = kn.GetPrevInfoset(Players.two)
self.assertTrue(infoset =="GameTree")
kn.MakeMove(Moves.bet)
infoset = kn.GetPrevInfoset( Players.one)
prefix1 = str(kn.cards[0]) + " | "
self.assertTrue(infoset == prefix1 + "uplayed;uplayed;uplayed")
self.CheckGetPrevInfosetRaiseIncorrectMoveId(kn, Players.two)
def CheckGetLastSubStateRaiseIncorrectMoveId(self, kn, player):
with self.assertRaises(ValueError) as cm:
kn.GetLastSubState(player)
self.assertTrue('Call in incorrect currentMoveId' in str(cm.exception))
def test_last_sub_set(self):
kn = self.CreatePokerEngine()
subState = kn.GetLastSubState(Players.one)
self.assertTrue(subState == str(kn.cards[0]))
self.CheckGetLastSubStateRaiseIncorrectMoveId(kn, Players.two)
kn.MakeMove(Moves.bet)
subState = kn.GetLastSubState(Players.two)
self.assertTrue(subState == "2 | bet")
self.CheckGetLastSubStateRaiseIncorrectMoveId(kn, Players.one)
kn.MakeMove(Moves.bet)
subState = kn.GetLastSubState(Players.one)
self.assertTrue(subState == "bet")
if __name__ == '__main__':
unittest.main()