?
Solved

URGENT - Input from user rather than from file

Posted on 2005-03-18
1
Medium Priority
?
248 Views
Last Modified: 2010-04-02
In my code, I want to change the style of the program;
I do not want to create the maze randomly, but from the input file like this for example;
(the user shoul type these columns and rows not receiving from file)

10 10
1 5
1 1 1 1 0 1 1 1 1 1
1 0 0 1 0 1 0 0 0 1
1 0 0 0 0 0 0 1 0 1
1 0 1 1 1 1 0 1 0 1
1 0 1 0 0 1 0 1 0 1
1 0 1 1 1 1 0 0 0 1
1 1 1 0 1 0 0 1 0 1
0 0 1 0 1 0 1 1 0 0
1 0 1 0 0 0 0 0 0 1
1 0 1 1 1 1 1 1 1 1
describes a maze of 10 rows by 10 columns whose start point is at coordinates (x=1,
y=5) .
The user should type the input in this order(above) and then print the output as below.
The code will first print the input data to the standard output as shown above
and will then produce the following output showing path from the entrance point
to an exit point in terms of pairs of numbers, giving coordinates of the points on
the path.
For the example above, the code should produce the following output after the input is printed
The solution to the puzzle is:
1 5
2 5
3 5
3 6
3 7
4 7
5 7
6 7
6 8
6 9
7 9
8 9
8 10

How can I do this in a simple way?
I am not good at in this subject, please help me!!!



My code is here;

#include <stack>
#include <conio.h>
#include <iostream>
#include <fstream>
using namespace std;
#include <string>
#include <time.h>


// Maze -- // -1 : border wall //  0 : empty // +1 : wall
void Init_Maze(int maze[][4], const int width, const int height)
 {
  int numcells = width * height;

  for(int cell=0; cell<numcells; cell++)
    for(int i=0; i<4; i++)
     maze[cell][i] = 1;

  // place border - top
  for(int column=0; column<width; column++)
   {
    maze[column][0] = -1;
    maze[numcells-column-1][2] = -1;
   }
  for(int row=0; row<height; row++)
   {
    maze[width*row][3] = -1;
    maze[(width*row)+(width-1)][1] = -1;
   }
}

// Print map of maze
void Cool_Draw_Maze(int maze[][4], const int width, const int height, std::ofstream &str)
 {
  for(int row=0; row<height; row++)
   {
    // Print top
    int r = (row*width);
    for(int col=0; col<width; col++)
     {
      str << "+";
      switch( maze[r+col][0] )
       {
        case -1 : str << "="; break;
        case 0  : str << " "; break;
        case  1 : str << "-"; break;
       }
      if( (col==(width-1)) ) str << "+";
     }
    str << std::endl;
    // Next line, Print middle
    for(col=0; col<width; col++)
     {
      switch( maze[r+col][3] )
       {
        case -1 : str << "!"; break;
        case 0  : str << " "; break;
        case  1 : str << "|"; break;
       }
      str << " ";
      if( col==(width-1) )
       {
        switch( maze[r+col][1] )
         {
          case -1 : str << "!"; break;
          case 0  : str << " "; break;
         }
        }
      }
     str << std::endl;
   }
    // Print bottom, Last row
    for(int cell=(width*(height-1)); cell<(width*height); cell++)
     {
      str << "+";
      switch( maze[cell][2] )
       {
        case -1 : str << "="; break;
        case 0  : str << " "; break;
       }
      if( cell==(width*height)-1 ) str << "+";
     }
    str << std::endl;
 }

int cell_at(int cell, int direction, int width)
 {
  switch(direction)
   {
    case 0: return cell-width;
    case 1: return cell+1;
    case 2: return cell+width;
    case 3: return cell-1;
   }
  return -1;
 }

void Print_Maze_Solution(int maze[][4], const int width, const int height, std::ofstream &str)
 {
  int numcells = width*height;
  str << width << " " << height << std::endl;
  // Start point : the top left !!!!!!!!!
  // End Point   : the bottom right. !!!!
  str << "1 " << (width*height) << std::endl;
  for(int cell=0; cell<numcells; cell++)
   for(int i=0; i<4; i++)
    if(!maze[cell][i])
     str << cell+1 << " " << cell_at(cell,i,width)+1 << std::endl;
 }


// Count the (breakable) walls around this cell
int Walls(int maze[][4], const int cell)
 {
  int w=0;
  for(int i=0; i<4; i++)
   if(maze[cell][i]==1) w++;
  return w;
 }

// Does this cell have all 4 walls?
int Sealed(int maze[][4], const int cell)
 {
  int w=0;
  for(int i=0; i<4; i++)
   if(maze[cell][i]) w++;
  return (w==4);
 }

// Return walled neighbours count
int Neighbours(int maze[][4], int neighbours[4], int cell, int width, int height)
 {
  int count=0;
  if( Sealed(maze, cell-width) && (cell>width) )
   neighbours[count++]=cell-width;

  if( Sealed(maze, cell+1) && ((cell%width)!=(width-1)) )
   neighbours[count++]=cell+1;

  if( Sealed(maze, cell+width) && (cell<(width*(height-1))) )
   neighbours[count++]=cell+width;
  if( Sealed(maze, cell-1) && ((cell%width)!=0) )
   neighbours[count++]=cell-1;

  return count;
 }


int Break_Between(int maze[][4], const int cell1, const int cell2, int width)
 {
  if( cell2 == cell1-width )
   { // North from cell1
    maze[cell1][0] = 0;
    maze[cell2][2] = 0;
   }
  if( cell2 == cell1+width )
   { // South from cell1
    maze[cell1][2] = 0;
    maze[cell2][0] = 0;
   }
  if(cell2 == cell1+1)
   { // East from cell1
    maze[cell1][1] = 0;
    maze[cell2][3] = 0;
   }
  if(cell2 == cell1-1)
   { // West from cell1
    maze[cell1][3] = 0;
    maze[cell2][1] = 0;
   }
  return cell2;
}

void Create_Maze(int maze[][4], const int width, const int height)
 {
  int remaining = (width*height)-1;
  int currentcell = rand()%remaining;
  int nbrs[4];
  std::stack<int> s;
  while(remaining > 0)
   {
    int count = Neighbours(maze,nbrs,currentcell,width,height);
    if( count>0 )
     {
      s.push(currentcell);
      currentcell = Break_Between(maze,currentcell,nbrs[ rand() % count ],width );
      remaining--;
     }
    else
     {
      currentcell = s.top();
      s.pop();
     }
   }
}

int Border(int maze[][4], int cell)
 {
  int w = 0;
  for(int i=0; i<4; i++)
   if( maze[cell][i]==-1 )
    w++;
  return w;
}

int main(int argc, const char **argv)
 {
  std::stack<int> s;
  std::string filename;
  int maze[20*20][4];
  // Seed the random number generator
 
  srand(time(NULL));

  // Select maze size
  int width, height;
  if(argc >=3)
   {
    width  = atoi(argv[1]);
    height = atoi(argv[2]);
   }
  else
   {
    std::cout << "Enter Maze Width & Height. !!!!\n";
    std::cout << "Enter Width : ";
    std::cin  >> width;
    std::cout << "Enter Height : ";
    std::cin  >> height;
   }

  // Create a random maze
  Init_Maze(maze, width, height);
  Create_Maze(maze, width, height);

  // Select base filename
  if( argc >= 4 ) filename = std::string(argv[3]);
  else
   {
    std::cout << "Enter base filename: ";
    std::cin  >> filename;
   }
  // Display output
  std::ofstream drawMaze( (filename+".maz").c_str() );
  Cool_Draw_Maze(maze, width, height, drawMaze);

  std::ofstream pathFollowerFile( (filename+".sol").c_str() );
  Print_Maze_Solution(maze, width, height, pathFollowerFile);

  cout<<"Maze and solution files are created...!!!\n";
  getch();
  return 0;
}

0
Comment
Question by:aylinyzc
[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
1 Comment
 
LVL 22

Accepted Solution

by:
grg99 earned 2000 total points
ID: 13582029
It's not too hard to do the basic inputting.  Go see "man scanf" for details, you're going to be writing statements much like:

scanf("%d %d", &XDim, &YDim );
and
scanf("%d", &OneOrZero);

.. the second one surrounded by a for() loop or two.

We can't give you much more than that without giving away the whole show.

------

Secondarily, it's unrealistic to expect a human to type all that without making a mistake.

Yu should have some option (eventually) for asking if they'd lkike to make a correction to the last line input.

0

Featured Post

Industry Leaders: 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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

765 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