[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Maze() {

Posted on 2004-10-24
6
Medium Priority
?
376 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
Comment
Question by:edelossantos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Assisted Solution

by:HendrikTYR
HendrikTYR earned 1400 total points
ID: 12397960
Hi edelossantos,

change:

>> walk(11,1);

to:

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

in your main() function :)
0
 
LVL 3

Accepted Solution

by:
HendrikTYR earned 1400 total points
ID: 12398190
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
HendrikTYR earned 1400 total points
ID: 12398280
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 600 total points
ID: 12398574
>>>> // 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
ID: 12401280
HendrikTYR

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

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 600 total points
ID: 12401455
>>>> 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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

656 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