Skip to content

Commit 418fa17

Browse files
committed
Update to enum and remove static
1 parent 90f1910 commit 418fa17

File tree

3 files changed

+48
-38
lines changed

3 files changed

+48
-38
lines changed

exercises/practice/state-of-tic-tac-toe/.meta/example.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
declare(strict_types=1);
44

5-
class StateOfTicTacToe
5+
enum State
66
{
7-
const WIN = "win";
8-
const ONGOING = "ongoing";
9-
const DRAW = "draw";
7+
case Win;
8+
case Ongoing;
9+
case Draw;
10+
}
1011

11-
public static function gameState(array $board): string
12+
class StateOfTicTacToe
13+
{
14+
public function gameState(array $board): State
1215
{
13-
$xCount = self::countPlayer($board, 'X');
14-
$oCount = self::countPlayer($board, 'O');
16+
$xCount = $this->countPlayer($board, 'X');
17+
$oCount = $this->countPlayer($board, 'O');
1518

1619
if ($xCount < $oCount) {
1720
throw new \RuntimeException("Wrong turn order: O started");
@@ -21,25 +24,25 @@ public static function gameState(array $board): string
2124
throw new \RuntimeException("Wrong turn order: X went twice");
2225
}
2326

24-
$xWon = self::hasWon($board, 'X');
25-
$oWon = self::hasWon($board, 'O');
27+
$xWon = $this->hasWon($board, 'X');
28+
$oWon = $this->hasWon($board, 'O');
2629

2730
if ($xWon && $oWon) {
2831
throw new \RuntimeException("Impossible board: game should have ended after the game was won");
2932
}
3033

3134
if ($xWon || $oWon) {
32-
return self::WIN;
35+
return State::Win;
3336
}
3437

3538
if ($xCount + $oCount === 9) {
36-
return self::DRAW;
39+
return State::Draw;
3740
}
3841

39-
return self::ONGOING;
42+
return State::Ongoing;
4043
}
4144

42-
private static function countPlayer(array $board, string $player): int
45+
private function countPlayer(array $board, string $player): int
4346
{
4447
$count = 0;
4548
foreach ($board as $row) {
@@ -52,7 +55,7 @@ private static function countPlayer(array $board, string $player): int
5255
return $count;
5356
}
5457

55-
private static function hasWon(array $board, string $player): bool
58+
private function hasWon(array $board, string $player): bool
5659
{
5760
for ($i = 0; $i < 3; $i++) {
5861
if (($board[$i][0] === $player && $board[$i][1] === $player && $board[$i][2] === $player) || ($board[0][$i] === $player && $board[1][$i] === $player && $board[2][$i] === $player)) {

exercises/practice/state-of-tic-tac-toe/StateOfTicTacToe.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@
2424

2525
declare(strict_types=1);
2626

27+
enum State
28+
{
29+
case Win;
30+
case Ongoing;
31+
case Draw;
32+
}
33+
2734
class StateOfTicTacToe
2835
{
29-
public static function gameState(array $board): string
36+
public function gameState(array $board): State
3037
{
3138
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
3239
}

exercises/practice/state-of-tic-tac-toe/StateOfTicTacToeTest.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testWonGamesFinishedGameWhereXWonViaLeftColumnVictory(): void
2626
"X ",
2727
"X "
2828
];
29-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
29+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
3030
}
3131

3232
/**
@@ -39,7 +39,7 @@ public function testWonGameFinishedGameWhereXWonViaMiddleColumnVictory(): void
3939
" X ",
4040
" X "
4141
];
42-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
42+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
4343
}
4444

4545
/**
@@ -52,7 +52,7 @@ public function testWonGameFinishedGameWhereXWonViaRightColumnVictory(): void
5252
" X",
5353
" X"
5454
];
55-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
55+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
5656
}
5757

5858
/**
@@ -65,7 +65,7 @@ public function testWonGamesFinishedGameWhereOWonViaLeftColumnVictory(): void
6565
"OX ",
6666
"O "
6767
];
68-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
68+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
6969
}
7070

7171
/**
@@ -78,7 +78,7 @@ public function testWonGameFinishedGameWhereOWonViaMiddleColumnVictory(): void
7878
" OX",
7979
" O "
8080
];
81-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
81+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
8282
}
8383

8484
/**
@@ -91,7 +91,7 @@ public function testWonGameFinishedGameWhereOWonViaRightColumnVictory(): void
9191
" XO",
9292
" O"
9393
];
94-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
94+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
9595
}
9696

9797
/**
@@ -104,7 +104,7 @@ public function testWonGameFinishedWhereXWonViaTopRowVictory(): void
104104
"XOO",
105105
"O "
106106
];
107-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
107+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
108108
}
109109

110110
/**
@@ -117,7 +117,7 @@ public function testWonGameFinishedWhereXWonViaMiddleRowVictory(): void
117117
"XXX",
118118
" O "
119119
];
120-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
120+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
121121
}
122122

123123
/**
@@ -130,7 +130,7 @@ public function testWonGameFinishedWhereXWonViaBottomRowVictory(): void
130130
"O X",
131131
"XXX"
132132
];
133-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
133+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
134134
}
135135

136136
/**
@@ -143,7 +143,7 @@ public function testWonGameFinishedWhereOWonViaTopRowVictory(): void
143143
"XXO",
144144
"XX "
145145
];
146-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
146+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
147147
}
148148

149149
/**
@@ -156,7 +156,7 @@ public function testWonGameFinishedWhereOWonViaMiddleRowVictory(): void
156156
"OOO",
157157
"X "
158158
];
159-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
159+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
160160
}
161161

162162
/**
@@ -169,7 +169,7 @@ public function testWonGameFinishedWhereOWonViaBottomRowVictory(): void
169169
" XX",
170170
"OOO"
171171
];
172-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
172+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
173173
}
174174

175175
/**
@@ -182,7 +182,7 @@ public function testWonGameFinishedWhereXWonViaFallingDiagonalVictory(): void
182182
" X ",
183183
" X"
184184
];
185-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
185+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
186186
}
187187

188188
/**
@@ -195,7 +195,7 @@ public function testWonGameFinishedWhereXWonViaRisingDiagonalVictory(): void
195195
"OX ",
196196
"X "
197197
];
198-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
198+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
199199
}
200200

201201
/**
@@ -208,7 +208,7 @@ public function testWonGameFinishedWhereOWonViaFallingDiagonalVictory(): void
208208
"OOX",
209209
"X O"
210210
];
211-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
211+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
212212
}
213213

214214
/**
@@ -221,7 +221,7 @@ public function testWonGameFinishedWhereOWonViaRisingDiagonalVictory(): void
221221
" OX",
222222
"OXX"
223223
];
224-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
224+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
225225
}
226226

227227
/**
@@ -234,7 +234,7 @@ public function testWonGameFinishedGameWhereXWonViaARowAndAColumnVictory(): void
234234
"XOO",
235235
"XOO"
236236
];
237-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
237+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
238238
}
239239

240240
/**
@@ -247,7 +247,7 @@ public function testWonGameFinishedGameWhereXWonViaTwoDiagonalVictories(): void
247247
"OXO",
248248
"XOX"
249249
];
250-
$this->assertEquals('win', $this->stateOfTicTacToe->gameState($board));
250+
$this->assertEquals(State::Win, $this->stateOfTicTacToe->gameState($board));
251251
}
252252

253253
/**
@@ -260,7 +260,7 @@ public function testDrawGame(): void
260260
"XXO",
261261
"OXO"
262262
];
263-
$this->assertEquals('draw', $this->stateOfTicTacToe->gameState($board));
263+
$this->assertEquals(State::Draw, $this->stateOfTicTacToe->gameState($board));
264264
}
265265

266266
/**
@@ -273,7 +273,7 @@ public function testAnotherDrawGame(): void
273273
"OXX",
274274
"XOO"
275275
];
276-
$this->assertEquals('draw', $this->stateOfTicTacToe->gameState($board));
276+
$this->assertEquals(State::Draw, $this->stateOfTicTacToe->gameState($board));
277277
}
278278

279279
/**
@@ -286,7 +286,7 @@ public function testOngoingGameOneMoveIn(): void
286286
"X ",
287287
" "
288288
];
289-
$this->assertEquals('ongoing', $this->stateOfTicTacToe->gameState($board));
289+
$this->assertEquals(State::Ongoing, $this->stateOfTicTacToe->gameState($board));
290290
}
291291

292292
/**
@@ -299,7 +299,7 @@ public function testOngoingGameTwoMovesIn(): void
299299
" X ",
300300
" "
301301
];
302-
$this->assertEquals('ongoing', $this->stateOfTicTacToe->gameState($board));
302+
$this->assertEquals(State::Ongoing, $this->stateOfTicTacToe->gameState($board));
303303
}
304304

305305
/**
@@ -312,7 +312,7 @@ public function testOngoingGameFiveMovesIn(): void
312312
" XO",
313313
"OX "
314314
];
315-
$this->assertEquals('ongoing', $this->stateOfTicTacToe->gameState($board));
315+
$this->assertEquals(State::Ongoing, $this->stateOfTicTacToe->gameState($board));
316316
}
317317

318318
/**

0 commit comments

Comments
 (0)