<?php
class minechecker
{
/*
MineChecker 1.0.0 Copyright (C) 2005 Murat ATAY
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not,write to the
Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
var $errno = 0;
var $errmsg = NULL;
var $status = 0;
var $width; var $height; var $nofmines;
var $area;
function minechecker() {}
function autowincheck()
{
for ($i = 1; $i <= $this->height; $i++)
{
for ($j = 1; $j <= $this->width; $j++)
{
if ($this->area[$i][$j] > 0 && $this->area[$i][$j] != 9 && $this->area[$i][$j] != 90) return;
}
}
$this->status = 1;
}
function wincheck()
{
for ($i = 1; $i <= $this->height; $i++)
{
for ($j = 1; $j <= $this->width; $j++)
{
if ($this->area[$i][$j] == 9)
{
$this->status = -1;
return;
}
}
}
$this->status = 1;
}
function flagarea($x, $y)
{
$x = (int) $x; $y = (int) $y;
if ($x && $y)
{
if ($x >= 1 && $x <= $this->width && $y >= 1 && $y <= $this->height)
{
if ($this->area[$y][$x] == 0)
{
$this->area[$y][$x] = 100;
return;
}
elseif ($this->area[$y][$x] == 100)
{
$this->area[$y][$x] = 0;
return;
}
elseif ($this->area[$y][$x] > 0 && $this->area[$y][$x] < 10)
{
$this->area[$y][$x] = $this->area[$y][$x] * 10;
return;
}
elseif ($this->area[$y][$x] >= 10)
{
$this->area[$y][$x] = $this->area[$y][$x] / 10;
return;
}
}
}
}
function openarea($x, $y)
{
$x = (int) $x; $y = (int) $y;
if ($x && $y)
{
if ($x >= 1 && $x <= $this->width && $y >= 1 && $y <= $this->height)
{
if ($this->area[$y][$x] == 9 || $this->area[$y][$x] == 90)
{
$this->area[$y][$x] = -9;
$this->status = -1;
return;
}
elseif ($this->area[$y][$x] >= 0)
{
$this->openbox($x, $y);
$this->autowincheck();
}
}
}
}
function openbox($x, $y)
{
if (!($x >= 1 && $x <= $this->width && $y >= 1 && $y <= $this->height)) return;
if ($this->area[$y][$x] < 0 || $this->area[$y][$x] == 9 || $this->area[$y][$x] == 90)
{
return;
}
elseif ($this->area[$y][$x] > 0 && $this->area[$y][$x] != 100)
{
$this->area[$y][$x] = $this->area[$y][$x] * -1;
return;
}
else
{
$this->area[$y][$x] = -10;
$this->openbox($x - 1, $y);
$this->openbox($x - 1, $y - 1);
$this->openbox($x - 1, $y + 1);
$this->openbox($x + 1, $y);
$this->openbox($x + 1, $y - 1);
$this->openbox($x + 1, $y + 1);
$this->openbox($x, $y - 1);
$this->openbox($x, $y + 1);
return;
}
}
function loadarea($area)
{
$this->area = $area;
$this->height = count($area);
$this->width = count($area[1]);
for ($i = 1; $i <= $this->height; $i++)
{
for ($j = 1; $j <= $this->width; $j++)
{
if ($this->area[$i][$j] == 9) $this->nofmines++;
}
}
}
function newarea($width, $height, $nofmines)
{
$this->width = (int) $width; $this->height = (int) $height; $this->nofmines = (int) $nofmines;
if ($this->nofmines > ($this->width * $this->height))
{
$this->errno = -1; $this->errmsg = 'Check mine number.'; return;
}
for ($i = 1; $i <= $this->height; $i++)
{
for ($j = 1; $j <= $this->width; $j++)
{
$this->area[$i][$j] = 0;
}
}
$i = 0;
while ($i < $this->nofmines)
{
$x = rand(1, $this->width); $y = rand(1, $this->height);
if ($this->area[$y][$x] != 9)
{
$this->area[$y][$x] = 9;
if ($x > 1)
{
if ($this->area[$y][$x - 1] != 9) $this->area[$y][$x - 1]++;
if ($y > 1)
{
if ($this->area[$y - 1][$x - 1] != 9) $this->area[$y - 1][$x - 1]++;
}
if ($y < $this->height)
{
if ($this->area[$y + 1][$x - 1] != 9) $this->area[$y + 1][$x - 1]++;
}
}
if ($x < $this->width)
{
if ($this->area[$y][$x + 1] != 9) $this->area[$y][$x + 1]++;
if ($y > 1)
{
if ($this->area[$y - 1][$x + 1] != 9) $this->area[$y - 1][$x + 1]++;
}
if ($y < $this->height)
{
if ($this->area[$y + 1][$x + 1] != 9) $this->area[$y + 1][$x + 1]++;
}
}
if ($y > 1)
{
if ($this->area[$y - 1][$x] != 9) $this->area[$y - 1][$x]++;
}
if ($y < $this->height)
{
if ($this->area[$y + 1][$x] != 9) $this->area[$y + 1][$x]++;
}
$i++;
}
}
}
}
?>