-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFindWinnerOnATicTacToeGame.php
More file actions
63 lines (57 loc) · 1.56 KB
/
FindWinnerOnATicTacToeGame.php
File metadata and controls
63 lines (57 loc) · 1.56 KB
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
<?php
declare(strict_types=1);
namespace leetcode;
class FindWinnerOnATicTacToeGame
{
public static function tictactoe(array $moves): string
{
if (empty($moves)) {
return '';
}
$row = $col = array_fill(0, 2, array_fill(0, 3, 0));
$d1 = $d2 = array_fill(0, 2, 0);
foreach ($moves as $k => $v) {
[$r, $c, $i] = [$v[0], $v[1], $k % 2];
if (++$row[$i][$r] === 3 ||
++$col[$i][$c] === 3 ||
($r === $c && ++$d1[$i] === 3) ||
($r + $c === 2 && ++$d2[$i] === 3)) {
return $i === 0 ? 'A' : 'B';
}
}
return count($moves) === 9 ? 'Draw' : 'Pending';
}
public static function tictactoe2(array $moves): string
{
if (empty($moves)) {
return '';
}
$n = 8;
$a = $b = array_fill(0, $n, 0);
foreach ($moves as $k => $v) {
[$r, $c] = [$v[0], $v[1]];
if ($k % 2 === 0) {
$player = & $a;
} else {
$player = & $b;
}
$player[$r]++;
$player[$c + 3]++;
if ($r === $c) {
$player[6]++;
}
if ($r + $c === 2) {
$player[7]++;
}
}
for ($i = 0; $i < $n; $i++) {
if ($a[$i] === 3) {
return 'A';
}
if ($b[$i] === 3) {
return 'B';
}
}
return count($moves) === 9 ? 'Draw' : 'Pending';
}
}