Permalink
Browse files

Merge pull request #12 from ianmonge/master

Solución PHP
  • Loading branch information...
2 parents e1cb6c3 + 088bd8c commit ac0517991f230727269044bbc0f3ba547eccbc96 @12meses12katas committed Aug 29, 2011
Showing with 9,114 additions and 0 deletions.
  1. +12 −0 ianmonge/src/Bootstrap.php
  2. +122 −0 ianmonge/src/Field.php
  3. +66 −0 ianmonge/src/Minesweeper.php
  4. +39 −0 ianmonge/src/SourceFile.php
  5. +21 −0 ianmonge/src/SourceInterface.php
  6. +12 −0 ianmonge/src/SquareAbstract.php
  7. +17 −0 ianmonge/src/SquareBomb.php
  8. +42 −0 ianmonge/src/SquareSimple.php
  9. +20 −0 ianmonge/src/index.php
  10. +10 −0 ianmonge/src/input.dat
  11. +3 −0 ianmonge/tests/TestHelper.php
  12. +382 −0 ianmonge/tests/log/report/Field.php.html
  13. +257 −0 ianmonge/tests/log/report/Minesweeper.php.html
  14. +1,653 −0 ianmonge/tests/log/report/RGraph.bar.js
  15. +2,454 −0 ianmonge/tests/log/report/RGraph.common.core.js
  16. +502 −0 ianmonge/tests/log/report/RGraph.common.tooltips.js
  17. +1,168 −0 ianmonge/tests/log/report/RGraph.scatter.js
  18. +230 −0 ianmonge/tests/log/report/SourceFile.php.html
  19. +185 −0 ianmonge/tests/log/report/SquareBomb.php.html
  20. +256 −0 ianmonge/tests/log/report/SquareSimple.php.html
  21. BIN ianmonge/tests/log/report/butter.png
  22. BIN ianmonge/tests/log/report/chameleon.png
  23. BIN ianmonge/tests/log/report/close12_1.gif
  24. +19 −0 ianmonge/tests/log/report/container-min.js
  25. +324 −0 ianmonge/tests/log/report/container.css
  26. BIN ianmonge/tests/log/report/directory.png
  27. BIN ianmonge/tests/log/report/excanvas.compressed.js
  28. BIN ianmonge/tests/log/report/file.png
  29. BIN ianmonge/tests/log/report/glass.png
  30. +86 −0 ianmonge/tests/log/report/index.dashboard.html
  31. +260 −0 ianmonge/tests/log/report/index.html
  32. BIN ianmonge/tests/log/report/scarlet_red.png
  33. BIN ianmonge/tests/log/report/snow.png
  34. +459 −0 ianmonge/tests/log/report/style.css
  35. +14 −0 ianmonge/tests/log/report/yahoo-dom-event.js
  36. +15 −0 ianmonge/tests/phpunit.xml
  37. +156 −0 ianmonge/tests/src/FieldTest.php
  38. +124 −0 ianmonge/tests/src/MinesweeperTest.php
  39. +62 −0 ianmonge/tests/src/SourceFileTest.php
  40. +10 −0 ianmonge/tests/src/SourceFileTestInput.dat
  41. +38 −0 ianmonge/tests/src/SquareBombTest.php
  42. +96 −0 ianmonge/tests/src/SquareSimpleTest.php
View
@@ -0,0 +1,12 @@
+<?php
+
+/**
+ * Configuration of the environment.
+ */
+error_reporting( E_ALL | E_STRICT );
+ini_set( 'display_errors', true );
+
+/**
+ * Defines of the constants.
+ */
+define( 'PATH_BASE', realpath( dirname( __FILE__ ) ) );
View
@@ -0,0 +1,122 @@
+<?php
+
+require_once 'SquareBomb.php';
+require_once 'SquareSimple.php';
+
+class Field
+{
+ /**
+ * Board of the field.
+ *
+ * @var array
+ */
+ protected $board = array();
+
+ /**
+ * Number of columns.
+ *
+ * @var integer
+ */
+ protected $colsNum = 0;
+
+ /**
+ * Number of rows.
+ *
+ * @var integer
+ */
+ protected $rowsNum = 0;
+
+ /**
+ * Contructs a field with the indicated size.
+ *
+ * @param integer $rowsNum
+ * @param integer $colsNum
+ */
+ public function __construct( $rowsNum, $colsNum )
+ {
+ $this->rowsNum = $rowsNum;
+ $this->colsNum = $colsNum;
+
+ for( $row = 0; $row < $this->rowsNum; $row++ )
+ {
+ for( $col = 0; $col < $this->colsNum; $col++ )
+ {
+ $this->board[ $row ][ $col ] = new SquareSimple;
+ }
+ }
+ }
+
+ /**
+ * Sets a square bomb in an indicated position.
+ *
+ * @param integer $row
+ * @param integer $col
+ */
+ public function setSquareBomb( $row, $col )
+ {
+ $this->board[ $row ][ $col ] = new SquareBomb();
+
+ $this->updateSquaresNear( $row, $col );
+ }
+
+ /**
+ * Update the number of bombs near to the indicated square.
+ *
+ * @param integer $row
+ * @param integer $col
+ */
+ protected function updateSquaresNear( $row, $col )
+ {
+ for( $r = $row-1; $r < $row+2; $r++ )
+ {
+ for( $c = $col-1; $c < $col+2; $c++ )
+ {
+ $this->updateSquare( $r, $c );
+ }
+ }
+ }
+
+ /**
+ * Update the number of bombs of the indicated square, if it exists and
+ * it is not a bomb square.
+ *
+ * @param integer $row
+ * @param integer $col
+ */
+ protected function updateSquare( $row, $col )
+ {
+ // If the square exists.
+ if ( array_key_exists( $row, $this->board )
+ && array_key_exists( $col, $this->board[ $row ] )
+ )
+ {
+ $square = $this->board[ $row ][ $col ];
+
+ // If the square is not a bomb.
+ if ( $square instanceof SquareSimple )
+ {
+ $square->incrementNumOfBombs();
+ }
+ }
+ }
+
+ /**
+ * Prints the solution of the field.
+ */
+ public function __toString()
+ {
+ $output = '';
+ for( $row = 0; $row < $this->rowsNum; $row++ )
+ {
+ for( $col = 0; $col < $this->colsNum; $col++ )
+ {
+ $square = $this->board[ $row ][ $col ];
+ $output .= $square->__toString();
+ }
+
+ $output .= PHP_EOL;
+ }
+
+ return $output;
+ }
+}
@@ -0,0 +1,66 @@
+<?php
+
+require_once 'Field.php';
+
+/**
+ * Class Minesweeper that resolves fields.
+ */
+class Minesweeper
+{
+ /**
+ * Set of Fields.
+ *
+ * @var array
+ */
+ protected $fields = array();
+
+ /**
+ * Sets the data from the source.
+ *
+ * @param array $data
+ */
+ public function setData( array $data )
+ {
+ $line = 0;
+
+ while ( '0 0' != $data[ $line ] )
+ {
+ list( $rowsNum, $colsNum ) = explode( ' ', $data[ $line ] );
+ $field = new Field( $rowsNum, $colsNum );
+ $this->fields[] = $field;
+
+ $line++;
+ for ( $row = 0; $row < $rowsNum; $row++ )
+ {
+ for ( $col = 0; $col < $colsNum; $col++ )
+ {
+ if ( '*' == $data[ $line ][ $col ] )
+ {
+ $field->setSquareBomb( $row, $col );
+ }
+ }
+ $line++;
+ }
+ }
+ }
+
+ /**
+ * Prints the solutions of the fields.
+ *
+ *
+ */
+ public function printSolutions()
+ {
+ $output = '';
+
+ foreach( $this->fields as $index => $field )
+ {
+ $output .= 'Field #' . ( $index + 1 ) . ':' . PHP_EOL;
+ $output .= $field->__toString() . PHP_EOL;
+ }
+
+ $output = substr( $output, 0, -1 );
+
+ return $output;
+ }
+}
@@ -0,0 +1,39 @@
+<?php
+
+require_once PATH_BASE . '/SourceInterface.php';
+
+/**
+ * Class for extract the data from a source file.
+ */
+class SourceFile implements SourceInterface
+{
+ /**
+ * Filename of the source.
+ *
+ * @var string
+ */
+ protected $source;
+
+ /**
+ * Sets the source filename.
+ *
+ * @param mixed $source
+ */
+ public function setSource( $source )
+ {
+ $this->source = $source;
+ }
+
+ /**
+ * Returns the data to the Minesweeper.
+ *
+ * @return array
+ */
+ public function getData()
+ {
+ $fileContent = file( $this->source, FILE_IGNORE_NEW_LINES );
+
+ return $fileContent;
+ }
+
+}
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * Interface for the Source classes.
+ */
+interface SourceInterface
+{
+ /**
+ * Sets the source.
+ *
+ * @param mixed $source
+ */
+ public function setSource( $source );
+
+ /**
+ * Returns the data to the Minesweeper.
+ *
+ * @return array
+ */
+ public function getData();
+}
@@ -0,0 +1,12 @@
+<?php
+
+/**
+ * Abstract class of a square.
+ */
+abstract class SquareAbstract
+{
+ /**
+ * Prints the representation of the square.
+ */
+ abstract public function __toString();
+}
@@ -0,0 +1,17 @@
+<?php
+
+require_once PATH_BASE . '/SquareAbstract.php';
+
+/**
+ * Class of a square that is a bomb.
+ */
+class SquareBomb extends SquareAbstract
+{
+ /**
+ * Prints the representation of the square.
+ */
+ public function __toString()
+ {
+ return '*';
+ }
+}
@@ -0,0 +1,42 @@
+<?php
+
+require_once PATH_BASE . '/SquareAbstract.php';
+
+/**
+ * Class of a simple square that isn't a bomb.
+ */
+class SquareSimple extends SquareAbstract
+{
+ /**
+ * Number of bombs near the square.
+ *
+ * @var integer
+ */
+ protected $numBombs = 0;
+
+ /**
+ * Returns the number of bombs near the square.
+ *
+ * @return integer
+ */
+ public function getNumOfBombs()
+ {
+ return $this->numBombs;
+ }
+
+ /**
+ * Increment the number of bombs near the square.
+ */
+ public function incrementNumOfBombs()
+ {
+ $this->numBombs++;
+ }
+
+ /**
+ * Returns the representation of the square.
+ */
+ public function __toString()
+ {
+ return (string) $this->numBombs;
+ }
+}
View
@@ -0,0 +1,20 @@
+<?php
+
+/**
+ * Includes required files.
+ */
+require_once 'Bootstrap.php';
+require_once PATH_BASE . '/SourceFile.php';
+require_once PATH_BASE . '/Minesweeper.php';
+
+$filename = PATH_BASE . '/input.dat';
+
+$source = new SourceFile();
+$source->setSource( $filename );
+$data = $source->getData();
+
+$minesweeper = new Minesweeper();
+$minesweeper->setData( $data );
+echo $minesweeper->printSolutions();
+
+exit( 0 );
View
@@ -0,0 +1,10 @@
+4 4
+*...
+....
+.*..
+....
+3 5
+**...
+.....
+.*...
+0 0
@@ -0,0 +1,3 @@
+<?php
+
+require_once '../src/Bootstrap.php';
Oops, something went wrong.

0 comments on commit ac05179

Please sign in to comment.