1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\DominoGame\Value; |
6
|
|
|
|
7
|
|
|
use Arp\DominoGame\Exception\DominoGameException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Alex Patterson <[email protected]> |
11
|
|
|
* @package Arp\DominoGame\Value |
12
|
|
|
*/ |
13
|
|
|
class PlayerCollection extends AbstractCollection |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Sort and return a NEW collection sorted by players with the lowest number of dominoes in their hand. |
17
|
|
|
* |
18
|
|
|
* @return PlayerCollection |
19
|
|
|
*/ |
20
|
|
|
public function getOrderedByLowestCount(): PlayerCollection |
21
|
|
|
{ |
22
|
|
|
$elements = $this->elements; |
23
|
|
|
|
24
|
|
|
usort( |
25
|
|
|
$elements, |
26
|
|
|
static function (Player $playerA, Player $playerB): int { |
27
|
|
|
return $playerA->getHandCount() <=> $playerB->getHandCount(); |
28
|
|
|
} |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
return new static($elements); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sort and return a NEW collection sorted by players with the highest double in their hand. |
36
|
|
|
* |
37
|
|
|
* @return PlayerCollection |
38
|
|
|
*/ |
39
|
|
|
public function getOrderedByHighestDouble(): PlayerCollection |
40
|
|
|
{ |
41
|
|
|
$elements = $this->elements; |
42
|
|
|
|
43
|
|
|
usort( |
44
|
|
|
$elements, |
45
|
|
|
static function (Player $playerA, Player $playerB): int { |
46
|
|
|
$doubleA = $playerA->getHighestDouble(); |
47
|
|
|
$doubleB = $playerB->getHighestDouble(); |
48
|
|
|
|
49
|
|
|
if (null === $doubleA && null === $doubleB) { |
50
|
|
|
return 0; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ((null !== $doubleA) && $doubleA->isDouble() && (null === $doubleB || !$doubleB->isDouble())) { |
54
|
|
|
return 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ((null !== $doubleB) && $doubleB->isDouble() && (null === $doubleA || !$doubleA->isDouble())) { |
58
|
|
|
return 1; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $doubleB->getValue() <=> $doubleA->getValue(); |
62
|
|
|
} |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return new static($elements); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return the player with the lowest valued sum of all tiles in their hand. |
70
|
|
|
* |
71
|
|
|
* @return Player |
72
|
|
|
* |
73
|
|
|
* @throws DominoGameException If the collection is empty |
74
|
|
|
*/ |
75
|
|
|
public function getWithLowestHandValue(): Player |
76
|
|
|
{ |
77
|
|
|
$elements = $this->elements; |
78
|
|
|
|
79
|
|
|
usort( |
80
|
|
|
$elements, |
81
|
|
|
static function (Player $playerA, Player $playerB): int { |
82
|
|
|
return $playerA->getHandValue() <=> $playerB->getHandValue(); |
83
|
|
|
} |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
/** @var Player $player */ |
87
|
|
|
$player = (new static($elements))->first(); |
88
|
|
|
|
89
|
|
|
if (null === $player) { |
90
|
|
|
throw new DominoGameException( |
91
|
|
|
'Unable to find player with lowest value with an empty player collection' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $player; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|