• Status: Solved
• Priority: Medium
• Security: Public
• Views: 252

# URGENT - Input from user rather than from file

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?

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
aylinyzc
1 Solution

Commented:
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

Tackle projects and never again get stuck behind a technical roadblock.