-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
70 lines (59 loc) · 2.25 KB
/
index.php
File metadata and controls
70 lines (59 loc) · 2.25 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
64
65
66
67
68
69
70
<?php
require_once('flight/Flight.php');
Flight::set('flight.views.path', 'tmpl');
Flight::route('/', function(){
Flight::render('home.php', array(), 'body');
Flight::render('layout.php', array('title' => 'Tic Tac Toe Example'));
});
Flight::route('POST /move', function(){
$request = Flight::request();
$player_types = array('x', 'o');
$player = $request->data['player'];
$board = $request->data['board'];
$has_winner = FALSE;
$has_moves = FALSE;
if(!in_array($player, $player_types)){
$out = array('success'=>FALSE);
return Flight::json($out);
}
if(gettype($board) != 'array' or count($board) != 9){
$out = array('success'=>FALSE);
return Flight::json($out);
}
//Check all winning conditions
if(
($board['1'] == $player && $board['2'] == $player && $board['3'] == $player) ||
($board['4'] == $player && $board['5'] == $player && $board['6'] == $player) ||
($board['7'] == $player && $board['8'] == $player && $board['9'] == $player) ||
($board['1'] == $player && $board['4'] == $player && $board['7'] == $player) ||
($board['2'] == $player && $board['5'] == $player && $board['8'] == $player) ||
($board['3'] == $player && $board['6'] == $player && $board['9'] == $player) ||
($board['3'] == $player && $board['5'] == $player && $board['7'] == $player) ||
($board['1'] == $player && $board['5'] == $player && $board['9'] == $player)
){
$has_winner = TRUE;
}
//check to see if there are any open squares remaining
foreach($board as $b=>$p){
if($p == '_'){
$has_moves = TRUE;
}
}
if($player == 'x'){
$next_player = 'o';
}else{
$next_player = 'x';
}
$out = array(
'success' => TRUE,
'has_winner' => $has_winner,
'has_moves' => $has_moves,
'next_player' => $next_player
);
return Flight::json($out);
});
Flight::route('GET /move', function(){
echo "Nope";
});
Flight::start();
?>