Skip to content

Instantly share code, notes, and snippets.

@ramachandrajr
Created July 3, 2017 10:46
Show Gist options
  • Save ramachandrajr/18d50d2a3b19c6ac92c03a70fd032f1a to your computer and use it in GitHub Desktop.
Save ramachandrajr/18d50d2a3b19c6ac92c03a70fd032f1a to your computer and use it in GitHub Desktop.
A bruteforce program to solve mazes
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
// Used for proper response structuring.
// reduces code on check_for_dots and check_for_xs.
struct Response
{
bool tf;
char maze[][12];
int col;
int row;
};
typedef Response res;
// Shows maze
void show(char [][12]);
// Check for some character.
bool check_for(char, char [][12], int, int);
// Traverse through the maze.
void traverse(char [][12], int, int, int, int);
// Checks for dots
res check_for_dots(char [][12], int, int);
// Checks for x's
res check_for_xs(char [][12], int, int);
int main()
{
// start
int start_row = 4;
int start_col = 11;
// end
int end_row = 2;
int end_col = 0;
char maze[12][12] = {
{ '#', '#', '#', '#', '#', '#', '#', '#', '#' ,'#', '#', '#' },
{ '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
{ '.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
{ '#', '#', '#', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '.' },
{ '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }
};
traverse(maze, 4, 11, 2, 0);
show(maze);
cout << "\n\nGame comes to an end!\n\n" << endl;
return 0;
}
/**
* Traverse through given maze, one step at a time.
* @param {char[][12]} maze
* @param {int} row maze row.
* @param {int} col maze column.
* @param {int} end_row Row to end game.
* @param {int} end_col Column to end game.
*/
void traverse(char maze[][12], int row, int col, int end_row, int end_col)
{
show(maze);
// EOG
if (row == end_row, col == end_col)
{
maze[end_row][end_col] = 'X';
return;
}
res r1 = check_for_dots(maze, row, col);
if (r1.tf == true)
return traverse(maze, r1.row, r1.col, end_row, end_col);
// Couldn't find a dot at all.
else {
res r2 = check_for_xs(maze, row, col);
if (r2.tf == true)
return traverse(maze, r2.row, r2.col, end_row, end_col);
// logic error, this will never be hit.
else
{
cerr << "Logic error in the maze!" << endl;
exit(1);
}
}
}
/**
* Checks for given identifier in a given row and column of maze.
* @param {char} identifier Symbol to find.
* @param {char[][12]} maze
* @param {int} row maze row.
* @param {int} col maze column.
*/
bool check_for(char identifier, char maze[][12], int row, int col)
{
if (maze[row][col] == identifier)
return true;
// Not found.
else
return false;
}
/**
* Prints maze.
* @param {char[][12]} maze
*/
void show(char maze[][12])
{
cout << "\n\nMaze" << endl;
cout << "====\n" << endl;
for (int i = 0; i < 11; ++i) {
for (int j = 0; j < 11; ++j)
cout << " " << maze[i][j];
cout << endl;
}
cout << "\n====\n\n" << endl;
}
/**
* Checks for dots.
* @param {char[][12]} maze
* @param {int} row maze row.
* @param {int} col maze column.
*/
res check_for_dots(char maze[][12], int row, int col)
{
res r1;
r1.tf = true;
r1.row = row;
r1.col = col;
char default_cell_character = maze[row][col];
// Set to a value first, change back to default if does not satisfy some
// conditions. (saves 4 lines of code).
maze[row][col] = 'X';
// Check right
if (col != 11 && check_for('.', maze, row, col+1))
r1.col = col+1;
// Check left
else if (col != 0 && check_for('.', maze, row, col-1))
r1.col = col-1;
// Check top
else if (row != 0 && check_for('.', maze, row-1, col))
r1.row = row-1;
// Check bottom
else if (row != 11 && check_for('.', maze, row+1, col))
r1.row = row+1;
// catch for all outcasts
else if (row == 0 || row == 11 || col == 0 || col == 11) {
maze[row][col] = default_cell_character;
}
// Dot not found.
else {
maze[row][col] = default_cell_character;
r1.tf = false;
}
return r1;
}
/**
* Checks for xs.
* @param {char[][12]} maze
* @param {int} row maze row.
* @param {int} col maze column.
*/
res check_for_xs(char maze[][12], int row, int col)
{
res r1;
r1.tf = true;
r1.row = row;
r1.col = col;
char default_cell_character = maze[row][col];
// Set to '@' at first and change back to 'X' if necessary(saves 4 lines.)
maze[row][col] = '@';
// Check right
if (col != 11 && check_for('X', maze, row, col+1))
r1.col = col+1;
// Check left
else if (col != 0 && check_for('X', maze, row, col-1))
r1.col = col-1;
// Check top
else if (row != 0 && check_for('X', maze, row-1, col))
r1.row = row-1;
// Check bottom
else if (row != 11 && check_for('X', maze, row+1, col))
r1.row = row+1;
// catch for all outcasts
else if (row == 0 || row == 11 || col == 0 || col == 11)
maze[row][col] = default_cell_character;
// Couldn't find an 'X' either! Hence blocked!
else {
cerr << "There's something wrong with the maze!!" << endl;
exit(1);
}
return r1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment