-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGame.php
178 lines (154 loc) · 5.86 KB
/
Game.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
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
169
170
171
172
173
174
175
176
177
178
<?php
namespace Packages\Models\GameOrganizer;
use Packages\Models\Bot\BotFactory;
use Packages\Models\Bot\BotType;
use Packages\Models\GameOrganizer\Participant\ParticipantInterface;
use Packages\Models\Othello\Board\Color\Color;
use Packages\Models\Othello\Board\Position\Position;
use Packages\Models\Othello\Othello\Othello;
class Game
{
private function __construct(
private string $id,
private GameMode $gameMode,
private Participants $participants,
private GameStatus $gameStatus,
private Othello $turn,
)
{
// ゲームモードと実際の参加者の組み合わせをチェック
// プレイヤー対戦時に参加者がプレイヤーのみであること
if ($this->gameMode->isVsPlayerMode() && !$this->participants->hasOnlyPlayers()) {
throw new \RuntimeException('ゲームモードと参加者の種類の組み合わせが正しくありません');
}
// ボット対戦時に参加者にプレイヤーとボットが両方いること(偏りがないこと)
if ($this->gameMode->isVsBotMode() && ($this->participants->hasOnlyPlayers() || $this->participants->hasOnlyBots())) {
throw new \RuntimeException('ゲームモードと参加者の種類の組み合わせが正しくありません');
}
// 観戦時に参加者がボットのみであること
if ($this->gameMode->isViewingMode() && !$this->participants->hasOnlyBots()) {
throw new \RuntimeException('ゲームモードと参加者の種類の組み合わせが正しくありません');
}
// ゲームステータスとターンの状態チェック
if ($this->gameStatus->isPlaying() && $this->isGameOver()) {
throw new \RuntimeException('ゲームステータスとターンの状態が一致しません');
}
if ($this->gameStatus->isFinished() && !$this->isGameOver()) {
throw new \RuntimeException('ゲームステータスとターンの状態が一致しません');
}
}
public static function init(string $id, GameMode $gameMode, Participants $participants)
{
return new Game(
id: $id,
gameMode: $gameMode,
participants: $participants,
gameStatus: GameStatus::playing(),
turn: Othello::init()
);
}
public static function make(string $id, GameMode $gameMode, Participants $participants, GameStatus $status, Othello $turn)
{
return new Game(
id: $id,
gameMode: $gameMode,
participants: $participants,
gameStatus: $status,
turn: $turn
);
}
public function process(?Position $playerMove = null): Game
{
if (!$this->gameStatus->isPlaying()) throw new \RuntimeException();
if ($this->isBotTurn() && !$this->turn->mustSkip()) {
// TODO: ターンとプレイヤーの行動制限についてもう少し検討する(更新と制限処理の分離など)
if (isset($playerMove)) throw new \RuntimeException('ボットのターンにプレイヤーが行動することはできません。');
// ボットを起動して置く場所を算出する
$botType = BotType::from($this->getCurrentPlayer()->getId());
$bot = BotFactory::make($botType);
$playerMove = $bot->run($this->turn);
}
$nextTurn = $this->turn->next($playerMove);
if (!$nextTurn->isContinuable() || $nextTurn->finishedLastTurn()) {
$nextGameStatus = GameStatus::finish();
}
return new Game(
$this->id,
$this->gameMode,
$this->participants,
$nextGameStatus ?? $this->gameStatus,
$nextTurn
);
}
public function isGameOver(): bool
{
return $this->gameStatus->isFinished();
}
public function determinResult()
{
if (!$this->isGameOver()) throw new \RuntimeException();
}
// TODO: あとでリファクタリング
public function getWinner(): ?ParticipantInterface
{
if (!$this->isGameOver()) throw new \RuntimeException();
$whiteScore = $this->turn->getBoard()->getPoint(Color::white());
$blackScore = $this->turn->getBoard()->getPoint(Color::black());
// 勝敗判定
if ($whiteScore > $blackScore) {
return $this->participants->whitePlayer();
} elseif ($whiteScore < $blackScore) {
return $this->participants->blackPlayer();
}
// 勝者がいない場合はnull
return null;
}
public function isBotTurn(): bool
{
// プレイヤー同士の対戦にはボットが存在しない
if ($this->gameMode->isVsPlayerMode()) return false;
// 観戦モードではボットのターンしか存在しない
if ($this->gameMode->isViewingMode()) return true;
// ボット対戦モードの場合
return $this->getCurrentPlayer()->isBot();
}
public function getCurrentPlayer(): ParticipantInterface
{
return $this->participants->findByColor($this->turn->getPlayableColor());
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return GameMode
*/
public function getMode(): GameMode
{
return $this->gameMode;
}
/**
* @return Participants
*/
public function getParticipants(): Participants
{
return $this->participants;
}
/**
* @return GameStatus
*/
public function getStatus(): GameStatus
{
return $this->gameStatus;
}
/**
* @return Othello
*/
public function getTurn(): Othello
{
return $this->turn;
}
}