Normalize Position bounds for rows and columns

master
Peter Babič 8 years ago
parent 538a725748
commit 5e840726d5
Signed by: peter.babic
GPG Key ID: 4BB075BC1884BA40
  1. 191
      .idea/workspace.xml
  2. 2
      index.php
  3. 8
      src/Coffee/Map.php
  4. 72
      src/Coffee/Position.php
  5. 15
      tests/Coffee/PositionTest.php

@ -1,13 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="65f95bb6-3a86-4ed6-841e-441936291ad4" name="Default" comment="Decouple Map, Position and Tile even more">
<list default="true" id="65f95bb6-3a86-4ed6-841e-441936291ad4" name="Default" comment="Normalize Position bounds for rows and columns">
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/src/Coffee/Map.php" afterPath="$PROJECT_DIR$/src/Coffee/Map.php" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/index.php" afterPath="$PROJECT_DIR$/index.php" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/src/Coffee/Position.php" afterPath="$PROJECT_DIR$/src/Coffee/Position.php" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/tests/Coffee/MapTest.php" afterPath="$PROJECT_DIR$/tests/Coffee/MapTest.php" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/tests/Coffee/PositionTest.php" afterPath="$PROJECT_DIR$/tests/Coffee/PositionTest.php" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/tests/Coffee/TableTest.php" afterPath="$PROJECT_DIR$/tests/Coffee/TableTest.php" />
</list>
<ignored path="coffeetable.iws" />
<ignored path=".idea/workspace.xml" />
@ -33,21 +31,21 @@
</component>
<component name="FileEditorManager">
<leaf>
<file leaf-file-name="Spot.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/Coffee/Spot.php">
<file leaf-file-name="index.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/index.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-3.4074075">
<caret line="44" column="7" selection-start-line="44" selection-start-column="7" selection-end-line="44" selection-end-column="7" />
<state vertical-scroll-proportion="-10.0">
<caret line="15" column="0" selection-start-line="15" selection-start-column="0" selection-end-line="15" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="PositionTest.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/tests/Coffee/PositionTest.php">
<file leaf-file-name="Spot.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/Coffee/Spot.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-12.0">
<caret line="18" column="55" selection-start-line="18" selection-start-column="55" selection-end-line="18" selection-end-column="55" />
<state vertical-scroll-proportion="-26.0">
<caret line="44" column="7" selection-start-line="44" selection-start-column="7" selection-end-line="44" selection-end-column="7" />
<folding />
</state>
</provider>
@ -56,7 +54,7 @@
<file leaf-file-name="TableTest.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/tests/Coffee/TableTest.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-9.407408">
<state vertical-scroll-proportion="-14.0">
<caret line="21" column="37" selection-start-line="21" selection-start-column="37" selection-end-line="21" selection-end-column="37" />
<folding />
</state>
@ -66,18 +64,28 @@
<file leaf-file-name="Position.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/Coffee/Position.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="49.25926">
<caret line="57" column="27" selection-start-line="57" selection-start-column="27" selection-end-line="57" selection-end-column="27" />
<state vertical-scroll-proportion="-115.59259">
<caret line="186" column="28" selection-start-line="186" selection-start-column="28" selection-end-line="186" selection-end-column="28" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="PositionTest.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/tests/Coffee/PositionTest.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-34.74074">
<caret line="125" column="44" selection-start-line="125" selection-start-column="44" selection-end-line="125" selection-end-column="44" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="Map.php" pinned="false" current-in-tab="false">
<file leaf-file-name="Map.php" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/Coffee/Map.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-62.11111">
<caret line="116" column="0" selection-start-line="116" selection-start-column="0" selection-end-line="116" selection-end-column="0" />
<state vertical-scroll-proportion="0.021428572">
<caret line="44" column="100" selection-start-line="44" selection-start-column="100" selection-end-line="44" selection-end-column="100" />
<folding />
</state>
</provider>
@ -86,8 +94,8 @@
<file leaf-file-name="MapTest.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/tests/Coffee/MapTest.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-44.0">
<caret line="66" column="37" selection-start-line="66" selection-start-column="37" selection-end-line="66" selection-end-column="37" />
<state vertical-scroll-proportion="-16.074074">
<caret line="96" column="0" selection-start-line="96" selection-start-column="0" selection-end-line="96" selection-end-column="0" />
<folding />
</state>
</provider>
@ -103,11 +111,11 @@
</provider>
</entry>
</file>
<file leaf-file-name="Tile.php" pinned="false" current-in-tab="true">
<file leaf-file-name="Tile.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/Coffee/Tile.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.58214283">
<caret line="33" column="0" selection-start-line="33" selection-start-column="0" selection-end-line="33" selection-end-column="0" />
<state vertical-scroll-proportion="-16.74074">
<caret line="40" column="5" selection-start-line="40" selection-start-column="5" selection-end-line="40" selection-end-column="5" />
<folding />
</state>
</provider>
@ -153,16 +161,16 @@
<option value="$PROJECT_DIR$/phpunit.xml" />
<option value="$PROJECT_DIR$/composer.json" />
<option value="$PROJECT_DIR$/tests/Coffee/SpotTest.php" />
<option value="$PROJECT_DIR$/index.php" />
<option value="/usr/share/nginx/html/coffeetable/src/Coffee/Map.php" />
<option value="$PROJECT_DIR$/src/Coffee/Tile.php" />
<option value="$PROJECT_DIR$/tests/Coffee/TileTest.php" />
<option value="$PROJECT_DIR$/tests/Coffee/TableTest.php" />
<option value="/usr/share/nginx/html/coffeetable/src/Coffee/Tile.php" />
<option value="$PROJECT_DIR$/src/Coffee/Map.php" />
<option value="$PROJECT_DIR$/tests/Coffee/MapTest.php" />
<option value="$PROJECT_DIR$/tests/Coffee/PositionTest.php" />
<option value="$PROJECT_DIR$/src/Coffee/Position.php" />
<option value="$PROJECT_DIR$/tests/Coffee/MapTest.php" />
<option value="$PROJECT_DIR$/src/Coffee/Map.php" />
<option value="$PROJECT_DIR$/index.php" />
</list>
</option>
</component>
@ -405,7 +413,7 @@
<property name="options.splitter.main.proportions" value="0.3" />
<property name="options.splitter.details.proportions" value="0.2" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="SearchEverywhereHistoryKey" value="rear &#9;ACTION&#9;RearrangeCode&#10;replace in pa&#9;ACTION&#9;ReplaceInPath&#10;histor&#9;ACTION&#9;Vcs.ShowTabbedFileHistory&#10;rep in pa&#9;ACTION&#9;ReplaceInPath&#10;replace in&#9;ACTION&#9;ReplaceInPath&#10;rearr&#9;ACTION&#9;RearrangeCode&#10;rea&#9;ACTION&#9;RearrangeCode&#10;orde&#9;null&#9;null&#10;next hi&#9;ACTION&#9;GotoNextError&#10;next&#9;ACTION&#9;GotoNextError&#10;comm cha&#9;ACTION&#9;ChangesView.Commit&#10;comple&#9;ACTION&#9;CodeCompletionGroup&#10;complet&#9;ACTION&#9;CodeCompletionGroup&#10;sear&#9;ACTION&#9;SearchEverywhere&#10;breakpoint&#9;ACTION&#9;ToggleLineBreakpoint&#10;comm ch&#9;ACTION&#9;ChangesView.Commit&#10;smart&#9;ACTION&#9;SmartTypeCompletion&#10;para&#9;ACTION&#9;IntroduceParameter&#10;keyma&#9;ACTION&#9;ChangeKeymap&#10;commit&#9;ACTION&#9;Vcs.CheckCommitMessageSpelling&#10;commit &#9;ACTION&#9;Vcs.CheckCommitMessageSpelling&#10;phpunit&#9;null&#9;null&#10;exte&#9;ACTION&#9;EditorSelectWord&#10;composer&#9;ACTION&#9;ComposerGroup&#10;test&#9;ACTION&#9;RerunTests&#10;rer&#9;ACTION&#9;RerunTests&#10;tests&#9;ACTION&#9;RerunTests&#10;hpunit&#9;ACTION&#9;PhpUnitGenerateTestMethod&#10;phpuni&#9;ACTION&#9;PhpUnitNewTest&#10;rear&#9;ACTION&#9;RearrangeCode" />
<property name="SearchEverywhereHistoryKey" value="paste&#9;ACTION&#9;EditorPasteSimple&#10;rear &#9;ACTION&#9;RearrangeCode&#10;replace in pa&#9;ACTION&#9;ReplaceInPath&#10;histor&#9;ACTION&#9;Vcs.ShowTabbedFileHistory&#10;rep in pa&#9;ACTION&#9;ReplaceInPath&#10;replace in&#9;ACTION&#9;ReplaceInPath&#10;rearr&#9;ACTION&#9;RearrangeCode&#10;rea&#9;ACTION&#9;RearrangeCode&#10;orde&#9;null&#9;null&#10;next hi&#9;ACTION&#9;GotoNextError&#10;next&#9;ACTION&#9;GotoNextError&#10;comm cha&#9;ACTION&#9;ChangesView.Commit&#10;comple&#9;ACTION&#9;CodeCompletionGroup&#10;complet&#9;ACTION&#9;CodeCompletionGroup&#10;sear&#9;ACTION&#9;SearchEverywhere&#10;breakpoint&#9;ACTION&#9;ToggleLineBreakpoint&#10;comm ch&#9;ACTION&#9;ChangesView.Commit&#10;smart&#9;ACTION&#9;SmartTypeCompletion&#10;para&#9;ACTION&#9;IntroduceParameter&#10;keyma&#9;ACTION&#9;ChangeKeymap&#10;commit&#9;ACTION&#9;Vcs.CheckCommitMessageSpelling&#10;commit &#9;ACTION&#9;Vcs.CheckCommitMessageSpelling&#10;phpunit&#9;null&#9;null&#10;exte&#9;ACTION&#9;EditorSelectWord&#10;composer&#9;ACTION&#9;ComposerGroup&#10;test&#9;ACTION&#9;RerunTests&#10;rer&#9;ACTION&#9;RerunTests&#10;tests&#9;ACTION&#9;RerunTests&#10;hpunit&#9;ACTION&#9;PhpUnitGenerateTestMethod&#10;phpuni&#9;ACTION&#9;PhpUnitNewTest&#10;rear&#9;ACTION&#9;RearrangeCode" />
<property name="settings.editor.selected.configurable" value="preferences.sourceCode.PHP" />
<property name="settings.editor.splitter.proportion" value="0.2" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/tests" />
@ -617,38 +625,62 @@
<option name="project" value="LOCAL" />
<updated>1458223743752</updated>
</task>
<option name="localTasksCounter" value="18" />
<task id="LOCAL-00018" summary="Change positioning system to start at [1, 1] not [0, 0]">
<created>1458226413989</created>
<option name="number" value="00018" />
<option name="project" value="LOCAL" />
<updated>1458226413989</updated>
</task>
<task id="LOCAL-00019" summary="Implement Map.visitedTile as private Tile[]">
<created>1458227534205</created>
<option name="number" value="00019" />
<option name="project" value="LOCAL" />
<updated>1458227534205</updated>
</task>
<task id="LOCAL-00020" summary="Normalize Position bounds for rows and columns">
<created>1458234903337</created>
<option name="number" value="00020" />
<option name="project" value="LOCAL" />
<updated>1458234903337</updated>
</task>
<task id="LOCAL-00021" summary="Normalize Position bounds for rows and columns">
<created>1458235032718</created>
<option name="number" value="00021" />
<option name="project" value="LOCAL" />
<updated>1458235032718</updated>
</task>
<option name="localTasksCounter" value="22" />
<servers />
</component>
<component name="TestHistory">
<history-entry file="Tests - 2016.03.17 at 15h 42m 09s.xml">
<history-entry file="Tests - 2016.03.17 at 18h 28m 45s.xml">
<configuration name="Tests" configurationId="PHPUnitRunConfigurationType" />
</history-entry>
<history-entry file="Tests - 2016.03.17 at 15h 42m 48s.xml">
<history-entry file="Tests - 2016.03.17 at 18h 31m 37s.xml">
<configuration name="Tests" configurationId="PHPUnitRunConfigurationType" />
</history-entry>
<history-entry file="Tests - 2016.03.17 at 15h 44m 12s.xml">
<history-entry file="Tests - 2016.03.17 at 18h 33m 05s.xml">
<configuration name="Tests" configurationId="PHPUnitRunConfigurationType" />
</history-entry>
<history-entry file="Tests - 2016.03.17 at 15h 47m 15s.xml">
<history-entry file="Tests - 2016.03.17 at 18h 33m 27s.xml">
<configuration name="Tests" configurationId="PHPUnitRunConfigurationType" />
</history-entry>
<history-entry file="Tests - 2016.03.17 at 15h 47m 18s.xml">
<history-entry file="Tests - 2016.03.17 at 18h 33m 55s.xml">
<configuration name="Tests" configurationId="PHPUnitRunConfigurationType" />
</history-entry>
<history-entry file="Tests - 2016.03.17 at 15h 47m 44s.xml">
<history-entry file="Tests - 2016.03.17 at 18h 36m 43s.xml">
<configuration name="Tests" configurationId="PHPUnitRunConfigurationType" />
</history-entry>
<history-entry file="Tests - 2016.03.17 at 15h 48m 45s.xml">
<history-entry file="Tests - 2016.03.17 at 18h 37m 50s.xml">
<configuration name="Tests" configurationId="PHPUnitRunConfigurationType" />
</history-entry>
<history-entry file="Tests - 2016.03.17 at 15h 48m 52s.xml">
<history-entry file="Tests - 2016.03.17 at 18h 38m 42s.xml">
<configuration name="Tests" configurationId="PHPUnitRunConfigurationType" />
</history-entry>
<history-entry file="Tests - 2016.03.17 at 15h 50m 27s.xml">
<history-entry file="Tests - 2016.03.17 at 18h 41m 22s.xml">
<configuration name="Tests" configurationId="PHPUnitRunConfigurationType" />
</history-entry>
<history-entry file="Tests - 2016.03.17 at 15h 50m 31s.xml">
<history-entry file="Tests - 2016.03.17 at 19h 14m 06s.xml">
<configuration name="Tests" configurationId="PHPUnitRunConfigurationType" />
</history-entry>
</component>
@ -706,7 +738,10 @@
<MESSAGE value="Implemet Tile extends Position" />
<MESSAGE value="Decouple Map and Position" />
<MESSAGE value="Decouple Map, Position and Tile even more" />
<option name="LAST_COMMIT_MESSAGE" value="Decouple Map, Position and Tile even more" />
<MESSAGE value="Change positioning system to start at [1, 1] not [0, 0]" />
<MESSAGE value="Implement Map.visitedTile as private Tile[]" />
<MESSAGE value="Normalize Position bounds for rows and columns" />
<option name="LAST_COMMIT_MESSAGE" value="Normalize Position bounds for rows and columns" />
<option name="OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT" value="true" />
<option name="REFORMAT_BEFORE_PROJECT_COMMIT" value="true" />
<option name="REARRANGE_BEFORE_PROJECT_COMMIT" value="true" />
@ -986,14 +1021,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/index.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-4.6666665">
<caret line="7" column="17" selection-start-line="7" selection-start-column="17" selection-end-line="7" selection-end-column="17" />
<folding />
</state>
</provider>
</entry>
<entry file="file:///usr/share/nginx/html/coffeetable/src/Coffee/Tile.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.46964285">
@ -1010,22 +1037,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/tests/Coffee/TableTest.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-9.407408">
<caret line="21" column="37" selection-start-line="21" selection-start-column="37" selection-end-line="21" selection-end-column="37" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/tests/Coffee/PositionTest.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-12.0">
<caret line="18" column="55" selection-start-line="18" selection-start-column="55" selection-end-line="18" selection-end-column="55" />
<folding />
</state>
</provider>
</entry>
<entry file="phar:///usr/share/webapps/bin/phpunit.phar/phpunit/Framework/TestFailure.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-5.740741">
@ -1066,34 +1077,34 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/tests/Coffee/MapTest.php">
<entry file="file://$PROJECT_DIR$/src/Coffee/Tile.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-44.0">
<caret line="66" column="37" selection-start-line="66" selection-start-column="37" selection-end-line="66" selection-end-column="37" />
<state vertical-scroll-proportion="-16.74074">
<caret line="40" column="5" selection-start-line="40" selection-start-column="5" selection-end-line="40" selection-end-column="5" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Coffee/Map.php">
<entry file="file://$PROJECT_DIR$/tests/Coffee/TableTest.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-62.11111">
<caret line="116" column="0" selection-start-line="116" selection-start-column="0" selection-end-line="116" selection-end-column="0" />
<state vertical-scroll-proportion="-14.0">
<caret line="21" column="37" selection-start-line="21" selection-start-column="37" selection-end-line="21" selection-end-column="37" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Coffee/Position.php">
<entry file="file://$PROJECT_DIR$/src/Coffee/Spot.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="49.25926">
<caret line="57" column="27" selection-start-line="57" selection-start-column="27" selection-end-line="57" selection-end-column="27" />
<state vertical-scroll-proportion="-26.0">
<caret line="44" column="7" selection-start-line="44" selection-start-column="7" selection-end-line="44" selection-end-column="7" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Coffee/Spot.php">
<entry file="file://$PROJECT_DIR$/index.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-3.4074075">
<caret line="44" column="7" selection-start-line="44" selection-start-column="7" selection-end-line="44" selection-end-column="7" />
<state vertical-scroll-proportion="-10.0">
<caret line="15" column="0" selection-start-line="15" selection-start-column="0" selection-end-line="15" selection-end-column="0" />
<folding />
</state>
</provider>
@ -1106,10 +1117,34 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Coffee/Tile.php">
<entry file="file://$PROJECT_DIR$/tests/Coffee/MapTest.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-16.074074">
<caret line="96" column="0" selection-start-line="96" selection-start-column="0" selection-end-line="96" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/tests/Coffee/PositionTest.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-34.74074">
<caret line="125" column="44" selection-start-line="125" selection-start-column="44" selection-end-line="125" selection-end-column="44" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Coffee/Position.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-115.59259">
<caret line="186" column="28" selection-start-line="186" selection-start-column="28" selection-end-line="186" selection-end-column="28" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Coffee/Map.php">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.58214283">
<caret line="33" column="0" selection-start-line="33" selection-start-column="0" selection-end-line="33" selection-end-column="0" />
<state vertical-scroll-proportion="0.021428572">
<caret line="44" column="100" selection-start-line="44" selection-start-column="100" selection-end-line="44" selection-end-column="100" />
<folding />
</state>
</provider>

@ -9,7 +9,7 @@ try {
[0, 1, 0, 1],
[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 1]
[0, 0, 1, 1]
]);
// $table = new Table();

@ -42,7 +42,7 @@ class Map {
$height = 0;
$width = 0;
// This data processing could be written more read-ably in multiple cycles,
// This data processing could be written more read-ably in multiple cycles / private methods,
// but at a cost of reduced performance (minor).
foreach ($description as $rowIndex => $row) {
foreach ($row as $columnIndex => $tileRepresentation) {
@ -116,14 +116,12 @@ class Map {
}
/**
* Test the upper bound
*
* @param Position $position
* @return bool
*/
public function isValidPosition(Position $position) {
if ($position->getRow() < 0 || $position->getColumn() < 0) {
return false;
}
if ($position->getRow() > $this->getHeight() || $position->getColumn() > $this->getWidth()) {
return false;
}

@ -2,6 +2,11 @@
namespace Coffee;
/**
* Class Position
*
* @package Coffee
*/
/**
* Class Position
*
@ -9,6 +14,11 @@ namespace Coffee;
*/
class Position {
/**
* The lowest possible position for rows and columns
*/
const LOWER_BOUND = 1;
/**
* @var int
*/
@ -24,12 +34,8 @@ class Position {
* @throws \Exception
*/
function __construct($row, $column) {
if ($row <= 0) {
throw new \Exception('The row argument must be positive non-zero number.');
}
if ($column <= 0) {
throw new \Exception('The columns argument must be positive non-zero number.');
if (!$this->fitsIntoLowerBound($row, $column)) {
throw new \Exception('The row argument must be higher than ' . self::LOWER_BOUND . '.');
}
$this->column = $column;
@ -43,6 +49,9 @@ class Position {
return $this->column;
}
/**
* @return int
*/
public function getColumnIndex() {
return $this->column - 1;
}
@ -54,6 +63,9 @@ class Position {
return $this->row;
}
/**
* @return int
*/
public function getRowIndex() {
return $this->row - 1;
}
@ -86,63 +98,65 @@ class Position {
* @return Position
*/
public function getNorthEastPosition() {
return new Position($this->getRow() - 1, $this->getColumn() + 1);
return $this->getSafeNeighbor($this->getRow() - 1, $this->getColumn() + 1);
}
/**
* @return Position
*/
public function getEastPosition() {
return new Position($this->getRow(), $this->getColumn() + 1);
return $this->getSafeNeighbor($this->getRow(), $this->getColumn() + 1);
}
/**
* @return Position
*/
public function getSouthEastPosition() {
return new Position($this->getRow() + 1, $this->getColumn() + 1);
return $this->getSafeNeighbor($this->getRow() + 1, $this->getColumn() + 1);
}
/**
* @return Position
*/
public function getSouthPosition() {
return new Position($this->getRow() + 1, $this->getColumn());
return $this->getSafeNeighbor($this->getRow() + 1, $this->getColumn());
}
/**
* @return Position
*/
public function getSouthWestPosition() {
return new Position($this->getRow() + 1, $this->getColumn() - 1);
return $this->getSafeNeighbor($this->getRow() + 1, $this->getColumn() - 1);
}
/**
* @return Position
*/
public function getWestPosition() {
return new Position($this->getRow(), $this->getColumn() - 1);
return $this->getSafeNeighbor($this->getRow(), $this->getColumn() - 1);
}
/**
* @return Position
*/
public function getNorthWestPosition() {
return new Position($this->getRow() - 1, $this->getColumn() - 1);
return $this->getSafeNeighbor($this->getRow() - 1, $this->getColumn() - 1);
}
/**
* @return Position
*/
public function getNorthPosition() {
return new Position($this->getRow() - 1, $this->getColumn());
return $this->getSafeNeighbor($this->getRow() - 1, $this->getColumn());
}
/**
* @return array Neighbors of position from NE to N; CW direction
*/
public function getNeighbors() {
return [
// null values from out-of-lower-bound neighbors are filtered out and indices are fixed
return array_values(array_filter([
$this->getNorthEastPosition(),
$this->getEastPosition(),
$this->getSouthEastPosition(),
@ -151,7 +165,33 @@ class Position {
$this->getWestPosition(),
$this->getNorthWestPosition(),
$this->getNorthPosition(),
];
]));
}
/**
* @param $row
* @param $column
* @return bool
*/
private function fitsIntoLowerBound($row, $column) {
if ($row < self::LOWER_BOUND || $column < self::LOWER_BOUND) {
return false;
}
return true;
}
/**
* @param $row
* @param $column
* @return Position|null
*/
private function getSafeNeighbor($row, $column) {
if ($this->fitsIntoLowerBound($row, $column)) {
return new Position($row, $column);
}
return null;
}
}

@ -104,8 +104,9 @@ class PositionTest extends \PHPUnit_Framework_TestCase {
$this->assertTrue($northPositionA->isTheSamePosition($NorthPositionB));
}
public function testNeighbors() {
public function testOpenSpaceNeighbors() {
$position = new Position(2, 2);
// Neighbors from NE to N, in CW direction
$neighbours = [
new Position(1, 3),
new Position(2, 3),
@ -119,4 +120,16 @@ class PositionTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($neighbours, $position->getNeighbors());
}
public function testCornerNeighbors() {
$position = new Position(1, 1);
// Return just E, SE and S neighbors
$neighbours = [
new Position(1, 2),
new Position(2, 2),
new Position(2, 1),
];
$this->assertEquals($neighbours, $position->getNeighbors());
}
}
Loading…
Cancel
Save