Skip to content

Instantly share code, notes, and snippets.

@brtriver
Created October 28, 2011 05:01
Show Gist options
  • Save brtriver/1321664 to your computer and use it in GitHub Desktop.
Save brtriver/1321664 to your computer and use it in GitHub Desktop.
mytdd
<?php
/*
こんなイメージ
(例) 1,1 と 1,2 に爆弾がある
x:1 x:2 x:3 x:4
+-----+-----+-----+-----+
y:1| x | | | |
+-----+-----+-----+-----+
y:2| x | | | |
+-----+-----+-----+-----+
y:3| | | | |
+-----+-----+-----+-----+
y:4| | | | |
+-----+-----+-----+-----+
*/
/**
* SweeperTest
*
* @package TDDWS
* @author Masao Maeda <Masao_Maeda@voyagegroup.com>
* @version $id$
*/
class SweeperTest extends PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function 爆弾が1つだけのマップで爆弾の数を数える()
{
$bombs = array(
array('x' => 1, 'y' => 1),
);
$Map = new Map($bombs);
$Sweeper = new Sweeper($Map);
$this->assertEquals(1, $Sweeper->countNextBombs(2,2));
$this->assertEquals(0, $Sweeper->countNextBombs(3,3));
}
/**
* @test
*/
public function 爆弾が複数のマップで爆弾の数を数える()
{
$bombs = array(
array('x' => 1, 'y' => 1),
array('x' => 1, 'y' => 2),
);
$Map = new Map($bombs);
$Sweeper = new Sweeper($Map);
$this->assertEquals(2, $Sweeper->countNextBombs(2,2));
$this->assertEquals(1, $Sweeper->countNextBombs(2,3));
}
}
// Map
class Map
{
public $bombs = array();
public function __construct(array $bombs)
{
$this->bombs = $bombs;
}
public function getNextBombs($x, $y)
{
$nextBombs = array_filter($this->bombs, function($items) use($x, $y) {
return (abs($items['x'] - $x) <= 1 && abs($items['y'] - $y) <= 1);
});
return $nextBombs;
}
}
// Sweeper
class Sweeper
{
public $map;
public function __construct(Map $map)
{
$this->map = $map;
}
public function countNextBombs($x, $y)
{
return count($this->map->getNextBombs($x, $y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment