-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
184 lines (174 loc) · 6.84 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
<?php
define(WS_BOMB, -1);
define(WS_UNSET, '');
function generate_grid($width=9, $height=9, $bombs) {
srand($_GET['gameid']*$width*$height*$bombs);
$field = array_fill(0, $width, array_fill(0, $height, WS_UNSET));
for ($n=0; $n<$bombs; $n++) {
while (WS_BOMB == $field[$rw = rand(0, $width - 1)][$rh = rand(0, $height - 1)])
; # already a bomb, retry. For a low proportion of bombs, this method is fast.
$field[$rw][$rh] = WS_BOMB;
}
return $field;
}
function neighbours($w, $h, $max_w, $max_h) {
$result = array();
if (($w >= 1) && ($h >= 1))
$result[] = array($w-1, $h-1);
if ($h >= 1)
$result[] = array($w, $h-1);
if (($w <= $max_w-1) && ($h >= 1))
$result[] = array($w+1, $h-1);
if ($w >= 1)
$result[] = array($w-1, $h);
if ($w <= $max_w-1)
$result[] = array($w+1, $h);
if (($w >= 1) && ($h <= $max_h-1))
$result[] = array($w-1, $h+1);
if ($h <= $max_h-1)
$result[] = array($w, $h+1);
if (($w <= $max_w-1) && ($h <= $max_h-1))
$result[] = array($w+1, $h+1);
return $result;
}
function mark_empty_box($w, $h, $tag, &$grid) {
$max_w = count($grid) - 1;
$max_h = count($grid[0]) - 1;
$grid[$w][$h] = $tag;
foreach (neighbours($w, $h, $max_w, $max_h) as $neighbour) {
foreach (explode(' ', $grid[$neighbour[0]][$neighbour[1]]) as $value) {;
if ('0' === $value) {
mark_empty_box($neighbour[0], $neighbour[1], $tag, $grid);
} elseif (in_array($value, array(1,2,3,4,5,6,7,8))) {
$grid[$neighbour[0]][$neighbour[1]] .= " beach-$tag";
}
}
}
}
function count_adjacent_bombs($grid, $w, $h) {
$count = 0;
$wmax = count($grid);
$hmax = count($grid[0]);
foreach (neighbours($w, $h, $wmax, $hmax) as $neighbour)
if (WS_BOMB == $grid[$neighbour[0]][$neighbour[1]])
$count++;
return $count;
}
function fill_numbers(&$grid) {
for ($h=0; $h<count($grid[0]); $h++) {
for ($w=0; $w<count($grid); $w++) {
if (WS_BOMB != $grid[$w][$h]) {
$grid[$w][$h] = count_adjacent_bombs($grid, $w, $h);
}
}
}
for ($h=0; $h<count($grid[0]); $h++) {
for ($w=0; $w<count($grid); $w++) {
if (0 === $grid[$w][$h]) {
mark_empty_box($w, $h, "blank-$w-$h", $grid);
}
}
}
}
function generate_html_table($grid) {
echo "<form><table>\n";
for ($h=0; $h<count($grid[0]); $h++) {
echo " <tr>\n";
for ($w=0; $w<count($grid); $w++) {
$additional = '';
foreach (explode(' ', $grid[$w][$h]) as $value) {
if (substr($value, 0, 6) == 'blank-') {
$contents = ' ';
$html_class = 'count count-0';
$target = $value;
} elseif (WS_BOMB == $value) {
$contents = 'x';
$html_class = 'bomb';
$target = 'game';
} elseif ('beach-' == substr($value, 0, 6)) {
$additional .= '<a class="beach" href="#'.substr($value,6).'">.</a>';
} else { // normal number
$contents = $value;
$html_class = "count count-$value";
$target = "x$w-y$h";
}
}
echo " <td class=\"$html_class\">$additional<a class=\"info\" href=\"#$target\"><span>".$contents."</span></a><input type=\"checkbox\" /><span class=\"flag\"></span></td>\n";
}
echo " </tr>\n";
}
echo "</table></form>\n";
}
$redirect = 0;
if (!(($width = $_GET['w']) && ($height = $_GET['h']))) {
$width = 9;
$height = 9;
$_GET['n'] = 10;
$redirect = 1;
}
if (!($gameid = $_GET['gameid'])) {
srand((double)microtime()*1000000);
$gameid = rand();
$redirect = 1;
}
$bombs = ($_GET['n'] ? $_GET['n'] : round($width*$height/5.5));
if ($redirect) {
header("Location: ?w=$width&h=$height&n=$bombs&gameid=$gameid");
} else {
$grid = generate_grid($width, $height, $bombs);
fill_numbers($grid);
?><!DOCTYPE html>
<html>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="style/win95/img/mine.png" type="image/png" />
<link rel="stylesheet" href="style/base.css" />
<link rel="stylesheet" title="Windows 95" href="style/win95/win95.css" />
<link rel="stylesheet" title="Matrix (work in progress)" href="style/matrix/matrix.css" />
<!--[if lte IE 8]>
<script src="html5-ie.js" type="text/javascript"></script>
<![endif]-->
<title>Minesweeper, game #<?php echo "$gameid ($width"."x$height, $bombs mines = ".round($bombs*100/$width/$height)."%)" ?></title>
<body id="top">
<article class="dialog" id="intro">
<header><a class="close button" href="#top"><span>OK...</span></a>
<h1>Introduction</h1></header>
<h2>Find the mines!</h2>
<p>There are <?php echo $bombs;?> mines, you'll need to click around... but don't click on the mines!</p>
<p>As an indication, the numbers tell you <strong>how many mines</strong> there are in the 8 adjacent cells.</p>
<p>When you've guessed the location of a mine, click on the little white square to put a <strong>flag</strong> on the desired cell. The red counter tells
you how many mines are left to find.</p>
<p>To start a new game, either click on the smiley face or choose a difficulty level in the <em>Game</em> menu.</p>
<p><a href="#about">More information about the game...</a></p>
</article>
<article id="game">
<header>
<a title="Minesweeper" href="." rel="sidebar" class="minimize button"><span title="Bookmark CSS Minesweeper">Bookmark to play in the sidebar</span></a>
<h1>Minesweeper</h1>
</header>
<ul id="menu">
<li>Game
<ul><li><a href="?w=9&h=9&n=10" title="9x9, 10 mines">Beginner</a></li>
<li><a href="?w=16&h=16&n=40" title="16x16, 40 mines">Intermediate</a></li>
<li><a href="?w=30&h=16&n=99" title="30x16, 99 mines">Expert</a></li></ul>
</li>
<li>Help
<ul>
<li><a href="#intro">How to play?</a></li>
<li><a href="http://da.weeno.net/blog/?post/2010/01/29/Comment-miner-son-apr%C3%A8s-midi">Présentation en français (blog)</a></li>
<li><a href="TODO">Want to help?</a></li>
<li><a href="#about">About CSS Minesweeper...</a></li>
</ul>
</li>
</ul>
<?php
echo "<a href=\"#game\" id=\"lose\"><span>Go to top.</span></a>";
echo "<p id=\"controls\">";
echo "<div id=\"timer\"><div id=\"timer-1\"></div><div id=\"timer-2\"></div><div id=\"timer-3\"></div></div>";
echo "<a accesskey=\"N\" title=\"Start new game\" id=\"new\" href=\"?w=$width&h=$height&n=$bombs&gameid=".rand()."\"><span>Start <em>n</em>ew game</span></a></p>\n";
generate_html_table($grid);
echo "</article>\n";
include('README.html');
echo "</body>\n</html>";
}
?>