Skip to content

Commit

Permalink
Made changes to responsibility of direction ,logically is decided by …
Browse files Browse the repository at this point in the history
…map and not by tile itself (tiles don't know about relation to other tiles, but map does).

Added license and readme.
  • Loading branch information
Maxim Vainshtein authored and Maxim Vainshtein committed Nov 15, 2015
1 parent 7e2805c commit 1f46472
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 103 deletions.
6 changes: 6 additions & 0 deletions Ex1.xcodeproj/project.pbxproj
Expand Up @@ -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 */
Expand Down Expand Up @@ -41,6 +42,8 @@
9450125B1BF7A38A00913F6C /* UCSAlgorithm.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = UCSAlgorithm.hpp; sourceTree = "<group>"; };
9450125E1BF7A9F000913F6C /* Tile.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Tile.cpp; sourceTree = "<group>"; };
9450125F1BF7A9F000913F6C /* Tile.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Tile.hpp; sourceTree = "<group>"; };
94D023D61BF88ACE00CEF4A8 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
94D023D91BF88B3800CEF4A8 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -76,6 +79,8 @@
9450125D1BF7A39000913F6C /* Algorithm */,
9450124E1BF7910300913F6C /* Map */,
9423719D1BF27E5D00F46C34 /* main.cpp */,
94D023D61BF88ACE00CEF4A8 /* README.md */,
94D023D91BF88B3800CEF4A8 /* LICENSE */,
945012521BF7A35500913F6C /* input.txt */,
);
path = Ex1;
Expand Down Expand Up @@ -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 */,
Expand Down
21 changes: 21 additions & 0 deletions 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.
98 changes: 77 additions & 21 deletions Ex1/Map.cpp
Expand Up @@ -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;
}

Expand All @@ -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 {

}

Expand All @@ -83,7 +139,7 @@ const std::vector<const Tile*> 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 {

/*
Expand All @@ -93,36 +149,36 @@ const std::vector<const Tile*> 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;

Expand Down
18 changes: 16 additions & 2 deletions Ex1/Map.hpp
Expand Up @@ -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);

Expand All @@ -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<const Tile*> getNeighbors(const Tile& cTile) const;

Expand Down
2 changes: 2 additions & 0 deletions 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.
70 changes: 7 additions & 63 deletions Ex1/Tile.cpp
Expand Up @@ -10,68 +10,12 @@
#include <string>
#include <exception>

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));
Expand All @@ -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 << ")";
Expand Down
21 changes: 4 additions & 17 deletions Ex1/Tile.hpp
Expand Up @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit 1f46472

Please sign in to comment.