Finish the CYB Project MazeRunner
This commit is contained in:
parent
bf83664da4
commit
9939ce0dcf
1 changed files with 238 additions and 36 deletions
|
|
@ -10,43 +10,245 @@
|
||||||
#define SOUTH 2
|
#define SOUTH 2
|
||||||
#define WEST 3
|
#define WEST 3
|
||||||
|
|
||||||
|
// function prototypes
|
||||||
|
void breadcrumb(int row, int column);
|
||||||
|
bool checkForwardBlocked(int row, int column, int heading);
|
||||||
|
bool checkLeftBlocked(int row, int column, int heading);
|
||||||
|
bool checkRightBlocked(int row, int column, int heading);
|
||||||
|
int turnLeft(int heading);
|
||||||
|
int turnRight(int heading);
|
||||||
|
|
||||||
void runner_solve(void) {
|
void runner_solve(void) {
|
||||||
// Use a few variables to keep track of the current row and column position
|
// set the runner in the maze
|
||||||
// of the "runner". The runner should always start at the 'S' symbol in the
|
int row = 1;
|
||||||
// maze. Luckily, the 'S' symbol is always in the same place
|
int column = 2;
|
||||||
|
int heading = EAST;
|
||||||
|
|
||||||
// You should also keep track of which way the runner is facing. You can use
|
// "indefinately move through the maze"
|
||||||
// the preprocessor defines at the top of this file to make your life easier
|
for (int i = 0; i < 100; i++)
|
||||||
// instead of just having to remember "0 is North, 1 is East, etc."
|
{
|
||||||
// Basically, any time you use NORTH in your code, a zero will be substituted
|
// check to see if the end of the maze is here
|
||||||
// in for it at compile time.
|
if (maze_get_char(row, column) == 'E')
|
||||||
|
{
|
||||||
// Ok, algorithm time:
|
return;
|
||||||
//
|
}
|
||||||
// 1. As long as you are not standing on the 'E', do the following:
|
// set a breadcrumb through the maze
|
||||||
// 2. Turn to your left
|
breadcrumb(row, column);
|
||||||
// 3. Can you go that way? I.E., is there not a wall in front of you?
|
// check to see if the forward position is blocked
|
||||||
// 4. If your path is blocked, turn to your right and go back to (3)
|
if (checkForwardBlocked(row, column, heading))
|
||||||
// 5. If your path is open, throw down the appropriate breadcrumb (see note)
|
{
|
||||||
// 6. Move forward and go back to (1)
|
// check to see if the left position is blocked
|
||||||
// 7. You are standing on the 'E', STOP, you're done!
|
if (checkLeftBlocked(row, column, heading))
|
||||||
|
{
|
||||||
// NOTE: By "appropriate breadcrumb" I mean this:
|
// check to see if the right position is blocked
|
||||||
// If you are in an empty square, the breadcrumb should be '.'
|
if (checkRightBlocked(row, column, heading))
|
||||||
// If you are in a square with '.', the breadcrumb should be 'o'
|
{
|
||||||
// If you are in a square with 'o', the breadcrumb should be 'O'
|
// if it is, then turn left instead.
|
||||||
// If you are in a square with 'O', the breadcrumb should be '@'
|
heading = turnLeft(heading);
|
||||||
//
|
}
|
||||||
// Here is an example of a solved maze:
|
else
|
||||||
//
|
{
|
||||||
// ###########################
|
// otherwise, turn right instead.
|
||||||
// #S....#.oo..#.oooOoooooo..#
|
heading = turnRight(heading);
|
||||||
// #####.###.#.#####o#####.#.#
|
}
|
||||||
// # #.#...#.....#.#.....#.#
|
}
|
||||||
// # # #.#.#######.###.#####.#
|
}
|
||||||
// # # #...# #.#...#.....#
|
// keep track of the heading through the maze, "moving" the 'S'
|
||||||
// # ####### # ###.#.###.#####
|
if (heading == NORTH)
|
||||||
// # # ...# ....E#
|
{
|
||||||
// ###########################
|
row = row + 1;
|
||||||
|
}
|
||||||
|
if (heading == EAST)
|
||||||
|
{
|
||||||
|
column = column + 1;
|
||||||
|
}
|
||||||
|
if (heading == SOUTH)
|
||||||
|
{
|
||||||
|
row = row - 1;
|
||||||
|
}
|
||||||
|
if (heading == WEST)
|
||||||
|
{
|
||||||
|
column = column - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// function definitions, given logical breakdown of the problem.
|
||||||
|
int turnLeft(int heading)
|
||||||
|
{
|
||||||
|
// turns left given the current direction.
|
||||||
|
if (heading == NORTH)
|
||||||
|
{
|
||||||
|
heading = WEST;
|
||||||
|
}
|
||||||
|
if (heading == EAST)
|
||||||
|
{
|
||||||
|
heading = NORTH;
|
||||||
|
}
|
||||||
|
if (heading == SOUTH)
|
||||||
|
{
|
||||||
|
heading = EAST;
|
||||||
|
}
|
||||||
|
if (heading == WEST)
|
||||||
|
{
|
||||||
|
heading = SOUTH;
|
||||||
|
}
|
||||||
|
// returns the new heading.
|
||||||
|
return heading;
|
||||||
|
}
|
||||||
|
|
||||||
|
int turnRight(int heading)
|
||||||
|
{
|
||||||
|
// turns right given current direction
|
||||||
|
if (heading == NORTH)
|
||||||
|
{
|
||||||
|
heading = EAST;
|
||||||
|
}
|
||||||
|
if (heading == EAST)
|
||||||
|
{
|
||||||
|
heading = SOUTH;
|
||||||
|
}
|
||||||
|
if (heading == SOUTH)
|
||||||
|
{
|
||||||
|
heading = WEST;
|
||||||
|
}
|
||||||
|
if (heading == WEST)
|
||||||
|
{
|
||||||
|
heading = NORTH;
|
||||||
|
}
|
||||||
|
// returns the new direction
|
||||||
|
return heading;
|
||||||
|
}
|
||||||
|
|
||||||
|
void breadcrumb(int row, int column)
|
||||||
|
{
|
||||||
|
// leaves a "breadcrumb" as given by the spec
|
||||||
|
if(maze_get_char(row, column) == '.')
|
||||||
|
{
|
||||||
|
// set a 'o'
|
||||||
|
maze_set_char(row, column, 'o');
|
||||||
|
}
|
||||||
|
else if (maze_get_char(row, column) == 'o')
|
||||||
|
{
|
||||||
|
// set a 'O'
|
||||||
|
maze_set_char(row, column, 'O');
|
||||||
|
}
|
||||||
|
else if (maze_get_char(row, column) == 'O')
|
||||||
|
{
|
||||||
|
// set a '@'
|
||||||
|
maze_set_char(row, column, '@');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// set a '.'
|
||||||
|
maze_set_char(row, column, '.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool checkForwardBlocked(int row, int column, int heading)
|
||||||
|
{
|
||||||
|
// checks to see if the path is blocked from where the runner is in each
|
||||||
|
// direction.
|
||||||
|
if (heading == NORTH)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row - 1, column) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (heading == EAST)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row, column + 1) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (heading == SOUTH)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row + 1, column) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (heading == WEST)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row, column - 1) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// return false if there is no # within the given area.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool checkLeftBlocked(int row, int column, int heading)
|
||||||
|
{
|
||||||
|
// checks to see if the path is blocked from where the runner is in each
|
||||||
|
// direction.
|
||||||
|
if (heading == NORTH)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row, column - 1) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (heading == EAST)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row - 1, column) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (heading == SOUTH)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row, column + 1) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (heading == WEST)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row + 1, column) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// return false if there is no # within the given area.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool checkRightBlocked(int row, int column, int heading)
|
||||||
|
{
|
||||||
|
// checks to see if the path is blocked from where the runner is in each
|
||||||
|
// direction.
|
||||||
|
if (heading == NORTH)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row, column + 1) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (heading == EAST)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row + 1, column + 1) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (heading == SOUTH)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row, column - 1) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (heading == WEST)
|
||||||
|
{
|
||||||
|
if (maze_get_char(row - 1, column) == '#')
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// return false if there is no # within the given area.
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue