Solved

Maze() {

Posted on 2004-10-24
331 Views
Last Modified: 2010-04-01
// 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();
}





0
Question by:edelossantos
    6 Comments
     
    LVL 3

    Assisted Solution

    by:HendrikTYR
    Hi edelossantos,

    change:

    >> walk(11,1);

    to:

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

    in your main() function :)
    0
     
    LVL 3

    Accepted Solution

    by:
    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
     
    LVL 3

    Assisted Solution

    by:HendrikTYR
    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
     
    LVL 39

    Assisted Solution

    by:itsmeandnobodyelse
    >>>> // 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
     

    Author Comment

    by:edelossantos
    HendrikTYR

    [edeloss2@pegasus part2]$ cxx maze.cpp
    [edeloss2@pegasus part2]$ a.out
    File could not be opened
    0
     
    LVL 39

    Assisted Solution

    by:itsmeandnobodyelse
    >>>> 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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now