Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #7 from rmhdev/master

(php) Minesweeper
  • Loading branch information...
commit a3c936a54eeaed390e383a8a131b57c8730a24d0 2 parents 079c53e + b685590
@12meses12katas authored
View
102 rmhdev/php/Minesweeper.php
@@ -0,0 +1,102 @@
+<?php
+
+/**
+ * @author rmhdev
+ */
+class Minesweeper {
+
+ protected $minesweeperParameters;
+ protected $squares;
+
+ public function __construct(MinesweeperParameters $minesweeperParameters){
+ $this->minesweeperParameters = $minesweeperParameters;
+ $this->initializeSquares();
+ $this->setMineSquares();
+ $this->calculateDistances();
+ }
+
+ protected function initializeSquares(){
+ $this->squares = array();
+ for ($row = 0; $row < $this->getRows(); $row++){
+ for ($col = 0; $col < $this->getCols(); $col++){
+ $this->initializeSquare($row, $col);
+ }
+ }
+ }
+
+ protected function initializeSquare($row, $col){
+ if (!isset($this->squares[$row])){
+ $this->squares[$row] = array();
+ }
+ $this->squares[$row][$col] = 0;
+ }
+
+ protected function setMineSquares(){
+ foreach ($this->minesweeperParameters->getMines() as $mine){
+ list($row, $col) = $mine;
+ $this->squares[$row][$col] = MinesweeperParameters::MINE;
+ }
+ }
+
+ protected function calculateDistances(){
+ foreach ($this->squares as $row=>$valuesInRow){
+ foreach ($valuesInRow as $col=>$value){
+ if (!$this->isMineSquare($value)){
+ $this->updateDistancesForRowCol($row, $col);
+ }
+ }
+ }
+ }
+
+ protected function isMineSquareForRowCol($row, $col){
+ if (isset($this->squares[$row][$col])){
+ return $this->isMineSquare($this->squares[$row][$col]);
+ }
+ return false;
+ }
+
+ protected function isMineSquare($value){
+ return $this->minesweeperParameters->isMineSquare($value);
+ }
+
+ protected function updateDistancesForRowCol($row, $col){
+ $count = 0;
+ $count += $this->isMineSquareForRowCol($row - 1 , $col ) ? 1 : 0;
+ $count += $this->isMineSquareForRowCol($row - 1 , $col + 1 ) ? 1 : 0;
+ $count += $this->isMineSquareForRowCol($row , $col + 1 ) ? 1 : 0;
+ $count += $this->isMineSquareForRowCol($row + 1 , $col + 1 ) ? 1 : 0;
+ $count += $this->isMineSquareForRowCol($row + 1 , $col ) ? 1 : 0;
+ $count += $this->isMineSquareForRowCol($row + 1 , $col - 1 ) ? 1 : 0;
+ $count += $this->isMineSquareForRowCol($row , $col - 1 ) ? 1 : 0;
+ $count += $this->isMineSquareForRowCol($row - 1 , $col - 1 ) ? 1 : 0;
+
+ $this->squares[$row][$col] += $count;
+ }
+
+ public function getRows(){
+ return $this->minesweeperParameters->getRows();
+ }
+
+ public function getCols(){
+ return $this->minesweeperParameters->getCols();
+ }
+
+ public function renderRow($row){
+ return implode('', $this->squares[$row]);
+ }
+
+ public function renderFieldName(){
+ return sprintf("Field #%d:", $this->minesweeperParameters->getFieldId());
+ }
+
+ public function render(){
+ $value = $this->renderFieldName();
+ for ($row = 0; $row < $this->getRows(); $row++){
+ $value .= MinesweeperParameters::ROW_SEPARATOR;
+ $value .= $this->renderRow($row);
+ }
+
+ return $value;
+ }
+
+}
View
37 rmhdev/php/MinesweeperCollection.php
@@ -0,0 +1,37 @@
+<?php
+
+class MinesweeperCollection {
+
+ protected $minesweepers;
+ protected $count;
+
+ public function __construct($parameters){
+ $this->minesweepers = new ArrayIterator();
+ $fieldId = 1;
+ while (strlen($parameters) > 0){
+ $minesweeperParameters = new MinesweeperParameters($parameters);
+ if ($minesweeperParameters->getRows() > 0){
+ $minesweeperParameters->setFieldId($fieldId);
+ $minesweeper = new Minesweeper($minesweeperParameters);
+ $this->minesweepers->append($minesweeper);
+ $fieldId++;
+ }
+ $parameters = $minesweeperParameters->getUnusedParameters();
+
+ }
+ }
+
+ public function count(){
+ return $this->minesweepers->count();
+ }
+
+ public function render(){
+ $values = array();
+ foreach ($this->minesweepers as $minesweeper){
+ $values[] = $minesweeper->render();
+ }
+ $separator = MinesweeperParameters::FIELD_SEPARATOR . MinesweeperParameters::FIELD_SEPARATOR;
+
+ return implode($separator, $values);
+ }
+}
View
22 rmhdev/php/MinesweeperFactory.php
@@ -0,0 +1,22 @@
+<?php
+
+require_once 'Minesweeper.php';
+require_once 'MinesweeperParameters.php';
+require_once 'MinesweeperCollection.php';
+
+/**
+ * @author rmhdev
+ */
+class MinesweeperFactory {
+
+ public static function generateUnique($parameters){
+ $minesweeperParameters = New MinesweeperParameters($parameters);
+ $minesweeperParameters->setFieldId(1);
+ return new Minesweeper($minesweeperParameters);
+ }
+
+ public function generateCollection($parameters){
+ return new MinesweeperCollection($parameters);
+ }
+}
+
View
97 rmhdev/php/MinesweeperParameters.php
@@ -0,0 +1,97 @@
+<?php
+
+/**
+ * @author rmhdev
+ */
+class MinesweeperParameters {
+
+ const MINE = "*";
+ const COORD_SEPARATOR = " ";
+ const ROW_SEPARATOR = "\n";
+ const FIELD_SEPARATOR = "\n";
+
+ protected $cols;
+ protected $rows;
+ protected $mines;
+ protected $fieldId;
+ protected $unusedParameters;
+
+ public function __construct($parameters){
+ $this->setRowsColsFromParameters($parameters);
+ $this->setMinesFromParameters($parameters);
+ $this->calculateUnusedParameters($parameters);
+ }
+
+ protected function setRowsColsFromParameters($parameters){
+ list($rows, $cols) = $this->getRowsColsFromParameters($parameters);
+ $this->rows = (int)$rows;
+ $this->cols = (int)$cols;
+ }
+
+ protected function getRowsColsFromParameters($parameters){
+ $parameters = explode(self::ROW_SEPARATOR, $parameters);
+ return explode(self::COORD_SEPARATOR, $parameters[0]);
+ }
+
+ protected function getDefaultValuesInRowsFromParameters($parameters){
+ $parameters = explode(self::ROW_SEPARATOR, $parameters);
+ return array_slice($parameters, 1, $this->getRows());
+ }
+
+ protected function setMinesFromParameters($parameters){
+ $this->mines = new ArrayIterator();
+ $valuesInRows = $this->getDefaultValuesInRowsFromParameters($parameters);
+ foreach ($valuesInRows as $row=>$valuesInRow){
+ for ($col = 0; $col < strlen($valuesInRow); $col++){
+ if ($this->isMineSquare($valuesInRow[$col])){
+ $this->mines->append(array($row, $col));
+ }
+ }
+ }
+ }
+
+
+ public function getRows(){
+ return $this->rows;
+ }
+
+ public function getCols(){
+ return $this->cols;
+ }
+
+ /**
+ * @return ArrayIterator
+ */
+ public function getMines(){
+ return $this->mines;
+ }
+
+ public function isMineSquare($value){
+ return ($value === self::MINE);
+ }
+
+ public function setFieldId($fieldId){
+ $this->fieldId = $fieldId;
+ }
+
+ public function getFieldId(){
+ return $this->fieldId;
+ }
+
+ public function getUnusedParameters(){
+ return $this->unusedParameters;
+ }
+
+ protected function calculateUnusedParameters($parameters){
+ list($rows, $cols) = $this->getRowsColsFromParameters($parameters);
+ $sizeActualParameters =
+ $rows*$cols +
+ strlen(strval($rows)) + strlen(strval($cols)) +
+ $rows*strlen(self::ROW_SEPARATOR) +
+ strlen(self::COORD_SEPARATOR) +
+ strlen(self::FIELD_SEPARATOR);
+
+ $this->unusedParameters = substr($parameters, $sizeActualParameters);
+ }
+
+}
View
140 rmhdev/php/tests/MinesweeperTest.php
@@ -0,0 +1,140 @@
+<?php
+
+require_once '../MinesweeperFactory.php';
+
+/**
+ * Tests Minesweeper (12Meses12Katas, agosto 2011)
+ *
+ * @author rmhdev
+ */
+class MinesweeperTest extends PHPUnit_Framework_TestCase {
+
+ public function testFactorygenerateUniqueShouldReturnMinesweeperObject(){
+ $parameters = "1 1\n.";
+ $minesweeper = MinesweeperFactory::generateUnique($parameters);
+
+ $this->assertInstanceOf('Minesweeper', $minesweeper);
+ }
+
+ public function test1x1MinesweeperSizeShouldBe1x1(){
+ $parameters = "1 1\n.";
+ $minesweeper = MinesweeperFactory::generateUnique($parameters);
+
+ $this->assertEquals(1, $minesweeper->getRows());
+ $this->assertEquals(1, $minesweeper->getCols());
+ }
+
+ public function test2x3MinesweeperSizeShouldBe2x3(){
+ $parameters = "2 3\n...\n...";
+ $minesweeper = MinesweeperFactory::generateUnique($parameters);
+
+ $this->assertEquals(2, $minesweeper->getRows());
+ $this->assertEquals(3, $minesweeper->getCols());
+ }
+
+ public function testRenderRowInSafe1x1MinesweeperShouldReturnZero(){
+ $parameters = "1 1\n.";
+ $minesweeper = MinesweeperFactory::generateUnique($parameters);
+
+ $this->assertSame('0', $minesweeper->renderRow(0));
+ }
+
+ public function testRenderRowInSafe3x3MinesweeperShouldReturnZeros(){
+ $parameters = "3 3\n...\n...\n...";
+ $minesweeper = MinesweeperFactory::generateUnique($parameters);
+
+ $this->assertSame('000', $minesweeper->renderRow(0));
+ $this->assertSame('000', $minesweeper->renderRow(1));
+ $this->assertSame('000', $minesweeper->renderRow(2));
+ }
+
+ public function testRenderRowInMined1x1MinesweeperShouldReturnAsterisk(){
+ $parameters = "1 1\n*";
+ $minesweeper = MinesweeperFactory::generateUnique($parameters);
+
+ $this->assertSame('*', $minesweeper->renderRow(0));
+ }
+
+ public function testRenderRowInMined3x3MinesweeperShouldReturnCorrect(){
+ $parameters = "3 3\n*..\n...\n...";
+ $minesweeper = MinesweeperFactory::generateUnique($parameters);
+
+ $this->assertSame('*10', $minesweeper->renderRow(0));
+ $this->assertSame('110', $minesweeper->renderRow(1));
+ $this->assertSame('000', $minesweeper->renderRow(2));
+ }
+
+ public function testRenderRowInMined4x4MinesweeperShouldReturnCorrect(){
+ $parameters = "4 4\n*...\n....\n.*..\n....";
+ $minesweeper = MinesweeperFactory::generateUnique($parameters);
+
+ $this->assertSame('*100', $minesweeper->renderRow(0));
+ $this->assertSame('2210', $minesweeper->renderRow(1));
+ $this->assertSame('1*10', $minesweeper->renderRow(2));
+ $this->assertSame('1110', $minesweeper->renderRow(3));
+ }
+
+
+ public function testRenderFieldNameShouldReturnFieldName(){
+ $parameters = "1 1\n.";
+ $minesweeper = MinesweeperFactory::generateUnique($parameters);
+
+ $this->assertEquals("Field #1:", $minesweeper->renderFieldName());
+ }
+
+ public function testRenderShouldReturnCompleteString(){
+ $parameters = "1 1\n.";
+ $expected = "Field #1:\n0";
+ $minesweeper = MinesweeperFactory::generateUnique($parameters);
+
+ $this->assertEquals($expected, $minesweeper->render());
+ }
+
+ //multiple minesweeper
+ public function testFactorygenerateUniqueCollectionShouldReturnMinesweeperCollectionObject(){
+ $parameters = "1 1\n.";
+ $minesweeperCollection = MinesweeperFactory::generateCollection($parameters);
+
+ $this->assertInstanceOf('MinesweeperCollection', $minesweeperCollection);
+ }
+
+ public function testMinesweeperCollectionCountWithUniqueMinesweeperShouldReturn1(){
+ $parameters = "1 1\n.";
+ $minesweeperCollection = MinesweeperFactory::generateCollection($parameters);
+
+ $this->assertEquals(1, $minesweeperCollection->count());
+ }
+
+ public function testMinesweeperCollectionCountWithTwoMinesweepersShouldReturn2(){
+ $parameters = "2 3\n...\n...\n2 3\n...\n...";
+ $minesweeperCollection = MinesweeperFactory::generateCollection($parameters);
+
+ $this->assertEquals(2, $minesweeperCollection->count());
+ }
+
+ public function testMinesweeperCollectionRenderWithTwoMinesweepersShouldReturnCorrectString(){
+ $parameters = "2 3\n...\n...\n2 3\n...\n...";
+ $expected = "Field #1:\n000\n000\n\nField #2:\n000\n000";
+ $minesweeperCollection = MinesweeperFactory::generateCollection($parameters);
+
+ $this->assertEquals($expected, $minesweeperCollection->render());
+ }
+
+ public function testMinesweeperCollectionRenderShouldReturnCorrectString(){
+ $parameters = "2 3\n...\n...\n2 3\n...\n...";
+ $expected = "Field #1:\n000\n000\n\nField #2:\n000\n000";
+ $minesweeperCollection = MinesweeperFactory::generateCollection($parameters);
+
+ $this->assertEquals($expected, $minesweeperCollection->render());
+ }
+
+
+ public function testMinesweeperCollectionAcceptanceTest(){
+ $parameters = "4 4\n*...\n....\n.*..\n....\n3 5\n**...\n.....\n.*...\n0 0";
+ $expected = "Field #1:\n*100\n2210\n1*10\n1110\n\nField #2:\n**100\n33200\n1*100";
+ $minesweeperCollection = MinesweeperFactory::generateCollection($parameters);
+
+ $this->assertEquals($expected, $minesweeperCollection->render());
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.