Sign in to follow this  

Maze Running Program

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have written a program that will walk through a 30 x 30 maze and find the "Cheese" but it only has about a 50% sucess rate. I am trying to re write my code to raise this %. When I re wrote my code I tried making the whole fucntion different, but now it doesnt work. Please help! note large amounts of code
#include "Header.h"

#define MAP_WIDTH 30
#define MAP_HEIGHT 30
#define MAP_SQUARE 30

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]);
void random_maze();

const char mouse = 'M';
const char cheese = 'C';
const char space = ' ';
const char wall = '#';



int main()
{
    srand(GetTickCount());
    int choice;
    cout << "(1)Random\n";
    cout << "(2)Test\n";
    cin >> choice;
    
    if(choice == 2)
    {
    load_maze();
    }
    
    else
    {
        random_maze();
    }
    
    system("pause");
    return 0;
}

void walk_maze(int pass_maze[][MAP_WIDTH])
{
    cout << "\nwalk maze\n";
    go.set_flags_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;
                }
        }
    }
    while(1)
    {
    ++nk;
        
        if(pass_maze[col][row - 1] == 0)
        {
                //pass_maze[col][row] = 4;
                set_south_flag = true;
                cout << "\nSouth set\n";
                choose_direction(pass_maze);
                
        }
        
        if(pass_maze[col][row - 1] == 0 && pass_maze[col + 1][row] == 0)
        {
                //pass_maze[col][row] = 4;
                set_se_flag = true;
                choose_direction(pass_maze);
                 
        }
         
        if(pass_maze[col][row + 1] == 0)
        {
                //pass_maze[col][row] = 4;
                set_north_flag = true;
                choose_direction(pass_maze);
                
        }
        
        if(pass_maze[col][row + 1] == 0 && pass_maze[col + 1][row] == 0)
        {
                //pass_maze[col][row] = 4;
                set_ne_flag = true;
                choose_direction(pass_maze);
                
        }
        
        if(pass_maze[col - 1][row] == 0)
        {
                //pass_maze[col][row] = 4;
                set_west_flag = true;
                choose_direction(pass_maze);
                
        }
        
        if(pass_maze[col - 1][row] == 0 && pass_maze[col][row - 1] == 0)
        {
                //pass_maze[col][row] = 4;
                set_sw_flag = true;
                choose_direction(pass_maze);
                
        }
        
        if(pass_maze[col + 1][row] == 0)
        {
                //pass_maze[col][row] = 4;
                set_east_flag = true;
                choose_direction(pass_maze);
                
        }
        
        if(pass_maze[col + 1][row] == 0 && pass_maze[col][row + 1] == 0)
        {
                //pass_maze[col][row] = 4;
                set_nw_flag = true;
                choose_direction(pass_maze);
                
        }
        
        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 + bk + bkII << endl;
                system("pause");
                exit(0);
        }
        
        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)
        {
        
                cout << "\nHello?\n";
                scc = col;
                scr = row;
                backtrack(pass_maze);
        }
    }
}

void backtrack(int pass_maze[][MAP_WIDTH])
{
    cout << "\nBacktrack\n";
    
    set_backtrack_flag = true;
    go.set_flags_false();
    
    int col = 0;
    int row = 0;
    cout << "\nStep";
    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;
                }
        }
    }
    cout << "\nStep";
    while(1)
    {
    
    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;
            go.set_flags_false();
            draw_maze(pass_maze);
    }    
    ++nk;
        
        if(pass_maze[col][row - 1] == 4)
        {
                set_south_flag = true;
                cout << "\nSet Flag 1 \n";
                
                if(pass_maze[col][row - 1] == 0)
                {
                    set_backtrack_flag = false;
                    choose_direction(pass_maze);
                } 
                
                   else   
                choose_direction(pass_maze);
                
        }
        
        else if(pass_maze[col][row - 1] == 4 && pass_maze[col + 1][row] == 4)
        {
                
                set_se_flag = true;
                cout << "\nSet Flag 2 \n";
                set_backtrack_flag = true;
                
                if(pass_maze[col][row - 1] == 0 && pass_maze[col + 1][row] == 0)
                {
                    set_backtrack_flag = false;
                    choose_direction(pass_maze);
                }
                else 
                choose_direction(pass_maze);
                
        }
         
        else if(pass_maze[col][row + 1] == 4)
        {
                
                set_north_flag = true;
                set_backtrack_flag = true;
                cout << "\nSet Flag 3 \n";
                
                if(pass_maze[col][row + 1] == 0)
                {
                    set_backtrack_flag = false;
                    choose_direction(pass_maze);
                }
                else
                choose_direction(pass_maze);
                
        }
        
        else if(pass_maze[col][row + 1] == 4 && pass_maze[col + 1][row] == 4)
        {
                
                set_ne_flag = true;
                set_backtrack_flag = true;
                cout << "\nSet Flag 4  \n";
                
                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)
                {
                    set_backtrack_flag = false;
                    walk_maze(pass_maze);
                }
                else
                choose_direction(pass_maze);
                
        }
        
        else if(pass_maze[col - 1][row] == 4)
        {
                
                set_west_flag = true;
                set_backtrack_flag = true;
                cout << "\nSet Flag 5 \n";
                
                if(pass_maze[col - 1][row] == 0)
                {
                    cout << "\nError\n";
                    set_backtrack_flag = false;
                    choose_direction(pass_maze);
                }
                //else
                cout << "\nChoose West\n";
                choose_direction(pass_maze);
                
        }
        
        else if(pass_maze[col - 1][row] == 4 && pass_maze[col][row - 1] == 4)
        {
                
                set_sw_flag = true;
                set_backtrack_flag = true;
                cout << "\nSet Flag 6 \n";
                
                
                if(pass_maze[col - 1][row] == 0 && pass_maze[col][row - 1] == 0)
                {
                    set_backtrack_flag = false;
                    choose_direction(pass_maze);
                }
        
                choose_direction(pass_maze);
                
        }
        
        else if(pass_maze[col + 1][row] == 4)
        {
                
                set_east_flag = true;
                set_backtrack_flag = true;
                cout << "\nSet Flag 7 \n";
                
                if(pass_maze[col + 1][row] == 0)
                {
                    set_backtrack_flag = false;
                    choose_direction(pass_maze);
                }
                else
                choose_direction(pass_maze);
                
        }
        
        else if(pass_maze[col + 1][row] == 4 && pass_maze[col][row + 1] == 4)
        {
                
                set_nw_flag = true;
                set_backtrack_flag = true;
                cout << "\nSet Flag 8 \n";
                
                if(pass_maze[col + 1][row] == 0 && pass_maze[col][row + 1] == 0)
                {
                    set_backtrack_flag = false;
                    choose_direction(pass_maze);
                }
                else
                choose_direction(pass_maze);
                
        }
        
        else 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 + bk + bkII << endl;
                system("pause");
                exit(0);
        }
        
        else
        {
            walk_maze(pass_maze);
    }    
   }    
    //walk_maze(pass_maze);
}

void choose_direction(int pass_maze[][MAP_WIDTH])
{
        cout << "\nChoose\n";
        if(set_south_flag == true)
        {  
        
                if(set_se_flag == true)
                {               
                    cout << "\nSE\n";
                    go.SE(pass_maze); 
                }
                 
                if(set_sw_flag == true)
                {               
                    cout << "\nSw\n";
                    go.SW(pass_maze);
                }         
                
                else
                {
                cout << "\nS\n";
                go.south(pass_maze);
                }      
                     
        } 
        
        if(set_west_flag == true)
        {
                cout << "\nw\n";
                go.west(pass_maze);       
        }   
        
        else if(set_east_flag == true)
        {
                cout << "\nE\n";
                go.east(pass_maze);       
        } 
        
        else if(set_north_flag == true)
        {
                if(set_ne_flag == true)
                {               
                    cout << "\nNE\n";
                    go.NE(pass_maze);
                }
                
                if(set_nw_flag == true)
                {               
                    cout << "\nNw\n";
                    go.NW(pass_maze);
                }         
                
                else
                {
                cout << "\nN\n";
                go.north(pass_maze);
                }      
                     
        }   
              
}               


//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] = '+';
                                break;
                                
                                case 5:
                                
                                char_maze[c][r] = 'X';
                                break;
                                
                                case 6:
                                char_maze[c][r] = '!';
                                break;
                                
                                default:
                                
                                char_maze[c][r] = '!';
                                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);
}

void random_maze()
{
    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;
                }
        }
    }
    draw_maze(maze);
}

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];
                
            }
    }
    
    draw_maze(maze);
    return;
}


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

#define MAP_WIDTH 30
#define MAP_HEIGHT 30
#define MAP_SQUARE 30

using namespace std;

int nk = 0, bk = 0, bkII = 0;
int scr, scc;

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();
void random_maze();

bool set_north_flag = false;
bool set_south_flag = false;
bool set_east_flag = false;
bool set_west_flag = false;
bool set_ne_flag = false;
bool set_se_flag = false;
bool set_nw_flag = false;
bool set_sw_flag = false;
bool set_backtrack_flag = false;


class GO
{
    public:
        
        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]);
        void SE(int pass_maze[][MAP_WIDTH]);
        void SW(int pass_maze[][MAP_WIDTH]);
        void NE(int pass_maze[][MAP_WIDTH]);
        void NW(int pass_maze[][MAP_WIDTH]);
        void set_flags_false();
        
}go;


void GO::north(int pass_maze[][MAP_WIDTH])
    {
        //go.set_flags_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;
                }
        }
    }
    
    ++nk;
                if(set_backtrack_flag == true)
                {  
                pass_maze[col][row] = 5;
                pass_maze[col][row + 1] = 3;
                draw_maze(pass_maze);
                }  
                
                else
                {                                
                pass_maze[col][row] = 4;
                pass_maze[col][row + 1] = 3;
                draw_maze(pass_maze);
                }    
                
}

void GO::south(int pass_maze[][MAP_WIDTH])
    {
        //go.set_flags_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;
                }
        }
    }
    
    ++nk;
                if(set_backtrack_flag == true)
                {  
                pass_maze[col][row] = 5;
                pass_maze[col][row - 1] = 3;
                draw_maze(pass_maze);
                }  
                
                else
                {                                
                pass_maze[col][row] = 4;
                pass_maze[col][row - 1] = 3;
                draw_maze(pass_maze);
                }
                
}

void GO::SE(int pass_maze[][MAP_WIDTH])
    {
        //go.set_flags_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;
                }
        }
    }
    
    ++nk;
                if(set_backtrack_flag == true)
                {  
                pass_maze[col][row] = 5;
                pass_maze[col + 1][row - 1] = 3;
                draw_maze(pass_maze);
                }  
                
                else
                {                                
                pass_maze[col][row] = 4;
                pass_maze[col + 1][row - 1] = 3;
                draw_maze(pass_maze);
                }
}

void GO::SW(int pass_maze[][MAP_WIDTH])
    {
        //go.set_flags_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;
                }
        }
    }
    
    ++nk;
                if(set_backtrack_flag == true)
                {  
                pass_maze[col][row] = 5;
                pass_maze[col - 1][row - 1] = 3;
                draw_maze(pass_maze);
                }  
                
                else
                {                                
                pass_maze[col][row] = 4;
                pass_maze[col - 1][row] = 3;
                draw_maze(pass_maze);
                }
}
void GO::east(int pass_maze[][MAP_WIDTH])
    {
        //go.set_flags_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;
                }
        }
    }
    
    ++nk;
                if(set_backtrack_flag == true)
                {  
                pass_maze[col][row] = 5;
                pass_maze[col + 1][row ] = 3;
                draw_maze(pass_maze);
                }  
                
                else
                {                                
                pass_maze[col][row] = 4;
                pass_maze[col + 1][row] = 3;
                draw_maze(pass_maze);
                }
}

void GO::west(int pass_maze[][MAP_WIDTH])
    {
        //go.set_flags_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;
                }
        }
    }
    
    ++nk;
                if(set_backtrack_flag == true)
                {  
                pass_maze[col][row] = 5;
                pass_maze[col - 1][row] = 3;
                draw_maze(pass_maze);
                }  
                
                else
                {                                
                pass_maze[col][row] = 4;
                pass_maze[col - 1][row] = 3;
                draw_maze(pass_maze);
                }
}

void GO::NE(int pass_maze[][MAP_WIDTH])
    {
        //go.set_flags_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;
                }
        }
    }
    
    ++nk;
                if(set_backtrack_flag == true)
                {  
                pass_maze[col][row] = 5;
                pass_maze[col + 1][row + 1] = 3;
                draw_maze(pass_maze);
                }  
                
                else
                {                                
                pass_maze[col][row] = 4;
                pass_maze[col + 1][row + 1] = 3;
                draw_maze(pass_maze);
                }
}

void GO::NW(int pass_maze[][MAP_WIDTH])
    {
        //go.set_flags_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;
                }
        }
    }
    
    ++nk;
                if(set_backtrack_flag == true)
                {  
                pass_maze[col][row] = 5;
                pass_maze[col - 1][row + 1] = 3;
                draw_maze(pass_maze);
                }  
                
                else
                {                                
                pass_maze[col][row] = 4;
                pass_maze[col - 1][row + 1] = 3;
                draw_maze(pass_maze);
                }
}

void GO::set_flags_false()
{
        cout << "set flags false\n";
        system("pause");
        bool set_north_flag = false;
        bool set_south_flag = false;
        bool set_east_flag = false;
        bool set_west_flag = false;
        bool set_ne_flag = false;
        bool set_se_flag = false;
        bool set_nw_flag = false;
        bool set_sw_flag = false;
}


If you run it you will see the problem. It's kinda hard to explain because I don't really know why its not working! [Edited by - Betrayer_of_Code on September 15, 2004 8:10:05 PM]

Share this post


Link to post
Share on other sites
Hey man this is a cool idea!

I've got the code running, but with this much code its quite hard to understand without some comments or an explanation of what you are trying to do with some of the functions?

I mean i've sorted so it runs but it is not fixed:

here is the code


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


#define MAP_WIDTH 30
#define MAP_HEIGHT 30
#define MAP_SQUARE 30

using namespace std;

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

void choose_direct();
int nk = 0, bk = 0, bkII = 0;
int scr, scc;

bool set_north_flag;
bool set_south_flag;
bool set_east_flag;
bool set_west_flag;

const char mouse = 'M';
const char cheese = 'C';
const char space = ' ';
const char wall = '#';



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

if(choice == 2)
{
load_maze();
}

else
{
random_maze();
}

system("pause");
return 0;
}

void walk_maze(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;
}
}
}
while(1)
{
++nk;

if(pass_maze[col][row - 1] == 0)
{
pass_maze[col][row] = 4;
set_south_flag = true;
choose_direction();

}

if(pass_maze[col][row + 1] == 0)
{
pass_maze[col][row] = 4;
set_north_flag = true;
choose_direction();

}

if(pass_maze[col - 1][row] == 0)
{
pass_maze[col][row] = 4;
set_west_flag = true;
choose_direction();

}

if(pass_maze[col + 1][row] == 0)
{
pass_maze[col][row] = 4;
set_east_flag = true;
choose_direction();

}

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 + bk + bkII << endl;
system("pause");
exit(0);
}

/*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)
{

scc = col;
scr = row;
backtrack(pass_maze);
}*/

}

}

void choose_direction()
{
//if(set_south_flag == true)
//{
//if(set_east_flag == true)
//{


/*void backtrack(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;
}
}
}
while(1)
{
++bk;

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)
{

system("pause");
walk_maze(pass_maze);
}
else if(pass_maze[col][row + 1] == 4)
{
pass_maze[col][row] = 5;
pass_maze[col][row + 1] = 3;
draw_maze(pass_maze);

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

}

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

}

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

}

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 + bk + bkII << endl;
system("pause");
exit(0);
}
if((pass_maze[col + 1][row] == 6 || pass_maze[col + 1][row] == 1) && (pass_maze[col - 1][row] == 6 || pass_maze[col - 1][row] == 1) && (pass_maze[col][row + 1] == 6 ||pass_maze[col][row + 1] == 1) && (pass_maze[col][row - 1] == 6 || pass_maze[col][row - 1] == 1))
{
backtrackII(pass_maze);
}
else
{
backtrackII(pass_maze);
}
}
}


void backtrackII(int pass_maze[][MAP_WIDTH])
{
int col = 0;
int row = 0;

if(nk + bk + bkII > 1200)
{
cout << "Exceeded 1200 move limit\n";
system("pause");
exit(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;
}
}
}

scc = col;
scr = row;

while(1)
{
++bk;
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)
{
system("pause");
walk_maze(pass_maze);
}

else if(pass_maze[col][row + 1] == 5 || pass_maze[col][row - 1] == 5 || pass_maze[col + 1][row] == 5 || pass_maze[col - 1][row] == 5)
{
pass_maze[col][row] = 6;

srand(GetTickCount());

int select_random = rand() % 3;

if(select_random == 0)
{
pass_maze[col][row + 1] = 3;
}

else if(select_random == 1)
{
pass_maze[col][row - 1] = 3;
}

else if(select_random == 2)
{
pass_maze[col + 1][row] = 3;
}

else if(select_random == 3)
{
pass_maze[col - 1][row] = 3;
}


draw_maze(pass_maze);

}

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 + bk + bkII << endl;
system("pause");
exit(0);
}

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


cout<<"This Maze cannot be solved!!\n";
cout << nk + bk + bkII << " Moves\n";
system("pause");
exit(0);

}


}
}*/

}
//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] = '+';
break;

case 5:

char_maze[c][r] = 'X';
break;

case 6:
char_maze[c][r] = '!';
break;

default:

char_maze[c][r] = '!';
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);
}

void random_maze()
{
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;
}
}
}
draw_maze(maze);
}

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];

}
}

draw_maze(maze);

}



Share this post


Link to post
Share on other sites
Alright, I did infact backup all my code. I'm just trying it in a different way to make it better. And im about to post the real code, because this code I posted doesnt have the problem even in it :D. OOPS!

EDIT::The "finished" code that doesn't work was edited into the first post.

Onehundred: If you want a working copy, I can send you one, its older, and only about 50 - 70% sucsessful, but it does work.

Share this post


Link to post
Share on other sites

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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