Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tested the class Field

  • Loading branch information...
commit 675e80f8dfbbeb05aa533eafe33973596021c9e1 1 parent 89a87af
@ianmonge authored
Showing with 58 additions and 8 deletions.
  1. +32 −6 ianmonge/src/Field.php
  2. +26 −2 ianmonge/tests/src/FieldTest.php
View
38 ianmonge/src/Field.php
@@ -55,7 +55,13 @@ public function setSquareBomb( $row, $col )
$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++ )
@@ -66,12 +72,18 @@ protected function updateSquaresNear( $row, $col )
}
}
}
-
+
+ /**
+ * 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 )
+ if ( array_key_exists( $row, $this->board )
&& array_key_exists( $col, $this->board[ $row ] )
)
{
@@ -88,6 +100,20 @@ protected function updateSquare( $row, $col )
/**
* Prints the solution of the field.
*/
- public function printSolution()
- {}
+ 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;
+ }
}
View
28 ianmonge/tests/src/FieldTest.php
@@ -121,10 +121,34 @@ public function testSetSquareBomb()
}
/**
- * Test the method printSolution().
+ * Test the method __toString().
*/
- public function testPrintSolution()
+ public function test__toString()
{
+ $output = $this->object->__toString();
+
+ $expected = <<<HEREDOC
+00000
+00000
+00000
+
+HEREDOC;
+
+ $this->assertEquals( $expected, $output );
+
+ $this->object->setSquareBomb( 0, 0 );
+ $this->object->setSquareBomb( 1, 1 );
+ $this->object->setSquareBomb( 1, 2 );
+ $output = $this->object->__toString();
+
+ $expected = <<<HEREDOC
+*3210
+2**10
+12210
+
+HEREDOC;
+
+ $this->assertEquals( $expected, $output );
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.