initial commit

master
Peter Babič 8 years ago
commit c4acbb8ea8
Signed by: peter.babic
GPG Key ID: 4BB075BC1884BA40
  1. 1
      .gitignore
  2. 83
      .idea/coffeetable.iml
  3. 3
      .idea/copyright/profiles_settings.xml
  4. 6
      .idea/encodings.xml
  5. 8
      .idea/modules.xml
  6. 8
      .idea/php.xml
  7. 1870
      .idea/workspace.xml
  8. 15
      composer.json
  9. 1112
      composer.lock
  10. 19
      phpunit.xml
  11. 114
      src/Coffee/Table.php
  12. 241
      src/Coffee/Tile.php
  13. 67
      tests/TableTest.php
  14. 157
      tests/TileTest.php

1
.gitignore vendored

@ -0,0 +1 @@
vendor/

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/vendor/composer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
<excludeFolder url="file://$MODULE_DIR$/vendor/myclabs/deep-copy" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-docblock" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpspec/prophecy" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-code-coverage" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-file-iterator" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-text-template" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-timer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-token-stream" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/phpunit" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/phpunit-mock-objects" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/code-unit-reverse-lookup" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/comparator" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/diff" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/environment" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/exporter" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/global-state" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/resource-operations" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/version" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/yaml" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="PHP">
<CLASSES>
<root url="file://$MODULE_DIR$/vendor/composer" />
<root url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
<root url="file://$MODULE_DIR$/vendor/myclabs/deep-copy" />
<root url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-docblock" />
<root url="file://$MODULE_DIR$/vendor/phpspec/prophecy" />
<root url="file://$MODULE_DIR$/vendor/phpunit/php-code-coverage" />
<root url="file://$MODULE_DIR$/vendor/phpunit/php-file-iterator" />
<root url="file://$MODULE_DIR$/vendor/phpunit/php-text-template" />
<root url="file://$MODULE_DIR$/vendor/phpunit/php-timer" />
<root url="file://$MODULE_DIR$/vendor/phpunit/php-token-stream" />
<root url="file://$MODULE_DIR$/vendor/phpunit/phpunit" />
<root url="file://$MODULE_DIR$/vendor/phpunit/phpunit-mock-objects" />
<root url="file://$MODULE_DIR$/vendor/sebastian/code-unit-reverse-lookup" />
<root url="file://$MODULE_DIR$/vendor/sebastian/comparator" />
<root url="file://$MODULE_DIR$/vendor/sebastian/diff" />
<root url="file://$MODULE_DIR$/vendor/sebastian/environment" />
<root url="file://$MODULE_DIR$/vendor/sebastian/exporter" />
<root url="file://$MODULE_DIR$/vendor/sebastian/global-state" />
<root url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
<root url="file://$MODULE_DIR$/vendor/sebastian/resource-operations" />
<root url="file://$MODULE_DIR$/vendor/sebastian/version" />
<root url="file://$MODULE_DIR$/vendor/symfony/yaml" />
</CLASSES>
<SOURCES>
<root url="file://$MODULE_DIR$/vendor/composer" />
<root url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
<root url="file://$MODULE_DIR$/vendor/myclabs/deep-copy" />
<root url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-docblock" />
<root url="file://$MODULE_DIR$/vendor/phpspec/prophecy" />
<root url="file://$MODULE_DIR$/vendor/phpunit/php-code-coverage" />
<root url="file://$MODULE_DIR$/vendor/phpunit/php-file-iterator" />
<root url="file://$MODULE_DIR$/vendor/phpunit/php-text-template" />
<root url="file://$MODULE_DIR$/vendor/phpunit/php-timer" />
<root url="file://$MODULE_DIR$/vendor/phpunit/php-token-stream" />
<root url="file://$MODULE_DIR$/vendor/phpunit/phpunit" />
<root url="file://$MODULE_DIR$/vendor/phpunit/phpunit-mock-objects" />
<root url="file://$MODULE_DIR$/vendor/sebastian/code-unit-reverse-lookup" />
<root url="file://$MODULE_DIR$/vendor/sebastian/comparator" />
<root url="file://$MODULE_DIR$/vendor/sebastian/diff" />
<root url="file://$MODULE_DIR$/vendor/sebastian/environment" />
<root url="file://$MODULE_DIR$/vendor/sebastian/exporter" />
<root url="file://$MODULE_DIR$/vendor/sebastian/global-state" />
<root url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
<root url="file://$MODULE_DIR$/vendor/sebastian/resource-operations" />
<root url="file://$MODULE_DIR$/vendor/sebastian/version" />
<root url="file://$MODULE_DIR$/vendor/symfony/yaml" />
</SOURCES>
</library>
</orderEntry>
</component>
</module>

@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/coffeetable.iml" filepath="$PROJECT_DIR$/.idea/coffeetable.iml" />
</modules>
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpUnit">
<phpunit_settings>
<PhpUnitSettings load_method="PHPUNIT_PHAR" bootstrap_file_path="$PROJECT_DIR$/vendor/autoload.php" configuration_file_path="$PROJECT_DIR$/phpunit.xml" custom_loader_path="" phpunit_phar_path="/usr/bin/phpunit" />
</phpunit_settings>
</component>
</project>

File diff suppressed because it is too large Load Diff

@ -0,0 +1,15 @@
{
"require-dev": {
"phpunit/phpunit": "5.2.*"
},
"autoload-dev": {
"psr-0": {
"": "tests"
}
},
"autoload": {
"psr-0": {
"Coffee": "src"
}
}
}

1112
composer.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>

@ -0,0 +1,114 @@
<?php
/**
* Created by PhpStorm.
* User: delmadord
* Date: 3/16/16
* Time: 7:56 AM
*/
namespace Coffee;
use Exception;
/**
* Class Table
*
* @package Coffee
*/
class Table {
/**
* @var array
*/
private $remainingTiles = [];
/**
* @var int
*/
private $height = 0;
/**
* @var int
*/
private $width = 0;
/**
* Table constructor.
*
* @param $coffeeMap
* @throws Exception
*/
public function __construct($coffeeMap) {
if (is_null($coffeeMap) || !is_array($coffeeMap)) {
throw new Exception('The Coffee Table map could not be loaded.');
}
$this->height = $this->calculateMapHeight($coffeeMap);
$this->width = $this->calculateMapWidth($coffeeMap);
$this->remainingTiles = $coffeeMap;
}
/**
* @return int
*/
public function getHeight() {
return $this->height;
}
/**
* @return int
*/
public function getWidth() {
return $this->width;
}
/**
* @return array
*/
public function getRemainingTiles() {
return $this->remainingTiles;
}
/**
* @param $map
* @return int
*/
private function calculateMapHeight($map) {
// Count the level 1 array elements
return count($map);
}
/**
* @param $map
* @return int
*/
private function calculateMapWidth($map) {
$widestRow = 0;
foreach ($map as $row) {
// Count the level 2 array elements
$colWidth = count($row);
if ($colWidth > $widestRow) {
$widestRow = $colWidth;
}
}
return $widestRow;
}
/**
* @param Tile $tile
* @return bool
*/
public function isValidForTile(Tile $tile) {
if ($tile->getX() < 0 || $tile->getY() < 0) {
return false;
}
// Dimensions start from 1 but coordinates from 0, need to compensate
if ($tile->getX() > ($this->getWidth() - 1) || $tile->getY() > ($this->getHeight() - 1)) {
return false;
}
return true;
}
}

@ -0,0 +1,241 @@
<?php
namespace Coffee;
/**
* Class Tile
*
* @package Coffee
*/
class Tile {
/**
* @var int
*/
private $X = 0;
/**
* @var int
*/
private $Y = 0;
/**
* @param $X
* @param $Y
*/
function __construct($X, $Y) {
$this->X = $X;
$this->Y = $Y;
}
/**
* @return int
*/
public function getX() {
return $this->X;
}
/**
* @return int
*/
public function getY() {
return $this->Y;
}
/**
* @param Tile $tile
* @return bool
*/
public function isTheSameX(Tile $tile) {
return $this->getX() == $tile->getX();
}
/**
* @param Tile $tile
* @return bool
*/
public function isTheSameY(Tile $tile) {
return $this->getY() == $tile->getY();
}
/**
* @param Tile $tile
* @return bool
*/
public function isTheSameTile(Tile $tile) {
return $this->isTheSameX($tile) && $this->isTheSameY($tile);
}
/**
* @return Tile
*/
public function getNorthEastTile() {
return new Tile($this->getX() + 1, $this->getY() - 1);
}
/**
* @return Tile
*/
public function getEastTile() {
return new Tile($this->getX() + 1, $this->getY());
}
/**
* @return Tile
*/
public function getSouthEastTile() {
return new Tile($this->getX() + 1, $this->getY() + 1);
}
/**
* @return Tile
*/
public function getSouthTile() {
return new Tile($this->getX(), $this->getY() + 1);
}
/**
* @return Tile
*/
public function getSouthWestTile() {
return new Tile($this->getX() - 1, $this->getY() + 1);
}
/**
* @return Tile
*/
public function getWestTile() {
return new Tile($this->getX() - 1, $this->getY());
}
/**
* @return Tile
*/
public function getNorthWestTile() {
return new Tile($this->getX() - 1, $this->getY() - 1);
}
/**
* @return Tile
*/
public function getNorthTile() {
return new Tile($this->getX(), $this->getY() - 1);
}
// /**
// * @param Tile $tile
// * @return bool
// */
// public function isNorthOf(Tile $tile) {
// return $this->isTheSameX($tile) && $this->isInRowNorthOf($tile);
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// public function isSouthOf(Tile $tile) {
// return $this->isTheSameX($tile) && $this->isInRowSouthOf($tile);
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// public function isEastOf(Tile $tile) {
// return $this->isTheSameY($tile) && $this->isInColumnEastOf($tile);
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// public function isWestOf(Tile $tile) {
// return $this->isTheSameY($tile) && $this->isInColumnWestOf($tile);
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// public function isNorthEastOf(Tile $tile) {
// return $this->isInRowNorthOf($tile) && $this->isInColumnEastOf($tile);
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// public function isNorthWestOf(Tile $tile) {
// return $this->isInRowNorthOf($tile) && $this->isInColumnWestOf($tile);
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// public function isSouthEastOf(Tile $tile) {
// return $this->isInRowSouthOf($tile) && $this->isInColumnEastOf($tile);
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// public function isSouthWestOf(Tile $tile) {
// return $this->isInRowSouthOf($tile) && $this->isInColumnWestOf($tile);
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
//
//
// /**
// * @param Tile $tile
// * @return bool
// */
// public function isNeighborOf(Tile $tile) {
// // Guard; not necessary but to be completely clear
// if ($this->isTheSameTile($tile)) {
// return false;
// }
//
// return
// $this->isNorthOf($tile) || $this->isSouthOf($tile) ||
// $this->isEastOf($tile) || $this->isWestOf($tile) ||
// $this->isNorthEastOf($tile) || $this->isNorthWestOf($tile) ||
// $this->isSouthEastOf($tile) || $this->isSouthWestOf($tile);
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// protected function isInRowNorthOf(Tile $tile) {
// return ($this->getY() - $tile->getY()) == 1;
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// protected function isInRowSouthOf(Tile $tile) {
// return ($this->getY() - $tile->getY()) == -1;
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// protected function isInColumnEastOf(Tile $tile) {
// return ($this->getX() - $tile->getX()) == 1;
// }
//
// /**
// * @param Tile $tile
// * @return bool
// */
// protected function isInColumnWestOf(Tile $tile) {
// return ($this->getX() - $tile->getX()) == -1;
// }
}

@ -0,0 +1,67 @@
<?php
namespace Coffee;
/**
* Created by PhpStorm.
* User: delmadord
* Date: 3/16/16
* Time: 7:59 AM
*/
class TableTest extends \PHPUnit_Framework_TestCase {
public function testRemainingTiles() {
$tableMap = [
[0, 1],
[1, 0],
];
$table = new Table($tableMap);
$this->assertEquals($tableMap, $table->getRemainingTiles());
}
public function testHeight() {
$tableMap = [
[0, 1, 0, 1],
[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 1]
];
$table = new Table($tableMap);
$this->assertEquals(4, $table->getHeight());
}
public function testWidth() {
$tableMap = [
[0, 1, 0],
[1, 0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
$table = new Table($tableMap);
$this->assertEquals(4, $table->getWidth());
}
public function testTileLiesOnTable() {
$tableMap = [
[0, 1],
[1, 0],
];
$table = new Table($tableMap);
$tile = new Tile(0, 0);
$this->assertTrue($table->isValidForTile($tile));
}
public function testTileLiesOutsideTable() {
$tableMap = [
[0, 1],
[1, 0],
];
$table = new Table($tableMap);
$tile = new Tile(2, 1);
$this->assertFalse($table->isValidForTile($tile));
}
}

@ -0,0 +1,157 @@
<?php
namespace Coffee;
class TileTest extends \PHPUnit_Framework_TestCase {
public function testTheSameX() {
$tileA = new Tile(2, 2);
$tileB = new Tile(2, 2);
$this->assertTrue($tileA->isTheSameX($tileB));
}
public function testTheSameY() {
$tileA = new Tile(2, 2);
$tileB = new Tile(2, 2);
$this->assertTrue($tileA->isTheSameY($tileB));
}
public function testTheSameTile() {
$tileA = new Tile(2, 2);
$tileB = new Tile(2, 2);
$this->assertTrue($tileA->isTheSameTile($tileB));
}
public function testNorthEastTile() {
$tile = new Tile(2, 2);
$northEastTileA = $tile->getNorthEastTile();
$northEastTileB = new Tile(3, 1);
$this->assertTrue($northEastTileA->isTheSameTile($northEastTileB));
}
public function testEastTile() {
$tile = new Tile(2, 2);
$northTileA = $tile->getEastTile();
$NorthTileB = new Tile(3, 2);
$this->assertTrue($northTileA->isTheSameTile($NorthTileB));
}
public function testSouthEastTile() {
$tile = new Tile(2, 2);
$northTileA = $tile->getSouthEastTile();
$NorthTileB = new Tile(3, 3);
$this->assertTrue($northTileA->isTheSameTile($NorthTileB));
}
public function testSouthTile() {
$tile = new Tile(2, 2);
$northTileA = $tile->getSouthTile();
$NorthTileB = new Tile(2, 3);
$this->assertTrue($northTileA->isTheSameTile($NorthTileB));
}
public function testsSouthWestTile() {
$tile = new Tile(2, 2);
$northTileA = $tile->getSouthWestTile();
$NorthTileB = new Tile(1, 3);
$this->assertTrue($northTileA->isTheSameTile($NorthTileB));
}
public function testWestTile() {
$tile = new Tile(2, 2);
$northTileA = $tile->getWestTile();
$NorthTileB = new Tile(1, 2);
$this->assertTrue($northTileA->isTheSameTile($NorthTileB));
}
public function testNorthWestTile() {
$tile = new Tile(2, 2);
$northTileA = $tile->getNorthWestTile();
$NorthTileB = new Tile(1, 1);
$this->assertTrue($northTileA->isTheSameTile($NorthTileB));
}
public function testNorthTile() {
$tile = new Tile(2, 2);
$northTileA = $tile->getNorthTile();
$NorthTileB = new Tile(2, 1);
$this->assertTrue($northTileA->isTheSameTile($NorthTileB));
}
// public function testNorthOf() {
// $tileA = new Tile(2, 2);
// $tileB = new Tile(2, 1);
//
// $this->assertTrue($tileA->isNorthOf($tileB));
// }
//
// public function testSouthOf() {
// $tileA = new Tile(2, 1);
// $tileB = new Tile(2, 2);
//
// $this->assertTrue($tileA->isSouthOf($tileB));
// }
//
// public function testEastOf() {
// $tileA = new Tile(2, 2);
// $tileB = new Tile(1, 2);
//
// $this->assertTrue($tileA->isEastOf($tileB));
// }
//
// public function testWestOf() {
// $tileA = new Tile(1, 2);
// $tileB = new Tile(2, 2);
//
// $this->assertTrue($tileA->isWestOf($tileB));
// }
//
// public function testNorthEastOf() {
// $tileA = new Tile(2, 2);
// $tileB = new Tile(1, 1);
//
// $this->assertTrue($tileA->isNorthEastOf($tileB));
// }
//
// public function testNorthWestOf() {
// $tileA = new Tile(1, 2);
// $tileB = new Tile(2, 1);
//
// $this->assertTrue($tileA->isNorthWestOf($tileB));
// }
//
// public function testSouthEastOf() {
// $tileA = new Tile(2, 1);
// $tileB = new Tile(1, 2);
//
// $this->assertTrue($tileA->isSouthEastOf($tileB));
// }
//
// public function testSouthWestOf() {
// $tileA = new Tile(1, 1);
// $tileB = new Tile(2, 2);
//
// $this->assertTrue($tileA->isSouthWestOf($tileB));
// }
//
// public function testNeighborOf() {
// $tileA = new Tile(1, 1);
// $tileB = new Tile(2, 2);
//
// $this->assertTrue($tileA->isNeighborOf($tileB));
// }
}
Loading…
Cancel
Save