print "<html>";
print "<head>";
print "<title>Minesweeper</title>";
?>
<style type="text/css">
<!--
.andy { color: #FF0000; background-color: #CCCCCC; background-repeat: no-repeat; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; list-style-position: outside; list-style-type: circle; cursor: hand}
-->
</style>
<?php
print "</head>";
print "<body><center>";
print "<font size=4 face=Verdana><b>Minesweeper</b><br><font size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=mailto:[email protected]>Andy Wang</a></font>";

if ($submit=="") {
$NumMine=4;
$RowSize=5;
$ColSize=5;
$generer=1;
}

if ($generer==1) {
srand((double)microtime()*100000000);
$time_start=time();
if (($RowSize<=1) || ($ColSize<=1) || ($NumMine==0)) {
print "<p><br><font size=-1 color=red>Error:number of Rows or Columns or mini</font>";
exit; }
if ($NumMine > $RowSize*$ColSize) {
print "<p><br><font size=-1 color=red>Error:number of mini too much</font>";
exit; }

for ($Row=0;$Row<$RowSize;$Row++) {
for ($Col=0;$Col<$ColSize;$Col++) {
$Mine[$Row][$Col]="0";
$Decouv[$Row][$Col]="0"; } }

$index=0;
while ($index<$NumMine) {
srand;
$Row=rand(0,$RowSize);
$Col=rand(0,$ColSize);
if ($Mine[$Row][$Col]=="0") {
$Mine[$Row][$Col]="1";
$index++; }
}
}
else {
$perdu=0;
$reste=$RowSize*$ColSize;
$sss=$reste;
for ($Row=0;$Row<$RowSize;$Row++) {
for ($Col=0;$Col<$ColSize;$Col++) {
if ($Decouv[$Row][$Col]=="1") {$reste=$reste-1;}
if ($submit[$Row][$Col]=="ok") {
$reste=$reste-1;
if ($Mine[$Row][$Col]=="0") {
$nearmininum=0;
for ($iii=-1; $iii<=1; $iii++) {
for ($jjj=-1; $jjj<=1; $jjj++) {
if ($Mine[$Row+$iii][$Col+$jjj] == "1") {
$nearmininum++; } } }
if($nearmininum==0){
for ($iiii=$Row-1; $iiii<=$Row+1; $iiii++) {
for ($jjjj=$Col-1; $jjjj<=$Col+1; $jjjj++) {
$Decouv[$iiii][$jjjj]="1";} }
}
else{$Decouv[$Row][$Col]="1";}
}
else {
$perdu=1; } } }
}
if ($perdu==1) {
print "<h2><font color=red>YOU LOST</font></h2>";
for ($i=0;$i<$RowSize;$i++) {
for ($j=0;$j<$ColSize;$j++) {
$Decouv[$i][$j]="1"; } }
}
if (($reste==$NumMine)&&($perdu!=1)) {
print "<h2><font color=red>YOU WIN</font></h2>";
$time_stop=time();
$time=$time_stop-$time_start;
print "<p><font size=-1><i>Your Time: <font color=red>$time</font>s</i></font>";
for ($i=1;$i<=$RowSize;$i++) {
for ($j=1;$j<=$ColSize;$j++) {
$Decouv[$i][$j]="1"; } } }
}
print "<form method=post action=mini.php3>";
print "<input type=hidden name=time_start value=$time_start>";
print "<input type=hidden name=NumMine value=$NumMine>";
print "<input type=hidden name=RowSize value=$RowSize>";
print "<input type=hidden name=ColSize value=$ColSize>";
print "<input type=hidden name=submit value=ok>";
print "<p><table border=0 cellpadding=8 bgcolor=CCCCCC>";

for ($Row=0; $Row<$RowSize; $Row++) {
print "<tr>";
for ($Col=0; $Col<$ColSize; $Col++) {
$nearmini=0;
for ($i=-1; $i<=1; $i++) {
for ($j=-1; $j<=1; $j++) {
if ($Mine[$Row+$i][$Col+$j] == "1") {
$nearmini++;
}
}
}
print "<td width=15 height=15 align=center valign=middle>";
if ($Decouv[$Row][$Col]=="1") {
if ($nearmini==0) {
print "<font color=3333ff>0</font>";
}
else {
if ($Mine[$Row][$Col]=="1") {
print "<font color=red>*</font>"; }
else { print "<font color=3333ff>$nearmini</font>"; }
}
}
else { print "<input type=submit name=submit[$Row][$Col] value=ok class=andy>";
}
print "<input type=hidden name=Mine[$Row][$Col] value=".$Mine[$Row][$Col].">";
print "<input type=hidden name=Decouv[$Row][$Col] value=".$Decouv[$Row][$Col].">";
print "</td>";
}
print "</tr>";
}
print "</table>";
print "</form>";
?>

<hr>
<form method=post>
Rows:
<input type=text name=RowSize size=2 value=5>
&nbsp; &nbsp; &nbsp;
Columns:
<input type=text name=ColSize size=2 value=5>
&nbsp; &nbsp; &nbsp;
Mini:
<input type=text name=NumMine size=2 value=5>
<p>
<input type=submit name=submit value=Minisweeper>
<input type=hidden name=generer value=1>
</form>
</body>
</html>