/
ai.php
61 lines (48 loc) · 1.11 KB
/
ai.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
<?php
require_once '../../support/php/JavelinHelper.php';
$game = json_decode($_REQUEST['game']);
$response = array();
if (check_win($game, 'X')) {
$response['outcome'] = 'You win! Reload to play again.';
} else if (count(array_filter($game)) == 9) {
$response['outcome'] = 'Tie game. Reload to play again.';
} else {
do {
$pos = mt_rand(0, 8);
if (empty($game[$pos])) {
$game[$pos] = 'O';
$response['pos'] = $pos;
$response['move'] = 'O';
break;
}
} while (true);
if (check_win($game, 'O')) {
$response['outcome'] = 'You lose! Reload to play again.';
}
}
echo JavelinHelper::renderAjaxResponse($response);
function check_win($game, $player) {
static $wins = array(
0, 1, 2, // horizontal
3, 4, 5,
6, 7, 8,
0, 3, 6, // vertical
1, 4, 7,
2, 5, 8,
0, 4, 8, // diagonal
6, 4, 2,
);
foreach (array_chunk($wins, 3) as $win) {
$is_win = true;
foreach ($win as $cell) {
if ($game[$cell] !== $player) {
$is_win = false;
break;
}
}
if ($is_win) {
return true;
}
}
return false;
}