|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Tests\Game; |
|
4
|
|
|
|
|
5
|
|
|
use App\GameProj\BlackjackGameProj; |
|
6
|
|
|
use App\Card\DeckProj; |
|
7
|
|
|
use App\Card\Card; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class BlackjackGameProjStartTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Construct object and verify that the object has the expected |
|
14
|
|
|
* properties. |
|
15
|
|
|
*/ |
|
16
|
|
|
public function testCreateBlackjackGameProj() |
|
17
|
|
|
{ |
|
18
|
|
|
$game = new BlackjackGameProj('Player', 1000.00, 1, 10.00); |
|
19
|
|
|
$this->assertInstanceOf(BlackjackGameProj::class, $game); |
|
20
|
|
|
|
|
21
|
|
|
$this->assertEquals('Player', $game->getPlayerName()); |
|
22
|
|
|
$this->assertEquals(990.00, $game->getPlayerBank()); |
|
23
|
|
|
$this->assertCount(1, $game->getHands()); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Test constructor exception for invalid hands number. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testConstructorExceptionForInvalidNumberOfHands() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
32
|
|
|
new BlackjackGameProj('Player', 1000.00, 4, 10.00); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Test dealing cards. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testDealCards() |
|
39
|
|
|
{ |
|
40
|
|
|
$game = new BlackjackGameProj('Player', 1000.00, 1, 10.00); |
|
41
|
|
|
$game->dealInitialCards(); |
|
42
|
|
|
|
|
43
|
|
|
$hands = $game->getHands(); |
|
44
|
|
|
$this->assertCount(2, $hands[0]['hand']); |
|
45
|
|
|
|
|
46
|
|
|
$dealerHand = $game->getDealerHand(); |
|
47
|
|
|
$this->assertCount(1, $dealerHand); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Test hitting player and dealer. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testHit() |
|
54
|
|
|
{ |
|
55
|
|
|
$game = new BlackjackGameProj('Player', 1000.00, 1, 10.00); |
|
56
|
|
|
$game->dealInitialCards(); |
|
57
|
|
|
|
|
58
|
|
|
$game->hitHand(0); |
|
59
|
|
|
$hands = $game->getHands(); |
|
60
|
|
|
$this->assertCount(3, $hands[0]['hand']); |
|
61
|
|
|
|
|
62
|
|
|
$game->hitDealer(); |
|
63
|
|
|
$dealerHand = $game->getDealerHand(); |
|
64
|
|
|
$this->assertCount(2, $dealerHand); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Test hand value, stub Card to assure the value can be asserted. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function testGetHandValue() |
|
71
|
|
|
{ |
|
72
|
|
|
$game = new BlackjackGameProj('Player', 1000.00, 1, 10.00); |
|
73
|
|
|
|
|
74
|
|
|
// Create a stub for the Card class |
|
75
|
|
|
$stub = $this->createMock(Card::class); |
|
76
|
|
|
|
|
77
|
|
|
// Configure the stub |
|
78
|
|
|
$stub->method('getBlackjackValue') |
|
79
|
|
|
->willReturn(7, 10); |
|
80
|
|
|
$stub->method('getValue') |
|
81
|
|
|
->willReturn('7', 'King'); |
|
82
|
|
|
|
|
83
|
|
|
$hand = [clone $stub, clone $stub]; |
|
84
|
|
|
|
|
85
|
|
|
$value = $game->getHandValue($hand); |
|
86
|
|
|
$this->assertEquals(17, $value); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Test getBustProbability method for soft ace. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testGetBustProbabilitySoftAce() |
|
93
|
|
|
{ |
|
94
|
|
|
$game = new BlackjackGameProj('Player', 1000.00, 1, 10.00); |
|
95
|
|
|
|
|
96
|
|
|
// Create stubs for the Card class |
|
97
|
|
|
$aceStub = $this->createMock(Card::class); |
|
98
|
|
|
$aceStub->method('getBlackjackValue')->willReturn(11); |
|
99
|
|
|
$aceStub->method('getValue')->willReturn('Ace'); |
|
100
|
|
|
|
|
101
|
|
|
$sevenStub = $this->createMock(Card::class); |
|
102
|
|
|
$sevenStub->method('getBlackjackValue')->willReturn(7); |
|
103
|
|
|
$sevenStub->method('getValue')->willReturn('7'); |
|
104
|
|
|
|
|
105
|
|
|
$hand = [$aceStub, $sevenStub]; |
|
106
|
|
|
|
|
107
|
|
|
// Set the mocked hand as the player's hand |
|
108
|
|
|
$game->setPlayerHand($hand); |
|
109
|
|
|
|
|
110
|
|
|
$bustProbability = $game->getBustProbability(0); |
|
111
|
|
|
|
|
112
|
|
|
// With Ace-7 (soft 18), bust probability should be 0 |
|
113
|
|
|
$this->assertEquals(0, $bustProbability); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Test getBustProbability method. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function testGetBustProbabilityRest() |
|
120
|
|
|
{ |
|
121
|
|
|
$game = new BlackjackGameProj('Player', 1000.00, 1, 10.00); |
|
122
|
|
|
|
|
123
|
|
|
// Create stubs for the Card class |
|
124
|
|
|
$kingStub = $this->createMock(Card::class); |
|
125
|
|
|
$kingStub->method('getBlackjackValue')->willReturn(10); |
|
126
|
|
|
$kingStub->method('getValue')->willReturn('King'); |
|
127
|
|
|
|
|
128
|
|
|
$sevenStub = $this->createMock(Card::class); |
|
129
|
|
|
$sevenStub->method('getBlackjackValue')->willReturn(7); |
|
130
|
|
|
$sevenStub->method('getValue')->willReturn('7'); |
|
131
|
|
|
|
|
132
|
|
|
$hand = [$kingStub, $sevenStub]; |
|
133
|
|
|
|
|
134
|
|
|
// Set the mocked hand as the player's hand |
|
135
|
|
|
$game->setPlayerHand($hand); |
|
136
|
|
|
|
|
137
|
|
|
$bustProbability = $game->getBustProbability(0); |
|
138
|
|
|
$this->assertLessThan(0.70, $bustProbability); |
|
139
|
|
|
$this->assertGreaterThan(0.68, $bustProbability); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Test playing the computer's hand. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function testPlayComputerHand() |
|
146
|
|
|
{ |
|
147
|
|
|
$game = new BlackjackGameProj('Player', 1000.00, 2, 10.00, 1, 'smart'); |
|
148
|
|
|
|
|
149
|
|
|
// Create stubs for the Card class |
|
150
|
|
|
$aceStub = $this->createMock(Card::class); |
|
151
|
|
|
$aceStub->method('getBlackjackValue')->willReturn(11); |
|
152
|
|
|
$aceStub->method('getValue')->willReturn('Ace'); |
|
153
|
|
|
|
|
154
|
|
|
$sixStub = $this->createMock(Card::class); |
|
155
|
|
|
$sixStub->method('getBlackjackValue')->willReturn(6); |
|
156
|
|
|
$sixStub->method('getValue')->willReturn('6'); |
|
157
|
|
|
|
|
158
|
|
|
$hand = [$aceStub, $sixStub]; |
|
159
|
|
|
|
|
160
|
|
|
$game->setPlayerHand($hand, 1); |
|
161
|
|
|
|
|
162
|
|
|
$game->standHand(0); |
|
163
|
|
|
|
|
164
|
|
|
$game->playComputerHand(); |
|
165
|
|
|
|
|
166
|
|
|
// Force a bust |
|
167
|
|
|
while (!$game->isHandBusted(1)) { |
|
168
|
|
|
$game->hitHand(1); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$hands = $game->getHands(); |
|
172
|
|
|
$this->assertNotEquals('playing', $hands[1]['status']); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Test standHand method. |
|
177
|
|
|
*/ |
|
178
|
|
|
public function testStandHand() |
|
179
|
|
|
{ |
|
180
|
|
|
$game = new BlackjackGameProj('Player', 1000.00, 2, 20.00); |
|
181
|
|
|
$game->dealInitialCards(); |
|
182
|
|
|
|
|
183
|
|
|
$game->standHand(0); |
|
184
|
|
|
$hands = $game->getHands(); |
|
185
|
|
|
$this->assertEquals('standing', $hands[0]['status']); |
|
186
|
|
|
|
|
187
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
188
|
|
|
$game->standHand(5); // Invalid hand index |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|