|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame\Blackjack; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Smr\Blackjack\Card; |
|
7
|
|
|
use Smr\Blackjack\Result; |
|
8
|
|
|
use Smr\Blackjack\Table; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers Smr\Blackjack\Table |
|
12
|
|
|
*/ |
|
13
|
|
|
class TableTest extends TestCase { |
|
14
|
|
|
|
|
15
|
|
|
public function test_initial_state(): void { |
|
16
|
|
|
// Table constructed with deal=False, hands should be empty |
|
17
|
|
|
$table = new Table(false); |
|
18
|
|
|
self::assertEmpty($table->playerHand->getCards()); |
|
19
|
|
|
self::assertEmpty($table->playerHand->getCards()); |
|
20
|
|
|
|
|
21
|
|
|
// Table constructed with deal=True, hands have 2 cards each |
|
22
|
|
|
$table = new Table(true); |
|
23
|
|
|
self::assertCount(2, $table->playerHand->getCards()); |
|
24
|
|
|
self::assertCount(2, $table->playerHand->getCards()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function test_playerHits(): void { |
|
28
|
|
|
$table = new Table(false); |
|
29
|
|
|
|
|
30
|
|
|
// When the player "hits", they should get a card |
|
31
|
|
|
$table->playerHits(); |
|
32
|
|
|
self::assertCount(1, $table->playerHand->getCards()); |
|
33
|
|
|
|
|
34
|
|
|
// dealer's hand should be unchanged |
|
35
|
|
|
self::assertEmpty($table->dealerHand->getCards()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function test_dealerHitsUntil(): void { |
|
39
|
|
|
$table = new Table(false); |
|
40
|
|
|
|
|
41
|
|
|
// Dealer should be given cards until it has >= this value |
|
42
|
|
|
$table->dealerHitsUntil(22); |
|
43
|
|
|
self::assertGreaterThanOrEqual(22, $table->dealerHand->getValue()); |
|
44
|
|
|
|
|
45
|
|
|
// player's hand should be unchanged |
|
46
|
|
|
self::assertEmpty($table->playerHand->getCards()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function test_dealerHitsUntil_do_nothing(): void { |
|
50
|
|
|
$table = new Table(false); |
|
51
|
|
|
|
|
52
|
|
|
// Give the player 21 points |
|
53
|
|
|
$table->playerHand->addCard(new Card(12)); // king of hearts |
|
54
|
|
|
$table->playerHand->addCard(new Card(13)); // ace of clubs |
|
55
|
|
|
|
|
56
|
|
|
// Dealer is given no cards, since game is already over |
|
57
|
|
|
$table->dealerHitsUntil(22); |
|
58
|
|
|
self::assertEmpty($table->dealerHand->getCards()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @dataProvider provider_gameOver |
|
63
|
|
|
* |
|
64
|
|
|
* @param array<int> $playerCardIDs |
|
65
|
|
|
* @param array<int> $dealerCardIDs |
|
66
|
|
|
*/ |
|
67
|
|
|
public function test_gameOver(array $playerCardIDs, array $dealerCardIDs, bool $expected): void { |
|
68
|
|
|
$table = new Table(false); |
|
69
|
|
|
foreach ($playerCardIDs as $cardID) { |
|
70
|
|
|
$table->playerHand->addCard(new Card($cardID)); |
|
71
|
|
|
} |
|
72
|
|
|
foreach ($dealerCardIDs as $cardID) { |
|
73
|
|
|
$table->dealerHand->addCard(new Card($cardID)); |
|
74
|
|
|
} |
|
75
|
|
|
self::assertSame($expected, $table->gameOver()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array<array{array<int>, array<int>, bool}> |
|
|
|
|
|
|
80
|
|
|
*/ |
|
81
|
|
|
public function provider_gameOver(): array { |
|
82
|
|
|
return [ |
|
83
|
|
|
[[12, 13], [], true], // player 21, dealer 0 |
|
84
|
|
|
[[], [12, 13], true], // player 0, dealer 21 |
|
85
|
|
|
[[12, 13], [12, 13], true], // player 21, dealer 21 |
|
86
|
|
|
[[12, 12], [12, 12], false], // player 20, dealer 20 |
|
87
|
|
|
]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @dataProvider provider_getPlayerResult |
|
92
|
|
|
* |
|
93
|
|
|
* @param array<int> $playerCardIDs |
|
94
|
|
|
* @param array<int> $dealerCardIDs |
|
95
|
|
|
*/ |
|
96
|
|
|
public function test_getPlayerResult(array $playerCardIDs, array $dealerCardIDs, Result $expected): void { |
|
97
|
|
|
$table = new Table(false); |
|
98
|
|
|
foreach ($playerCardIDs as $cardID) { |
|
99
|
|
|
$table->playerHand->addCard(new Card($cardID)); |
|
100
|
|
|
} |
|
101
|
|
|
foreach ($dealerCardIDs as $cardID) { |
|
102
|
|
|
$table->dealerHand->addCard(new Card($cardID)); |
|
103
|
|
|
} |
|
104
|
|
|
self::assertSame($expected, $table->getPlayerResult()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array<array{array<int>, array<int>, Result}> |
|
|
|
|
|
|
109
|
|
|
*/ |
|
110
|
|
|
public function provider_getPlayerResult(): array { |
|
111
|
|
|
return [ |
|
112
|
|
|
[[12, 13], [], Result::Blackjack], // player has blackjack |
|
113
|
|
|
[[12, 13], [12, 13], Result::Blackjack], // both blackjack, player takes precedence |
|
114
|
|
|
[[12], [12], Result::Tie], // equal score, no one busted |
|
115
|
|
|
[[12, 12, 12], [], Result::Lose], // player busts, dealer does not |
|
116
|
|
|
[[], [12, 12, 12], Result::Win], // dealer busts, player does not |
|
117
|
|
|
[[12, 12, 12], [12, 12, 12], Result::Lose], // both bust, player takes precedence |
|
118
|
|
|
[[13], [12], Result::Win], // player higher score, no one busted |
|
119
|
|
|
[[12], [13], Result::Lose], // dealer higher score, no one busted |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|