From 1f46472e0c13a3a7a8705a06d35765f159ccdec7 Mon Sep 17 00:00:00 2001 From: Maxim Vainshtein Date: Sun, 15 Nov 2015 12:07:00 +0200 Subject: [PATCH] Made changes to responsibility of direction ,logically is decided by map and not by tile itself (tiles don't know about relation to other tiles, but map does). Added license and readme. --- Ex1.xcodeproj/project.pbxproj | 6 +++ Ex1/LICENSE | 21 ++++++++ Ex1/Map.cpp | 98 +++++++++++++++++++++++++++-------- Ex1/Map.hpp | 18 ++++++- Ex1/README.md | 2 + Ex1/Tile.cpp | 70 +++---------------------- Ex1/Tile.hpp | 21 ++------ 7 files changed, 133 insertions(+), 103 deletions(-) create mode 100644 Ex1/LICENSE create mode 100644 Ex1/README.md diff --git a/Ex1.xcodeproj/project.pbxproj b/Ex1.xcodeproj/project.pbxproj index 7fb3820..df08341 100644 --- a/Ex1.xcodeproj/project.pbxproj +++ b/Ex1.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 945012591BF7A38100913F6C /* IDSAlgorithm.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 945012571BF7A38100913F6C /* IDSAlgorithm.cpp */; }; 9450125C1BF7A38A00913F6C /* UCSAlgorithm.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9450125A1BF7A38A00913F6C /* UCSAlgorithm.cpp */; }; 945012601BF7A9F000913F6C /* Tile.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9450125E1BF7A9F000913F6C /* Tile.cpp */; }; + 94D023D71BF88ACE00CEF4A8 /* README.md in Sources */ = {isa = PBXBuildFile; fileRef = 94D023D61BF88ACE00CEF4A8 /* README.md */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -41,6 +42,8 @@ 9450125B1BF7A38A00913F6C /* UCSAlgorithm.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = UCSAlgorithm.hpp; sourceTree = ""; }; 9450125E1BF7A9F000913F6C /* Tile.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Tile.cpp; sourceTree = ""; }; 9450125F1BF7A9F000913F6C /* Tile.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Tile.hpp; sourceTree = ""; }; + 94D023D61BF88ACE00CEF4A8 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + 94D023D91BF88B3800CEF4A8 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -76,6 +79,8 @@ 9450125D1BF7A39000913F6C /* Algorithm */, 9450124E1BF7910300913F6C /* Map */, 9423719D1BF27E5D00F46C34 /* main.cpp */, + 94D023D61BF88ACE00CEF4A8 /* README.md */, + 94D023D91BF88B3800CEF4A8 /* LICENSE */, 945012521BF7A35500913F6C /* input.txt */, ); path = Ex1; @@ -164,6 +169,7 @@ 945012591BF7A38100913F6C /* IDSAlgorithm.cpp in Sources */, 9450125C1BF7A38A00913F6C /* UCSAlgorithm.cpp in Sources */, 945012561BF7A37700913F6C /* Algorithm.cpp in Sources */, + 94D023D71BF88ACE00CEF4A8 /* README.md in Sources */, 945012601BF7A9F000913F6C /* Tile.cpp in Sources */, 9450124D1BF78D0A00913F6C /* Map.cpp in Sources */, 9423719E1BF27E5D00F46C34 /* main.cpp in Sources */, diff --git a/Ex1/LICENSE b/Ex1/LICENSE new file mode 100644 index 0000000..7ff31fc --- /dev/null +++ b/Ex1/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 MaximV88 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/Ex1/Map.cpp b/Ex1/Map.cpp index 0c6e522..4aa3aad 100644 --- a/Ex1/Map.cpp +++ b/Ex1/Map.cpp @@ -33,12 +33,12 @@ Map::Map(const std::string& strFormattedMap) : m_cData(nullptr) { //Using placement new to create tiles, assignemt is as told by instructions switch (strRowInfo[uiCol]) { - case 'S': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::TileType::kStart, uiCol, uiRow); break; - case 'G': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::TileType::kEnd, uiCol, uiRow); break; - case 'R': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::TileType::kRoad, uiCol, uiRow); break; - case 'D': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::TileType::kDirt, uiCol, uiRow); break; - case 'H': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::TileType::kHill, uiCol, uiRow); break; - case 'W': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::TileType::kWater, uiCol, uiRow); break; + case 'S': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kStart, uiCol, uiRow); break; + case 'G': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kEnd, uiCol, uiRow); break; + case 'R': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kRoad, uiCol, uiRow); break; + case 'D': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kDirt, uiCol, uiRow); break; + case 'H': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kHill, uiCol, uiRow); break; + case 'W': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kWater, uiCol, uiRow); break; default: break; } @@ -59,7 +59,63 @@ Tile Map::getStartTile() const { return Tile(m_cData[0]); } Tile Map::getEndTile() const { return Tile(m_cData[index(m_uiWidth, m_uiHeight)]); } -const Tile& Map::getTile(const Tile &cOrigin, Tile::Direction) const { +Map::Directions Map::getDirection(const Tile &cOrigin, const Tile& cDestination) const { + + //Important to remember that highest is 0 and lowest is N-1, sideways is normal + + //Get the horizontal direction + Directions eHorizontal; + if (cDestination.m_uiPositionX == cOrigin.m_uiPositionX) eHorizontal = Directions::kCenter; + else if (cDestination.m_uiPositionX > cOrigin.m_uiPositionX) eHorizontal = Directions::kRight; + else eHorizontal = Directions::kLeft; + + //Get the vertical direction + Directions eVertical; + if (cDestination.m_uiPositionY == cOrigin.m_uiPositionY) eVertical = Directions::kCenter; + else if (cDestination.m_uiPositionY > cOrigin.m_uiPositionY) eVertical = Directions::kDown; + else eVertical = Directions::kUp; + + //Compare both horizontal and vertical to get the final heading + switch (eHorizontal) { + case Directions::kCenter: + + switch (eVertical) { + case Directions::kCenter: return Directions::kCenter; + case Directions::kUp: return Directions::kUp; + case Directions::kDown: return Directions::kDown; + default: break; //Shouldnt reach + } + + break; + + case Directions::kRight: + + switch (eVertical) { + case Directions::kCenter: return Directions::kRight; + case Directions::kUp: return Directions::kRightUp; + case Directions::kDown: return Directions::kRightDown; + default: break; //Shouldnt reach + + } + + case Directions::kLeft: + + switch (eVertical) { + case Directions::kCenter: return Directions::kLeft; + case Directions::kUp: return Directions::kLeftUp; + case Directions::kDown: return Directions::kLeftDown; + default: break; //Shouldnt reach + } + + //Shouldnt reach + default: break; + } + + throw std::runtime_error("incorrect implementation of direction between tiles"); + +} + +const Tile& Map::getTile(const Tile &cOrigin, Directions direction) const { } @@ -83,7 +139,7 @@ const std::vector Map::getNeighbors(const Tile &cTile) const { const Tile& cNeighbor = m_cData[index(uiRow, uiCol)]; //Check for water tiles - if (cNeighbor.eType == Tile::TileType::kWater) continue; + if (cNeighbor.eType == Tile::Types::kWater) continue; else { /* @@ -93,36 +149,36 @@ const std::vector Map::getNeighbors(const Tile &cTile) const { * been checked and found not to be Water, it's valid. */ - switch (cTile.getDirection(cNeighbor)) { - case Tile::Direction::kLeftDown: + switch (getDirection(cTile, cNeighbor)) { + case Directions::kLeftDown: //Must get the 'Down' and 'Left' neighbors of current tile - they must exist due to sideways existing - if (getTile(cTile, Tile::Direction::kLeft).eType == Tile::TileType::kWater) continue; - if (getTile(cTile, Tile::Direction::kDown).eType == Tile::TileType::kWater) continue; + if (getTile(cTile, Directions::kLeft).eType == Tile::Types::kWater) continue; + if (getTile(cTile, Directions::kDown).eType == Tile::Types::kWater) continue; vcNeighbors.push_back(&cNeighbor); break; - case Tile::Direction::kLeftUp: + case Directions::kLeftUp: //Must get the 'Up' and 'Left' neighbors of current tile - they must exist due to sideways existing - if (getTile(cTile, Tile::Direction::kLeft).eType == Tile::TileType::kWater) continue; - if (getTile(cTile, Tile::Direction::kUp).eType == Tile::TileType::kWater) continue; + if (getTile(cTile, Directions::kLeft).eType == Tile::Types::kWater) continue; + if (getTile(cTile, Directions::kUp).eType == Tile::Types::kWater) continue; vcNeighbors.push_back(&cNeighbor); break; - case Tile::Direction::kRightDown: + case Directions::kRightDown: //Must get the 'Down' and 'Right' neighbors of current tile - they must exist due to sideways existing - if (getTile(cTile, Tile::Direction::kRight).eType == Tile::TileType::kWater) continue; - if (getTile(cTile, Tile::Direction::kDown).eType == Tile::TileType::kWater) continue; + if (getTile(cTile, Directions::kRight).eType == Tile::Types::kWater) continue; + if (getTile(cTile, Directions::kDown).eType == Tile::Types::kWater) continue; vcNeighbors.push_back(&cNeighbor); break; - case Tile::Direction::kRightUp: + case Directions::kRightUp: //Must get the 'Up' and 'Right' neighbors of current tile - they must exist due to sideways existing - if (getTile(cTile, Tile::Direction::kRight).eType == Tile::TileType::kWater) continue; - if (getTile(cTile, Tile::Direction::kUp).eType == Tile::TileType::kWater) continue; + if (getTile(cTile, Directions::kRight).eType == Tile::Types::kWater) continue; + if (getTile(cTile, Directions::kUp).eType == Tile::Types::kWater) continue; vcNeighbors.push_back(&cNeighbor); break; diff --git a/Ex1/Map.hpp b/Ex1/Map.hpp index e9e09c2..5acb19f 100644 --- a/Ex1/Map.hpp +++ b/Ex1/Map.hpp @@ -26,7 +26,19 @@ class Map { constexpr size_t index(size_t x, size_t y) const; public: - + + enum class Directions : int_fast8_t { + kRight = 0, + kLeft, + kUp, + kDown, + kRightDown, + kRightUp, + kLeftDown, + kLeftUp, + kCenter + }; + /** Constructor */ Map(const std::string& strFormattedMap); @@ -36,7 +48,9 @@ class Map { Tile getStartTile() const; Tile getEndTile() const; - const Tile& getTile(const Tile& cOrigin, Tile::Direction) const; + Directions getDirection(const Tile &cOrigin, const Tile& cDestination) const; + + const Tile& getTile(const Tile& cOrigin, Directions direction) const; const std::vector getNeighbors(const Tile& cTile) const; diff --git a/Ex1/README.md b/Ex1/README.md new file mode 100644 index 0000000..59fbd96 --- /dev/null +++ b/Ex1/README.md @@ -0,0 +1,2 @@ +# Artificial-Intelligence-Ex1 +This repository contains my solution for implementing a set of rules on a map, while traversing it via IDS/UCS. diff --git a/Ex1/Tile.cpp b/Ex1/Tile.cpp index 3ac7314..54c59e5 100644 --- a/Ex1/Tile.cpp +++ b/Ex1/Tile.cpp @@ -10,68 +10,12 @@ #include #include -Tile::Tile(TileType eTileType, size_t uiPositionX, size_t uiPositionY) : +Tile::Tile(Types eTileType, size_t uiPositionX, size_t uiPositionY) : eType(eTileType), m_uiPositionX(uiPositionX), m_uiPositionY(uiPositionY) { } Tile::Tile(const Tile& cTile) : eType(cTile.eType), m_uiPositionX(cTile.m_uiPositionX), m_uiPositionY(cTile.m_uiPositionY) { } -Tile::Direction Tile::getDirection(const Tile &cTile) const { - - //Important to remember that highest is 0 and lowest is N-1, sideways is normal - - //Get the horizontal direction - Direction eHorizontal; - if (cTile.m_uiPositionX == m_uiPositionX) eHorizontal = Direction::kCenter; - else if (cTile.m_uiPositionX > m_uiPositionX) eHorizontal = Direction::kRight; - else eHorizontal = Direction::kLeft; - - //Get the vertical direction - Direction eVertical; - if (cTile.m_uiPositionY == m_uiPositionY) eVertical = Direction::kCenter; - else if (cTile.m_uiPositionY > m_uiPositionY) eVertical = Direction::kDown; - else eVertical = Direction::kUp; - - //Compare both horizontal and vertical to get the final heading - switch (eHorizontal) { - case Direction::kCenter: - - switch (eVertical) { - case Direction::kCenter: return Direction::kCenter; - case Direction::kUp: return Direction::kUp; - case Direction::kDown: return Direction::kDown; - default: break; //Shouldnt reach - } - - break; - - case Direction::kRight: - - switch (eVertical) { - case Direction::kCenter: return Direction::kRight; - case Direction::kUp: return Direction::kRightUp; - case Direction::kDown: return Direction::kRightDown; - default: break; //Shouldnt reach - - } - - case Direction::kLeft: - - switch (eVertical) { - case Direction::kCenter: return Direction::kLeft; - case Direction::kUp: return Direction::kLeftUp; - case Direction::kDown: return Direction::kLeftDown; - default: break; //Shouldnt reach - } - - //Shouldnt reach - default: break; - } - - throw std::runtime_error("incorrect implementation of direction between tiles"); - -} - bool Tile::operator==(const Tile &cTile) const { return ((m_uiPositionX == cTile.m_uiPositionX) && (m_uiPositionY == cTile.m_uiPositionY)); @@ -89,12 +33,12 @@ std::ostream& operator<< (std::ostream &out, const Tile &cTile) { std::string strType; switch (cTile.eType) { - case Tile::TileType::kStart : strType = "Start"; break; - case Tile::TileType::kEnd : strType = "End"; break; - case Tile::TileType::kHill : strType = "Hill"; break; - case Tile::TileType::kRoad : strType = "Road"; break; - case Tile::TileType::kDirt : strType = "Dirt"; break; - case Tile::TileType::kWater : strType = "Water"; break; + case Tile::Types::kStart : strType = "Start"; break; + case Tile::Types::kEnd : strType = "End"; break; + case Tile::Types::kHill : strType = "Hill"; break; + case Tile::Types::kRoad : strType = "Road"; break; + case Tile::Types::kDirt : strType = "Dirt"; break; + case Tile::Types::kWater : strType = "Water"; break; } out << "Type: \t" << strType << ", \tLocation: (" << cTile.m_uiPositionX << ", " << cTile.m_uiPositionY << ")"; diff --git a/Ex1/Tile.hpp b/Ex1/Tile.hpp index be21a38..86d3ecb 100644 --- a/Ex1/Tile.hpp +++ b/Ex1/Tile.hpp @@ -23,7 +23,7 @@ class Tile { public: - enum class TileType : std::int_fast8_t { + enum class Types : std::int_fast8_t { kRoad = 0, kDirt, kHill, @@ -32,30 +32,17 @@ class Tile { kEnd }; - enum class Direction : int_fast8_t { - kRight = 0, - kLeft, - kUp, - kDown, - kRightDown, - kRightUp, - kLeftDown, - kLeftUp, - kCenter - }; - + //Public Variables - const TileType eType; + const Types eType; /** Constructor */ - Tile(TileType eTileType, size_t uiPositionX, size_t uiPositionY); + Tile(Types eTileType, size_t uiPositionX, size_t uiPositionY); /** Copy Constructor */ Tile(const Tile& cTile); - Direction getDirection(const Tile& cTile) const; - /** opeartor == */ bool operator==(const Tile& cTile) const; bool operator!=(const Tile& cTile) const;