Created
June 19, 2018 14:55
-
-
Save sh-zam/a5f14c0a6b92ab41a7de9e9a0c7eca78 to your computer and use it in GitHub Desktop.
Robomaze puzzle solution using wall follower algorithm.
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 RoboAI_Demo | |
{ | |
using TT = TileMap::TileType; | |
using Action = Robo::Action; | |
bool isRightWallEnding = false; | |
bool isRightWall = true; | |
bool isLeftWall = true; | |
bool hasGoalReached = false; | |
public: | |
Action Plan( std::array<TT,3> view ) | |
{ | |
TT left = view[0]; | |
TT front = view[1]; | |
TT right = view[2]; | |
if (hasGoalReached) | |
{ | |
return Action::Done; | |
} | |
if (front == TT::Goal) | |
{ | |
hasGoalReached = true; | |
return Action::MoveForward; | |
} | |
if (isRightWallEnding) | |
{ | |
isRightWallEnding = false; | |
assign(left, right); | |
return Action::TurnRight; | |
} | |
if (right == TT::Floor && front != TT::Wall) | |
{ | |
isRightWallEnding = true; | |
assign(left, right); | |
return Action::MoveForward; | |
} | |
if (isLeftWall && isRightWall && front == TT::Wall) | |
{ | |
assign(left, right); | |
return Action::TurnRight; | |
} | |
if (front == TT::Wall && isRightWall) | |
{ | |
assign(left, right); | |
return Action::TurnLeft; | |
} | |
if (front == TT::Wall) | |
{ | |
assign(left, right); | |
return Action::TurnRight; | |
} | |
assign(left, right); | |
return Action::MoveForward; | |
} | |
private: | |
void assign(TT left, TT right) | |
{ | |
isLeftWall = (left == TT::Wall) ? true : false; | |
isRightWall = (right == TT::Wall) ? true : false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment