Last active
June 28, 2017 05:34
-
-
Save jason790228/f3e473584d9fe2475556b2dc64b22737 to your computer and use it in GitHub Desktop.
118
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "gtest\gtest.h" | |
#pragma once | |
#include "Guide.h" | |
#include <iostream> | |
using namespace std; | |
Direction::eDirection mapping_direction(char s) | |
{ | |
if (s == 'N') | |
return Direction::eDirection::NORTH; | |
else if (s == 'E') | |
return Direction::eDirection::EAST; | |
else if (s == 'S') | |
return Direction::eDirection::SOUTH; | |
else if (s == 'W') | |
return Direction::eDirection::WEST; | |
return Direction::eDirection::UNKNOWN; | |
} | |
char remapping_direction(Direction::eDirection d) | |
{ | |
if (d == Direction::eDirection::NORTH) | |
return 'N'; | |
else if (d == Direction::eDirection::EAST) | |
return 'E'; | |
else if (d == Direction::eDirection::SOUTH) | |
return 'S'; | |
else if (d == Direction::eDirection::WEST) | |
return 'W'; | |
return Direction::eDirection::UNKNOWN; | |
} | |
Guide::eDangerousLevel mapping_action(Guide& maper, char s) | |
{ | |
if (s == 'R') | |
return maper.turn_right(); | |
else if (s == 'L') | |
return maper.turn_left(); | |
else if (s == 'F') | |
return maper.move_forward(); | |
return Guide::eDangerousLevel::VERY_DANGEROUS; | |
} | |
void run(Guide& maper, string s) | |
{ | |
for each (char t in s) | |
{ | |
if (mapping_action(maper, t) == Guide::eDangerousLevel::VERY_DANGEROUS) | |
{ | |
cout << maper.get_position().x << " " << maper.get_position().y << " " << remapping_direction(maper.get_direction().get_direction()) << " LOST" << endl; | |
return; | |
} | |
} | |
cout << maper.get_position().x << " " << maper.get_position().y << " " << remapping_direction(maper.get_direction().get_direction()) << endl; | |
return; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
int a, b; | |
cin >> a >> b; | |
Guide maper(Point(a, b)); | |
string input; | |
while (!cin.eof()) | |
{ | |
int x, y; | |
char d; | |
cin >> x >> y >> d; | |
maper.set_position(Point(x, y)); | |
maper.set_direction(mapping_direction(d)); | |
cin.ignore(1024, '\n'); | |
getline(cin, input); | |
run(maper, input); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use mod to implenment direction | |
// North = 0, East = 1, South = 2, West = 3 | |
class Direction | |
{ | |
public: | |
enum eDirection | |
{ | |
UNKNOWN = -1, | |
NORTH, | |
EAST, | |
SOUTH, | |
WEST, | |
DIRECTION_SIZE | |
}; | |
public: | |
Direction() :m_direction(NORTH) {} | |
Direction(eDirection direction) :m_direction(direction) {} | |
eDirection get_direction() { return (eDirection)m_direction; } | |
eDirection set_direction(const eDirection& direction){ m_direction = direction; } | |
eDirection turn_left() | |
{ | |
m_direction = (m_direction-1) % DIRECTION_SIZE; | |
return (eDirection)((m_direction >= 0) ? m_direction : m_direction += DIRECTION_SIZE); | |
} | |
eDirection turn_right() | |
{ | |
m_direction = (m_direction+1) % DIRECTION_SIZE; | |
return (eDirection)((m_direction >= 0) ? m_direction : m_direction += DIRECTION_SIZE); | |
} | |
Direction& operator = (const Direction& other) | |
{ | |
if (this->m_direction == other.m_direction) return *this; | |
m_direction = other.m_direction; | |
return *this; | |
} | |
bool operator == (const Direction& other) const | |
{ | |
return this->m_direction == other.m_direction; | |
} | |
private: | |
int m_direction; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Guide.h" | |
#include <algorithm> | |
Guide::eDangerousLevel Guide::turn_right() | |
{ | |
m_direction.turn_right(); | |
return eDangerousLevel::NO_DANGEROUS; | |
} | |
Guide::eDangerousLevel Guide::turn_left() | |
{ | |
m_direction.turn_left(); | |
return eDangerousLevel::NO_DANGEROUS; | |
} | |
Guide::eDangerousLevel Guide::move_forward() | |
{ | |
Point temp_p(m_position); | |
if (m_direction == Direction::NORTH) | |
{ | |
temp_p.y++; | |
} | |
else if (m_direction == Direction::EAST) | |
{ | |
temp_p.x++; | |
} | |
else if (m_direction == Direction::SOUTH) | |
{ | |
temp_p.y--; | |
} | |
else if (m_direction == Direction::WEST) | |
{ | |
temp_p.x--; | |
} | |
if (is_over_edge(temp_p)) | |
{ | |
if (is_dangerous(m_position)) | |
{ | |
return eDangerousLevel::A_BIT_DANGEROUS; | |
} | |
else | |
{ | |
m_dangerous_position_direction[m_position] = m_direction; | |
return eDangerousLevel::VERY_DANGEROUS; | |
} | |
} | |
m_position = temp_p; | |
return eDangerousLevel::NO_DANGEROUS; | |
} | |
bool Guide::is_over_edge(const Point& position) | |
{ | |
if (position <= m_max_edge_point && m_min_edge_point <= position) | |
{ | |
return false; | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Point.h" | |
#include "Direction.h" | |
#include <map> | |
class Guide | |
{ | |
public: | |
Guide() : | |
m_min_edge_point(Point(0, 0)), | |
m_max_edge_point(Point(50, 50)) | |
{ | |
} | |
Guide(const Point& edge_point) : | |
m_min_edge_point(Point(0, 0)), | |
m_max_edge_point(edge_point) | |
{ | |
} | |
public: | |
enum eDangerousLevel | |
{ | |
NUKNOWN, | |
NO_DANGEROUS, | |
A_BIT_DANGEROUS, | |
VERY_DANGEROUS, | |
}; | |
public: | |
Point get_position(){ return m_position; } | |
void set_position(const Point& position){ m_position = position; } | |
Direction get_direction(){ return m_direction; } | |
void set_direction(const Direction& direction){ m_direction = direction; } | |
Point get_edge_point(){ return m_max_edge_point; } | |
void set_edge_point(const Point& edge_point){ m_max_edge_point = edge_point; } | |
eDangerousLevel turn_right(); | |
eDangerousLevel turn_left(); | |
eDangerousLevel move_forward(); | |
private: | |
Point m_position; | |
Direction m_direction; | |
Point m_max_edge_point; | |
Point m_min_edge_point; | |
std::map<Point, Direction> m_dangerous_position_direction; | |
private: | |
bool is_over_edge(const Point& position); | |
bool is_dangerous(const Point& position); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point | |
{ | |
public: | |
double x; | |
double y; | |
Point() :x(0), y(0) {} | |
Point(double x_coor, double y_coor): | |
x(x_coor), | |
y(y_coor) | |
{ | |
} | |
bool operator < (const Point& point) const | |
{ | |
if (this->x < point.x) return true; | |
if (this->x == point.x) | |
{ | |
if (this->y < point.y) return true; | |
} | |
return false; | |
} | |
bool operator > (const Point& point) const | |
{ | |
return (this->x > point.x && (this->y > point.y)); | |
} | |
bool operator <= (const Point& point) const | |
{ | |
return (this->x <= point.x && (this->y <= point.y)); | |
} | |
Point operator + (const Point& point) const | |
{ | |
return Point(this->x + point.x, this->y + point.y); | |
} | |
Point operator - (const Point& point) const | |
{ | |
return Point(this->x - point.x, this->y - point.y); | |
} | |
Point& operator = (const Point& other) | |
{ | |
if (*this == other) return *this; | |
x = other.x; | |
y = other.y; | |
return *this; | |
} | |
bool operator == (const Point& other) const | |
{ | |
return (this->x == other.x) && (this->y == other.y); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "gtest\gtest.h" | |
#include "Guide.h" | |
TEST(DirectionTurnLeftTest, DirectionTurnLeftTest) | |
{ | |
Direction test(Direction::eDirection::NORTH); | |
EXPECT_EQ(Direction::eDirection::WEST, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::SOUTH, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::EAST, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::NORTH, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::WEST, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::SOUTH, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::EAST, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::NORTH, test.turn_left()); | |
} | |
TEST(DirectionTurnRightTest, DirectionTurnRightTest) | |
{ | |
Direction test(Direction::eDirection::NORTH); | |
EXPECT_EQ(Direction::eDirection::EAST, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::SOUTH, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::WEST, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::NORTH, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::EAST, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::SOUTH, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::WEST, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::NORTH, test.turn_right()); | |
} | |
TEST(DirectionMapTurnRightTest, DirectionMapTurnRightTest) | |
{ | |
Guide test; | |
EXPECT_EQ(true, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::EAST, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::SOUTH, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::WEST, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::NORTH, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::EAST, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::SOUTH, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::WEST, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_right()); | |
EXPECT_EQ(Direction::eDirection::NORTH, test.get_direction().get_direction()); | |
} | |
TEST(DirectionMapTurnLeftTest, DirectionMapTurnLeftTest) | |
{ | |
Guide test; | |
EXPECT_EQ(true, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::WEST, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::SOUTH, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::EAST, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::NORTH, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::WEST, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::SOUTH, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::EAST, test.get_direction().get_direction()); | |
EXPECT_EQ(true, test.turn_left()); | |
EXPECT_EQ(Direction::eDirection::NORTH, test.get_direction().get_direction()); | |
} | |
TEST(DirectionMapMoveForwardTest, DirectionMapMoveForwardTest) | |
{ | |
Guide test(Point(3, 5)); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::VERY_DANGEROUS, test.move_forward()); | |
} | |
TEST(DirectionMapCornerTest, DirectionMapCornerTest) | |
{ | |
Guide test(Point(5, 3)); | |
test.set_position(Point(1, 1)); | |
test.set_direction(Direction::eDirection::EAST); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_right()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_right()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_right()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_right()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
test.set_position(Point(3, 2)); | |
test.set_direction(Direction::eDirection::NORTH); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_right()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_right()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_left()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_left()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::VERY_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(true, test.turn_right()); | |
EXPECT_EQ(true, test.turn_right()); | |
EXPECT_EQ(true, test.move_forward()); | |
EXPECT_EQ(true, test.turn_left()); | |
EXPECT_EQ(true, test.turn_left()); | |
test.set_position(Point(0, 3)); | |
test.set_direction(Direction::eDirection::WEST); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_left()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_left()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_left()); | |
EXPECT_EQ(Guide::eDangerousLevel::A_BIT_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_left()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_left()); | |
} | |
TEST(DirectionMapRandomTest, DirectionMapRandomTest) | |
{ | |
Guide test(Point(2, 2)); | |
test.set_position(Point(1, 1)); | |
test.set_direction(Direction::eDirection::EAST); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_left()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::VERY_DANGEROUS, test.move_forward()); | |
test.set_position(Point(1, 1)); | |
test.set_direction(Direction::eDirection::EAST); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_left()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::A_BIT_DANGEROUS, test.move_forward()); | |
test.set_position(Point(1, 1)); | |
test.set_direction(Direction::eDirection::NORTH); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.turn_right()); | |
EXPECT_EQ(Guide::eDangerousLevel::NO_DANGEROUS, test.move_forward()); | |
EXPECT_EQ(Guide::eDangerousLevel::A_BIT_DANGEROUS, test.move_forward()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment