-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathParticipants.php
92 lines (77 loc) · 2.5 KB
/
Participants.php
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
<?php
namespace Packages\Models\GameOrganizer;
use Packages\Models\GameOrganizer\Participant\ParticipantInterface;
use Packages\Models\Othello\Board\Color\Color;
class Participants
{
const MAX_PARTICIPANTS = 2;
/**
* ゲームへの参加者のリスト
* 人間は"Player", ボットは"BotParticipant"
* @var array<string, ParticipantInterface>
*/
private array $participants;
private function __construct(ParticipantInterface $whiteParticipant, ParticipantInterface $blackParticipant,)
{
$this->participants[Color::white()->toCode()] = $whiteParticipant;
$this->participants[Color::black()->toCode()] = $blackParticipant;
}
// ---------------------------------------
// 生成系
// ---------------------------------------
/**
* @param ParticipantInterface $whiteParticipant
* @param ParticipantInterface $blackParticipant
* @return Participants
*/
public static function make(ParticipantInterface $whiteParticipant, ParticipantInterface $blackParticipant): Participants
{
return new Participants($whiteParticipant, $blackParticipant);
}
// ---------------------------------------
// 判定系
// ---------------------------------------
public function hasOnlyPlayers(): bool
{
return $this->countPlayers() === self::MAX_PARTICIPANTS;
}
public function hasOnlyBots(): bool
{
return $this->countBots() === self::MAX_PARTICIPANTS;
}
public function countPlayers(): int
{
return count($this->players());
}
public function countBots(): int
{
return count($this->bots());
}
// ---------------------------------------
// getter
// ---------------------------------------
public function whitePlayer(): ParticipantInterface
{
return $this->participants[Color::white()->toCode()];
}
public function blackPlayer(): ParticipantInterface
{
return $this->participants[Color::black()->toCode()];
}
public function players(): array
{
return array_filter($this->participants, function ($participant) {
return $participant->isPlayer();
});
}
public function bots(): array
{
return array_filter($this->participants, function ($participant) {
return $participant->isBot();
});
}
public function findByColor(Color $color): ?ParticipantInterface
{
return $this->participants[$color->toCode()] ?? null;
}
}