Permalink
Browse files

solution PHP completed

  • Loading branch information...
1 parent fdb84d4 commit 9e494814ef84c2e83ec271aa897c13c6a869f770 @ahurt2000 ahurt2000 committed Aug 17, 2011
View
@@ -0,0 +1,141 @@
+<?php
+
+require_once dirname(__FILE__) . '/src/Field.php';
+
+class Exercise
+{
+ private $fields;
+ private $debug;
+
+ public function __construct(){
+ $this->fields = array();
+ }
+
+ private function getFieldDimensions($line)
+ {
+ list($n, $m) = explode(' ', $line); //inverted read
+ return array($m, $n);
+ }
+
+ private function IsTheEnd($m,$n)
+ {
+ return (($m + $n) == 0);
+ }
+
+ private function isValidDim($m, $n)
+ {
+ return ($m > 0 && $m <= 100 && $n > 0 );
+ }
+
+ private function isNewField($line)
+ {
+ return ( strlen(trim($line)) == 3 ) ;
+ }
+
+ public function process($input)
+ {
+ $id = 0;
+ $minesAll = array();
+ while(count($input)>0)
+ {
+ $line = array_shift($input);
+
+ if($this->isNewField($line))
+ {
+ $dim = $this->getFieldDimensions($line);
+ $m = $dim[0];
+ $n = $dim[1];
+ if(!$this->IsTheEnd($m, $n))
+ {
+ if(!$this->isValidDim($m, $n)){
+ throw new Exception ("Error: Invalid field dimensions! $m, $n");
+ }
+ ++$id;
+ $lineNum = 0;
+ $field = new Field($id, $m, $n);
+ $field->setDebug($this->debug);
+ $this->fields[$id] = $field;
+ }
+ }
+ else
+ {
+ if( count($this->fields)<=0 ){
+ throw new Exception ("Error something wrong\n");
+ }
+ $field = $this->getField($id);
+ $id = $field->getFieldId();
+
+ /* line data process */
+ foreach(str_split($line) as $x=>$cellvalue)
+ {
+ $field->addCell($x, $lineNum);
+ /* detecting mines */
+ if($this->isMinedCell($cellvalue)){
+ $minesAll[$id][] = array($x, $lineNum);
+ }
+ }
+ $this->fields[$id] = $field;
+ $lineNum++;
+ }
+ }
+
+ if(count($minesAll)>0)
+ {
+ foreach($minesAll as $id=>$mines)
+ {
+ $field = $this->getField($id);
+ $field = $this->MineField($field, $mines);
+ }
+
+ }
+
+ return $this->getOutput();
+ }
+
+ private function getField($id)
+ {
+ return $this->fields[$id];
+ }
+
+ private function MineField($field, $mines)
+ {
+ foreach($mines as $cord)
+ {
+ $field->mineCell($cord[0], $cord[1]);
+ }
+ return $field;
+ }
+
+
+ public function getOutput()
+ {
+ $out = array();
+ foreach ($this->fields as $field)
+ {
+ $out = array_merge($out,$field->printField());
+ $out[]='';
+ }
+
+ return $out;
+ }
+
+ private function isMinedCell($value){
+ $out = false;
+ switch ($value){
+ case '*':
+ // mined cell
+ $out = true;
+ break;
+ case '.':
+ // normal cell
+ break;
+ default:
+ throw new Exception("Cell content error");
+ }
+ return $out;
+ }
+
+ public function setDebug($value) {
+ $this->debug = $value;
+ }
+}
View
@@ -0,0 +1,99 @@
+Minesweeper
+===========
+
+Idea
+----
+
+Each cell mined notifies its neighbors.
+
+tests:
+------
+
+- Each cell is able to return their neighboring cells.
+*CellTest-->**ok** *
+
+- Cada campo genera es resuelto
+*FieldTest---**ok** *
+
+- Testing a program
+*ExerciseTest---**ok** *
+
+
+Outs
+---
+
+# php -f index.php
+
+
+
+
+
+
+----------------------
+About this Kata
+Kata originated here: acm.uva.es/p/v101/10189.html
+
+Difficulty: Easy
+
+Problem Description
+
+Have you ever played Minesweeper? It's a cute little game which comes within a certain Operating System whose name we can't really remember. Well, the goal of the game is to find all the mines within an MxN field. To help you, the game shows a number in a square which tells you how many mines there are adjacent to that square. For instance, take the following 4x4 field with 2 mines (which are represented by an * character):
+
+*...
+....
+.*..
+....
+The same field including the hint numbers described above would look like this:
+*100
+2210
+1*10
+1110
+You should write a program that takes input as follows:
+The input will consist of an arbitrary number of fields.
+The first line of each field contains two integers n and m (0 < n,m <= 100)
+which stands for the number of lines and columns of the field respectively.
+The next n lines contains exactly m characters and represent the field.
+Each safe square is represented by an "." character (without the quotes) and
+each mine square is represented by an "*" character (also without the quotes).
+The first field line where n = m = 0 represents the end of input and should not be processed.
+
+Your program should produce output as follows:
+
+For each field, you must print the following message in a line alone:
+
+Field #x:
+
+Where x stands for the number of the field (starting from 1).
+The next n lines should contain the field with the "." characters replaced by the number of adjacent mines to that square. There must be an empty line between field outputs.
+
+Clues
+
+As you may have already noticed, each square may have at most 8 adjacent squares.
+
+Suggested Test Cases
+
+This is the acceptance test input:
+
+4 4
+*...
+....
+.*..
+....
+3 5
+**...
+.....
+.*...
+0 0
+and output:
+Field #1:
+*100
+2210
+1*10
+1110
+
+Field #2:
+**100
+33200
+1*100
+
+Info: http://codingdojo.org/cgi-bin/wiki.pl?KataMinesweeper
View
@@ -0,0 +1,26 @@
+<?php
+require_once dirname(__FILE__) . '/Exercise.php';
+
+$inputs = array(
+'4 4',
+'*...',
+'....',
+'.*..',
+'....',
+'3 5',
+'**...',
+'.....',
+'.*...',
+'0 0',
+ );
+// mines (0,0) (1,2)
+// mines (0,0) (1,0) (1,2)
+$fields = array();
+
+
+$e = new Exercise();
+$e->setDebug(false);
+
+$out = $e->process($inputs);
+
+echo implode("\n",$out);
View
@@ -0,0 +1,106 @@
+<?php
+
+/**
+ * CellField class
+ *
+ * @author ahurt2000
+ */
+class CellField {
+
+ private $x;
+ private $y;
+ private $mined;
+ private $danger;
+ private $debug;
+
+ public function __construct($x, $y) {
+ $this->setPosition($x, $y);
+ $this->setDanger(0);
+ }
+
+ public function setPosition($x, $y) {
+ $this->setX($x);
+ $this->setY($y);
+ }
+
+ public function setX($value) {
+ $this->x = $value;
+ }
+
+ public function setY($value) {
+ $this->y = $value;
+ }
+
+ public function setDebug($value)
+ {
+ $this->debug = $value;
+ }
+
+ public function getX() {
+ return $this->x;
+ }
+
+ public function getY() {
+ return $this->y;
+ }
+
+ public function getDanger() {
+ return $this->danger;
+ }
+
+ private function setDanger($value) {
+ $this->danger = $value;
+ }
+
+ public function addDanger() {
+ $this->danger++;
+ }
+
+ public function printCell()
+ {
+ if($this->debug)
+ {
+ return $this->getX()."-".$this->getY()." ";
+ }
+ if($this->isMined()){
+ return "*";
+ }else{
+ return $this->danger;
+ }
+ }
+
+ /**
+ * returns to neighboring cells.
+ *
+ * @param integer $m x boundary
+ * @param integer $n y boundary
+ * @return array()
+ */
+ public function getNeighbor($m, $n) {
+ $ret = array();
+ for ($j = -1; $j <= 1; $j++) {
+ for ($i = -1; $i <= 1; $i++) {
+ $x = $this->x + $i;
+ $y = $this->y + $j;
+ if ( !($x == $this->x && $y == $this->y)
+ && ( $x >= 0 ) && ( $y >= 0 )
+ && ( $x < $m ) && ( $y < $n )
+ ) {
+ $ret[] = array($x, $y);
+ }
+ }
+ }
+ return $ret;
+ }
+
+ public function mine() {
+ $this->mined = true;
+ }
+
+ public function isMined() {
+ return $this->mined;
+ }
+
+}
+
+?>
Oops, something went wrong.

0 comments on commit 9e49481

Please sign in to comment.