Maze() {

// My maze is misbehaving.....why?

# Makefile:
#        It uses the C++ Compiler with all warnings and
#        full debugging; will create a single executable called 'maze'
# ---------------------------------------------------------------
# the compiler
CPP = cxx
# compiler flags
CFLAGS = -L/usr/lib/cmplrs/cxx -DPOSIX_4D9 -w0 -gall
# linker flags to link in the libraries libm.a and libtask.a
LFLAGS = -lm -ltask
#
RM = rm -f
# ----------------------------------------------------------------
# Explanation of macros:
#     $< is any dependent file which is out of file1
#     $* is the target's basename (without extension)
#     $@ is the target's fullname
#
# add suffix .cpp since it is not a default with make util
.SUFFIXES:      .cpp .o
#
# implicit rule for compilation only:
.cpp.o:
      ${CPP} -c ${CFLAGS} $<

OFILES=            maze.o util.o

HFILES=            util.h

# dependencies
#
default:      maze      
#
maze:           $(OFILES)
            ${CPP} ${CFLAGS} ${LFLAGS} $(OFILES) -o $@

maze.o:            maze.cpp util.h

util.o:                      util.cpp util.h

#
clean:
      ${RM} *.o
      ${RM} core
#
veryclean:      clean
      ${RM}  maze  

// maze.txt
|--------------------|
|   |-------| |      |
| |           |    | |
| |------------    | |
|             |    | |
|------| |--- |    | |
|      | |  | |----| |
|      | |           |
|------| |---------| |
|        |         | |
|------- ---- -----| |
|             |    |X|
|--------------------|

// util.h

#ifndef UTIL_H  
#define UTIL_H

#define NULL 0L

#define STDSCREEN 80        
#define DOWN 0                        
#define UP   1
#define END   0
#define START 1
#define FAIL    0
#define SUCCESS 1
#define MISS -1
#define HIT   1

enum Error_code{success,fail,range_error,underflow,overflow,fatal,
                not_present,duplicate_error,entry_inserted,entry_found,
                internal_error};

void clearScreen();                  
void clearTop();                      
void clearBelowTop();                
void goTop();                            
void topLine(const char *text = " ");    
void bottomLine(char *text = " ");  
void hitAnyKey();                  
void flushInput();                    
void Warning(char *);                
void Error(char *);                    
bool promptYesNo(char *prompt="");  
void EatWhiteSpace(void);            

#endif

// filename: util.cpp

#include <iostream>
#include <ctype.h>
#include <stdlib.h>
#include "util.h"
 
void clearScreen (void) {
 
  cout << "\033[2J";          
  cout << "\033[;f";          

}

void clearTop()
// Goto Top-Left corner; Clear to end of line
  {cout << "\033[0;0f" << "\033[0K";}

void clearBelowTop()
// Clear everything except top row
  {cout << "\033[1;0f" << "\033[1B" << "\033[J";}

void goTop ()
// to to the Top-Left corner;
  {cout << "\033[0;0f";}

void topLine(const char str[])
// Goto top-left corner; clear to end of line; print str
  {cout << "\033[0;0f" << "\033[0K" << str;}

void bottomLine (char * str)
  // goto line 23 ; clear line 23; write text
  {cout << "\033[23;0Hf" << "\033[2K" << str;}

/* ----------------  I/O FUNCTIONS */

/* Precondition: the input buffer is empty */
void hitAnyKey ()
{
   char ch;
   bottomLine ("Hit Enter to continue...");
   cin.get(ch);
   clearScreen();
}

void flushInput ()
// flush input buffer
{
   char buff[81];
   if (cin)
      cin.getline(buff,80);   // flush the input buffer of 80 characters
}

void Warning(char *message)
{
   cerr << message;
}

void Error(char *message)
{
   cerr << message;
   exit(1);
}

void EatWhiteSpace()
/* discard leading white space */
{
    char ch;
    do {
      cin.get(ch);
    }
    while(isspace(ch));

    if(ch != EOF)
      cin.putback(ch);
}

bool promptYesNo(char * c)
{
   char ch;
   cout << c << " [y|n]?";
   do {
      cin.get(ch);  
      tolower(ch);
      } while (ch != 'y' && ch != 'n');
   return (ch == 'y');
}

// maze.cpp

#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include "util.h"

const int maxcol=22;
const int maxrow=13;
int finish = 0;
int calls = 0;
void walk(int row, int col);

char maze[13][22];

void print();
void readMaze();

int main() {
   readMaze();
   walk(11,1);  
}

void walk(int row, int col) {
 
  calls++;
  if (maze[row][col] == 'X'){
     finish = 1;
     print();
 
  }
  else {
     
     if(maze[row][col] == ' ') {
       
        maze[row][col] = '*';    
        print();                
     
     }
     if (!finish && maze[row][col-1] == ' ') walk(row,col-1);
     if (!finish && maze[row][col+1] == ' ') walk(row,col+1);
     if (!finish && maze[row-1][col] == ' ') walk(row-1,col);
     if (!finish && maze[row+1][col] == ' ') walk(row+1,col);
  }
}

void readMaze() {
 
   int row,col;
   char ch;
   char filename[30] = "maze.txt";
 
   ifstream infile( filename, ios::in );
   if( !infile ) {
      cerr << "File could not be opened\n";
      exit( 1 );
    }
    for (row = 0; row < maxrow; row++) {
      for (col = 0; col < maxcol; col++){
         infile.get(ch);
         maze[row][col] = ch;
      }
      infile.get(ch);
    }
  infile.close();
}

void print()
{
   clearScreen();
   int row, col;
   cout << "finish: " << finish << " calls: " << calls << endl;
   for (row = 0; row < maxrow; row++) {
      for (col = 0; col < maxcol; col++)
          cout << maze[row][col];
      cout << endl;
   }
   cout << endl;
   hitAnyKey();
}





edelossantosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HendrikTYRCommented:
Hi edelossantos,

change:

>> walk(11,1);

to:

while(!finish) walk(11,1);

in your main() function :)
0
HendrikTYRCommented:
Follow up:

Sorry, Enrique... I missed the point a bit... only saw now that you DO have a recursive function

The only problems I picked up was inside your "util" stuff

I tested this in VC++ and it now works fine for me: (util.ccp and util.h removed)
I used system("cls") to clear the screen.  If you are going to run this on a platform not supporting "cls", you will have to use a different  way to clear the screen.  Does your compiler have access to clrscrn();?

/* main.cpp : your only file*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
//#include "util.h"

using namespace std;

const int maxcol=22;
const int maxrow=13;
int finish = 0;
int calls = 0;
void walk(int row, int col);

char maze[13][22];

void print();
void readMaze();

int main() {
   char ch;
   readMaze();
   walk(11,1);  

   cin.get(ch);
}

void walk(int row, int col) {
  calls++;
  if (maze[row][col] == 'X'){
     finish = 1;
     print();
 
  }
  else {
     
      if(maze[row][col] == ' ') {
       
        maze[row][col] = '*';    
        print();                
     
     }
     if (!finish && maze[row][col-1] == ' ') walk(row,col-1);
     if (!finish && maze[row][col+1] == ' ') walk(row,col+1);
     if (!finish && maze[row-1][col] == ' ') walk(row-1,col);
     if (!finish && maze[row+1][col] == ' ') walk(row+1,col);
  }
}

void readMaze() {
 
   int row,col;
   char ch;
   char filename[30] = "maze.txt";
 
   ifstream infile( filename, ios::in );
   if( !infile ) {
      cerr << "File could not be opened\n";
      exit( 1 );
    }
    for (row = 0; row < maxrow; row++) {
      for (col = 0; col < maxcol; col++){
         infile.get(ch);
         maze[row][col] = ch;
      }
      infile.get(ch);
    }
  infile.close();
}

void print()
{
   char ch;
   int row, col;
   system("cls");
   cout << "finish: " << finish << " calls: " << calls << endl;
   for (row = 0; row < maxrow; row++) {
      for (col = 0; col < maxcol; col++)
          cout << maze[row][col];
      cout << endl;
   }
   cout << endl;
   cout << "Hit Enter to continue...";
   cin.get(ch);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HendrikTYRCommented:
If your OS console is ANSI complient:

then you may use:
printf( "\33[2J");

instead of system("cls");

In Windows/DOS you have to load ansi.sys in your config.sys file for this.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

itsmeandnobodyelseCommented:
>>>> // My maze is misbehaving.....why?

What exactly is your problem?

I did the same as Hendryk, removed all escape sequences and used system("cls"); to clear screen, and all worked fine.

Regards, Alex
0
edelossantosAuthor Commented:
HendrikTYR

[edeloss2@pegasus part2]$ cxx maze.cpp
[edeloss2@pegasus part2]$ a.out
File could not be opened
0
itsmeandnobodyelseCommented:
>>>> File could not be opened

Put the file maze.txt to the directory where your executable is (normally the project directory) *or* to the directory where you are invoking the maze executable from.

Regards, Alex

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.