Sign in to follow this  
Betrayer_of_Code

Code Organization

Recommended Posts

I finally "finished" my maze running program. I had been working on it for about a year. It shouldn't have taken that long, but there was a lot of stuff I didnt know when I started so I had to re write it several times. I would like to know if someone could look at my code and organize it some so it more easily read...I may be the only person on earth who can read it right now... Maze.zip

Share this post


Link to post
Share on other sites
Alright, here, ill put it in source boxes.

/*
Name: Maze Runner v2
Copyright: 2004
Author: Peter Bretton
Date: 19/09/04 19:19
Description: This will run a 30 x 30 maze, and find the "cheese", it's sucsess rate os mpt 100%
it is maybe 85% of the mazes that are possible. It is the prototype I am using to create "worlds"
for my ASCII graphics game.
*/

#include "Move.h"

#define MAP_WIDTH 30
#define MAP_HEIGHT 30
#define MAP_SQUARE 30
#define NORTH 1
#define SOUTH 2
#define EAST 3
#define WEST 4


//Simply a main menu function
int main()
{
nk = 0;

srand(GetTickCount());
int choice;
cout << "(1)Random\n";
cout << "(2)Test\n";
cout << "(3)Exit\n";
cin >> choice;

switch(choice)
{
case 1:
//create a random 30 x 30 maze
random_maze();
break;


case 2:
//load a pre-made 30 x 30 maze
load_maze();
break;

case 3:
//exit
exit(0);
break;

default:
load_maze();
break;
}
return 0;
}

/*This function is a first pass at the maze
if it has to backtrack or if it get's "stuck" then
it goes to the next function "backtrack" */

void walk_maze(int pass_maze[][MAP_WIDTH])
{
set_backtrack_flag = false;
set_btII_flag = false;

int col = 0;
int row = 0;

for(int r = 0; r < MAP_SQUARE; ++r)
{
for(int c = 0; c < MAP_SQUARE; ++c)
{
if(pass_maze[c][r] == 3)
{
col = c;
row = r;
}
}
}

if(pass_maze[col + 1][row] == 2 || pass_maze[col - 1][row] == 2 || pass_maze[col][row + 1] == 2 || pass_maze[col][row - 1] == 2)
{

cout << "\nMaze Solved!!\n";
cout << "Total Moves: " << nk << endl;
system("pause");
system("cls");
main();
}

if(pass_maze[col][row + 1] == 0)
{
choose_direction(pass_maze, SOUTH);
}

else if(pass_maze[col + 1][row] == 0)
{
choose_direction(pass_maze, EAST);
}


else if(pass_maze[col][row - 1] == 0)
{
choose_direction(pass_maze, NORTH);
}

else if(pass_maze[col - 1][row] == 0)
{
choose_direction(pass_maze, WEST);
}

if(pass_maze[col + 1][row] != 0 && pass_maze[col - 1][row] != 0 || pass_maze[col][row + 1] != 0 || pass_maze[col][row - 1] != 0)
{
//Go to the next function, which will run the maze again in a different way
backtrack(pass_maze);
}
}


/*This will run the maze as though the path created by walk_maze() didnt exist */
void backtrack(int pass_maze[][MAP_WIDTH])
{

set_backtrack_flag = true;
set_btII_flag = false;

int col = 0;
int row = 0;

//Find where the "mouse" is
for(int r = 0; r < MAP_SQUARE; ++r)
{
for(int c = 0; c < MAP_SQUARE; ++c)
{
if(pass_maze[c][r] == 3)
{
col = c;
row = r;
}
}
}

if(pass_maze[col + 1][row] == 2 || pass_maze[col - 1][row] == 2 || pass_maze[col][row + 1] == 2 || pass_maze[col][row - 1] == 2)
{

cout << "\nMaze Solved!!\n";
cout << "Total Moves: " << nk << endl;
system("pause");
system("cls");
main();
}

if(pass_maze[col][row - 1] == 0 || pass_maze[col][row + 1] == 0 || pass_maze[col - 1][row] == 0 || pass_maze[col + 1][row] == 0)
{
set_backtrack_flag = false;
draw_maze(pass_maze);
}
++nk;

if(pass_maze[col][row + 1] == 4)
{
set_backtrack_flag = true;
choose_direction(pass_maze, SOUTH);
}

else if(pass_maze[col + 1][row] == 4)
{
set_backtrack_flag = true;
choose_direction(pass_maze, EAST);
}

else if(pass_maze[col][row - 1] == 4)
{
set_backtrack_flag = true;
choose_direction(pass_maze, NORTH);
}

else if(pass_maze[col - 1][row] == 4)
{
set_backtrack_flag = true;
choose_direction(pass_maze, WEST);
}

else if(pass_maze[col + 1][row] != 4 && pass_maze[col - 1][row] != 4 || pass_maze[col][row + 1] != 4 || pass_maze[col][row - 1] != 4)
{
backtrackII(pass_maze);
}
}

//Another backtrack, just in case
void backtrackII(int pass_maze[][MAP_WIDTH])
{

set_btII_flag = true;
set_backtrack_flag = false;

int col = 0;
int row = 0;

for(int r = 0; r < MAP_SQUARE; ++r)
{
for(int c = 0; c < MAP_SQUARE; ++c)
{
if(pass_maze[c][r] == 3)
{
col = c;
row = r;
}
}
}

if(pass_maze[col + 1][row] == 2 || pass_maze[col - 1][row] == 2 || pass_maze[col][row + 1] == 2 || pass_maze[col][row - 1] == 2)
{

cout << "\nMaze Solved!!\n";
cout << "Total Moves: " << nk << endl;
system("pause");
system("cls");
main();
}

if(pass_maze[col][row - 1] == 4 || pass_maze[col][row + 1] == 4 || pass_maze[col - 1][row] == 4 || pass_maze[col + 1][row] == 4)
{
set_btII_flag = false;
set_backtrack_flag = true;
backtrack(pass_maze);
}

if(pass_maze[col][row - 1] == 0 || pass_maze[col][row + 1] == 0 || pass_maze[col - 1][row] == 0 || pass_maze[col + 1][row] == 0)
{
set_btII_flag = false;
set_backtrack_flag = false;
walk_maze(pass_maze);
}

if(pass_maze[col + 1][row] == 5 || pass_maze[col + 1][row] == 6)
{
set_btII_flag = true;
choose_direction(pass_maze, EAST);
}

else if(pass_maze[col][row + 1] == 5 || pass_maze[col][row + 1] == 6)
{
set_btII_flag = true;
choose_direction(pass_maze, SOUTH);
}

else if(pass_maze[col - 1][row] == 5 || pass_maze[col - 1][row] == 5)
{
set_btII_flag = true;
choose_direction(pass_maze, WEST);
}

else if(pass_maze[col][row - 1] == 5 || pass_maze[col][row - 1] == 6)
{
set_btII_flag = true;
choose_direction(pass_maze, NORTH);
}

cout << "\nThis maze is insovlable\n";
system("pause");
system("cls");
main();
}

//This function actually decides what direction to go in
//It may be obsolete, I think I may be able to go right from
//walking function to the actual moving but...Oh well
void choose_direction(int pass_maze[][MAP_WIDTH], int direction)
{


if(nk >= 600)
{
cout << "\nExceeded 600 move limit\n";
nk = 0;
system("pause");
system("cls");
main();
}

switch(direction)
{
case 1:

go.north(pass_maze);
break;

case 2:

go.south(pass_maze);
break;

case 3:

go.east(pass_maze);
break;

case 4:

go.west(pass_maze);
break;

default:

cout << "\n\nChoose Error\n\n";
system("pause");
exit(0);
break;
}
}




//Draw and Initilize the Maze//
void draw_maze(int pass_maze[][MAP_SQUARE])
{
system("cls");
char char_maze[MAP_HEIGHT][MAP_WIDTH];
int switch_char;
int k = 0;

for (int r = 0; r < MAP_SQUARE; r++)
{
for(int c = 0; c < MAP_SQUARE; c++)
{
switch_char = pass_maze[c][r];

switch(switch_char)
{

case 0:

char_maze[c][r] = space;
break;

case 1:

char_maze[c][r] = wall;
break;

case 2:

char_maze[c][r] = cheese;
break;

case 3:

char_maze[c][r] = mouse;
break;

case 4:

char_maze[c][r] = walk;
break;

case 5:

char_maze[c][r] = back;
break;

case 6:
char_maze[c][r] = backII;
break;

default:

char_maze[c][r] = 'H';
break;
}
}
}

for (int r = 0; r < MAP_SQUARE; ++r)
{
for(int c = 0; c < MAP_SQUARE; ++c)
{
k++;
cout << char_maze[c][r];

if ( k == MAP_SQUARE )
{
cout << "\n";
k = 0;
}
}
}

walk_maze(pass_maze);
}

//Creates a 30x30 maze, with walls and a cheese
//Also exports the created maze to "maze.txt"
//For later use
void random_maze()
{
fout.open("maze.txt");

int maze[MAP_HEIGHT][MAP_WIDTH];

for (int r = 0; r < MAP_SQUARE; ++r)
{
for(int c = 0; c < MAP_SQUARE; ++c)
{
maze[c][r] = (rand()% 3);
if(maze[c][r] == 2)
{
maze[c][r] = 0;
}

if(c == 0 || c == 29)
{
maze[c][r] = 1;
}

if(r == 0 || r == 29)
{
maze[c][r] = 1;
}

if(r == 1 && c == 1)
{
maze[c][r] = 3;
}

if(r == 28 && c == 28)
{
maze[c][r] = 2;
}

fout << maze[c][r];
fout << "\n";
}
}
fout.close();
draw_maze(maze);
}

//Loads a 30 x 30 maze from "maze.txt"
void load_maze()
{
ifstream fin;

int maze[MAP_HEIGHT][MAP_WIDTH];

fin.open("maze.txt");

if(fin.fail())
{
cout << "\nError\n";
}

for(int r = 0; r < MAP_SQUARE; ++r)
{
for(int c = 0; c < MAP_SQUARE; ++c)
{
fin >> maze[c][r];

}
}

fin.close();

draw_maze(maze);
return;
}


Move.h

#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <windows.h>

#define MAP_WIDTH 30
#define MAP_HEIGHT 30
#define MAP_SQUARE 30
#define NORTH 1
#define SOUTH 2
#define EAST 3
#define WEST 4

using namespace std;

int nk = 0;

fstream fin;
fstream fout;

const char mouse = 'M';
const char cheese = 'C';
const char space = ' ';
const char wall = '#';
const char walk = '+';
const char back = 'X';
const char backII = '!';

void draw_maze(int pass_maze[][MAP_WIDTH]), load_maze(), walk_maze(int pass_maze[][MAP_WIDTH]), backtrack(int pass_maze[][MAP_WIDTH]), backtrackII(int pass_maze[][MAP_WIDTH]), choose_direction(int pass_maze[][MAP_WIDTH], int direction);
void random_maze();

bool set_backtrack_flag = false;
bool set_btII_flag = false;


class GO
{
public:

//All of these actually move the "mouse" and then starts over at walk_maze();
void north(int pass_maze[][MAP_WIDTH]);
void south(int pass_maze[][MAP_WIDTH]);
void east(int pass_maze[][MAP_WIDTH]);
void west(int pass_maze[][MAP_WIDTH]);
}go;


void GO::north(int pass_maze[][MAP_WIDTH])
{

int col = 0;
int row = 0;

for(int r = 0; r < MAP_SQUARE; ++r)
{
for(int c = 0; c < MAP_SQUARE; ++c)
{
if(pass_maze[c][r] == 3)
{
col = c;
row = r;
}
}
}

++nk;
if(set_btII_flag == true)
{
pass_maze[col][row] = 6;
pass_maze[col][row - 1] = 3;
draw_maze(pass_maze);
}

else if(set_backtrack_flag == true)
{
pass_maze[col][row] = 5;
pass_maze[col][row - 1] = 3;
draw_maze(pass_maze);
}

else if(set_backtrack_flag == false && set_btII_flag == false)
{
pass_maze[col][row] = 4;
pass_maze[col][row - 1] = 3;
draw_maze(pass_maze);
}

else
{
cout <<"\nn\n";
system("pause");
}

}

void GO::south(int pass_maze[][MAP_WIDTH])
{

int col = 0;
int row = 0;

for(int r = 0; r < MAP_SQUARE; ++r)
{
for(int c = 0; c < MAP_SQUARE; ++c)
{
if(pass_maze[c][r] == 3)
{
col = c;
row = r;
}
}
}

++nk;
if(set_btII_flag == true)
{
pass_maze[col][row] = 6;
pass_maze[col][row + 1] = 3;
draw_maze(pass_maze);
}

else if(set_backtrack_flag == true)
{
pass_maze[col][row] = 5;
pass_maze[col][row + 1] = 3;
draw_maze(pass_maze);
}

else if(set_backtrack_flag == false && set_btII_flag == false)
{
pass_maze[col][row] = 4;
pass_maze[col][row + 1] = 3;
draw_maze(pass_maze);
}
else
{
cout <<"\ns\n";
system("pause");
}

}

void GO::east(int pass_maze[][MAP_WIDTH])
{

int col = 0;
int row = 0;

for(int r = 0; r < MAP_SQUARE; ++r)
{
for(int c = 0; c < MAP_SQUARE; ++c)
{
if(pass_maze[c][r] == 3)
{
col = c;
row = r;
}
}
}

++nk;
if(set_btII_flag == true)
{
pass_maze[col][row] = 6;
pass_maze[col + 1][row ] = 3;
draw_maze(pass_maze);
}

else if(set_backtrack_flag == true)
{
pass_maze[col][row] = 5;
pass_maze[col + 1][row ] = 3;
draw_maze(pass_maze);
}

else if(set_backtrack_flag == false && set_btII_flag == false)
{
pass_maze[col][row] = 4;
pass_maze[col + 1][row] = 3;
draw_maze(pass_maze);
}

else
{
cout <<"\ne\n";
system("pause");
}
}

void GO::west(int pass_maze[][MAP_WIDTH])
{

int col = 0;
int row = 0;

for(int r = 0; r < MAP_SQUARE; ++r)
{
for(int c = 0; c < MAP_SQUARE; ++c)
{
if(pass_maze[c][r] == 3)
{
col = c;
row = r;
}
}
}

++nk;
if(set_btII_flag == true)
{
pass_maze[col][row] = 6;
pass_maze[col - 1][row] = 3;
draw_maze(pass_maze);
}

else if(set_backtrack_flag == true)
{
pass_maze[col][row] = 5;
pass_maze[col - 1][row] = 3;
draw_maze(pass_maze);
}

else if(set_backtrack_flag == false && set_btII_flag == false)
{
pass_maze[col][row] = 4;
pass_maze[col - 1][row] = 3;
draw_maze(pass_maze);
}

else
{
cout <<"\nw\n";
system("pause");
}
}


Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this