@ 2014-08-31T23:42:25Z <?php
class MineSweeper{
private $data = NULL;
private $map;
function debug(){
header('Content-Type:text/plain');
}
function createMap($data){
$this->data = $data;
if(!preg_match('/^[\*\.\r\n]+$/', $this->data)){
throw new Exception('Invalid input provided. Only asterik (*) and/or dot (.) is allowed.');
}
// breaking input to lines
$lines = preg_split('/\r\n/', $this->data);
$tmp_line = NULL; // holding previous line for comparsion
// lines index
foreach ($lines as $lnum=>$line){
// lines should be equal in length
if ($tmp_line != NULL && strlen($tmp_line) != strlen($line) ){
throw new Exception('Invalid input provided, please correct the format. Not all rows are equal.');
}else {
$tmp_line = $line;
// removing back spac and line break and spliting into charatcers (means multi dimension array)
$lines[$lnum] = str_split(str_replace('\r\n','', $line));
}
}
// N= lines
// M = Characters
$M = count($lines[0]);
$this->map = new MineMap($lines);
}
function display(){
$this->map->display();
}
}
class MineMap{
private $lines = array();
private $N;
private $M;
function __construct($lines){
$this->lines = $lines;
$this->N = count($lines);
$this->M = count($lines[0]);
foreach($this->lines as $lnum => $line){
$this->lines[$lnum] = array_map(function($str){
return (trim($str) == "*") ? trim($str) : 0;
}, $line);
}
foreach($this->lines as $lnum => $line){
foreach($line as $index => $str){
if ($str === "*"){
$this->mineFound($lnum, $index);
}
}
}
}
function incValue($n, $m){
if ($m < $this->M && $n < $this->N && $n >= 0 && $m >= 0){
$this->lines[$n][$m]++;
}
}
function mineFound($n, $m){
$this->incValue($n, $m+1);
$this->incValue($n, $m-1);
$this->incValue($n+1, $m-1);
$this->incValue($n+1, $m);
$this->incValue($n+1, $m+1);
$this->incValue($n-1, $m-1);
$this->incValue($n-1, $m);
$this->incValue($n-1, $m+1);
}
function display(){
echo "<table cellspacin='5px'>";
foreach ($this->lines as $line){
echo "<tr>";
foreach($line as $str){
echo "<td style=''><button data='$str' class='btn btn-primary'> </td>";
}
echo "</tr>";
}
echo "</table>";
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.7 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.10 , 7.2.0 - 7.2.33 , 7.3.16 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Output for 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /in/Re39j on line 66
Process exited with code 255 . Output for 5.0.0 - 5.0.5 Parse error: parse error, unexpected T_FUNCTION, expecting ')' in /in/Re39j on line 66
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Re39j on line 6
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Re39j on line 6
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Re39j on line 6
Process exited with code 255 . preferences:dark mode live preview
228.43 ms | 401 KiB | 311 Q