forked from ryananicholson/PHP-Chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
197 lines (178 loc) · 7.11 KB
/
index.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
session_start();
/**
* Include the needed class file
* @param string $classname The name of the class file to include
*/
function autoloader($classname)
{
include 'classes/' . $classname . '.php';
}
spl_autoload_register('autoloader');
if (isset($_GET['reset']) && $_GET['reset'] == 1)
{
session_destroy();
header('Location: index.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style/style.css" type="text/css" rel="stylesheet" media="all" />
<title>Chess</title>
</head>
<body>
<?php
// Board
if (!isset($_SESSION['board']))
{
$board = new Board();
$board->Init();
}
else
{
$board = unserialize($_SESSION['board']);
}
// Log
if (!isset($_SESSION['logs']))
$logs = new Log();
else
{
$logs = unserialize($_SESSION['logs']);
}
// History
if (isset($_GET['previous']) && $_GET['previous'] == 1)
{
$board->Previous();
}
else if (isset($_GET['next']) && $_GET['next'] == 1)
{
$board->Next();
}
// Promotion
if (isset($_GET['action']) && $_GET['action'] == 'promotion' &&
isset($_GET['x']) && ctype_digit($_GET['x']) &&
isset($_GET['y']) && ctype_digit($_GET['y']))
{
$origin = unserialize($_SESSION['origin']);
$piece = $board->GetPiece($origin);
$x = $_GET['x'];
$y = $_GET['y'];
echo '<div id="promotion-box"><h1>Please choose your promotion !</h1>';
$pieceTypes = array('bishop', 'knight', 'rook', 'queen');
foreach ($pieceTypes as $type)
{
echo '<a href="index.php?action=move_target&x=' . $x . '&y=' . $y . '&choice=' . $type . '"><img src="sprites/' . $piece->GetColor() . '_' . $type . '.png" /></a>';
}
echo '</div>';
}
else
{
// Display the current player turn
if (empty($_GET) && !$board->IsFinished())
{
$logs->Add($board->DisplayTurn(), 'info');
}
if (isset($_GET['action']))
{
switch ($_GET['action'])
{
case 'move_origin':
if (isset($_GET['x']) && ctype_digit($_GET['x']) &&
isset($_GET['y']) && ctype_digit($_GET['y']))
{
$origin = new Position($_GET['x'], $_GET['y']);
$piece = $board->GetPiece($origin);
if ($piece !== null)
{
if ($piece->GetColor() == $board->GetTurn())
{
$board->ComputeRealPossibleCells($piece);
if (count($piece->GetPossibleCells()) == 0)
{
$board->CleanPossibleCells($board->GetTurn());
$logs->Add('No move available for this piece !', 'error');
header('Location: index.php');
}
else
$_SESSION['origin'] = serialize($origin);
}
else
{
$logs->Add('This is not your turn !', 'error');
}
}
}
break;
case 'move_target':
if (isset($_SESSION['origin']))
{
if (isset($_GET['x']) && ctype_digit($_GET['x']) &&
isset($_GET['y']) && ctype_digit($_GET['y']))
{
$origin = unserialize($_SESSION['origin']);
$piece = $board->GetPiece($origin);
$target = new Position((int)$_GET['x'], (int)$_GET['y']);
if ($board->IsPromotion($piece, $target) && empty($_GET['choice']))
{
header('Location: index.php?action=promotion&x=' . $target->x . '&y=' . $target->y);
exit;
}
else
{
if (!empty($_GET['choice']))
$_SESSION['promotion'] = $_GET['choice'];
if ($board->Move($origin, $target))
{
$board->NextTurn();
}
else
{
$logs->Add('Invalid move !!', 'error');
}
}
}
else
{
$logs->Add('Invalid move !', 'error');
}
unset($_SESSION['origin']);
header('Location: index.php');
}
break;
default:
break;
}
}
if (isset($_GET['clear']) && $_GET['clear'] == 1)
{
$logs->Clear();
}
$_SESSION['board'] = serialize($board);
$_SESSION['logs'] = serialize($logs);
?>
<div id="board">
<?php $board->DrawBoard(); ?>
</div>
<div id="info">
<p style="text-align: center;">
<a href="index.php?reset=1" style="font-weight: bold; font-size: xx-large;">Reset the game !</a><br />
<a href="index.php?previous=1" style="float: left; font-style: italic;"><-- Previous</a>
<a href="index.php?clear=1" style="font-style: italic;">(Clear logs)</a>
<a href="index.php" style="float: right; font-style: italic;">Next --></a>
</p>
<div id="logs">
<?php $logs->Display(); ?>
</div>
<script>
// Auto scroll for log's window
var elem = document.getElementById('logs');
elem.scrollTop = elem.scrollHeight;
</script>
</div>
<?php
}
?>
</body>
</html>