Question

​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file...

​​​​​​This program will make Maze game. Please Help in c++

  • Prompt the user for a file that contains the maze. Read it into a two-dimensional array
    • Remember you can use inputStream.get(c) to read the next character from the inputStream. This will read whitespace and non-whitespace characters
    • Don’t forget to read the newline character at the end of each line
  • Print the maze to the screen from your array
    • You should include a ‘*’ in your maze to indicate where the user is located. The initial position is the second row, first column
  • Prompt the user for a single move
  • Print the current location as (x,y) and the location after the move is done as (x,y)
    • For this week, just do the arithmetic, do not worry if the move is bad
    • Note (0, 0) is the upper left corner. X increases to the right and Y increases down
  • Use functions as appropriate – you should have more than just main()
  • Let small_maze.txt contain the following lines (4 rows, 8 columns):
||||||||
  ||||||
|       
||||||||

Sample Run #1 (bold, underlined text is what the user types):

File? small_maze.txt

||||||||
* ||||||
|       
||||||||
Move (udrlq)? r

(0,1) to (1,1)

Sample Run #2 (bold, underlined text is what the user types):

File? small_maze.txt

||||||||
* ||||||
|       
||||||||
Move (udrlq)? u

(0,1) to (0,0)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include<iostream>
#include<fstream>

using namespace std;

//function to read from file
void readFile(char maze[][10], int &m, int &n)
{
char ch, filename[20];
int i = 0,j = 0;
ifstream in;

cout<<"File? ";
cin>>filename;
in.open(filename);

while(in)
{
ch = in.get();
if(ch=='\n')
{
n = i;
i = 0;
j++;
}
else
{
maze[j][i++] = ch;
}
}
m = j;
}

//function to print the maze
void print(char maze[][10], int m, int n)
{
cout<<endl;
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
cout<<maze[i][j];
}
cout<<endl;
}
cout<<endl;
}

//main function
int main()
{
char ch;
int i, j, t, m, n;
  
char maze[10][10] = {' '};

//read the file
readFile(maze, m, n);

//initial position is the second row, first column
maze[i=1][j=0] = '*';
  
print(maze, m, n);

while(1)
{
  
   cout<<"Move (udrlq)?";
   cin>>ch;
  
switch(ch)
{
case 'u':
if(i==0)
cout<<"Invalid move!"<<endl;
else
{
   t = i;
   i--;
cout<<"("<<t<<","<<j<<") to ("<<i<<","<<j<<")"<<endl;
}
break;
case 'd':
if(i==m-1)
cout<<"Invalid move!"<<endl;
else
{
   t = i;
   i++;
cout<<"("<<t<<","<<j<<") to ("<<i<<","<<j<<")"<<endl;
               }
               break;
case 'r':
if(j==n-1)
cout<<"Invalid move!"<<endl;
else
{
   t = j;
   j++;
cout<<"("<<i<<","<<t<<") to ("<<i<<","<<j<<")"<<endl;
               }
               break;

case 'l':
if(j==0)
cout<<"Invalid move!"<<endl;
else
{
   t = j;
   j--;
cout<<"("<<i<<","<<t<<") to ("<<i<<","<<j<<")"<<endl;
               }
case 'q':
exit(0);
}
}
return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help and follow instructions, c++ , let me know if there is any questions Description:...

    Please help and follow instructions, c++ , let me know if there is any questions Description: This program is part 1 of a larger program. Eventually, it will be a complete Flashcard game. For this part, the program will Prompt the user for a file that contains the questions – use getline(cin, fname) instead of cin >> fname to read the file name from the user. This will keep you in sync with the user’s input for later in the...

  • Help please, write code in c++. The assignment is about making a hangman game. Instructions: This...

    Help please, write code in c++. The assignment is about making a hangman game. Instructions: This program is part 1 of a larger program. Eventually, it will be a complete Hangman game. For this part, the program will Prompt the user for a game number, Read a specific word from a file, Loop through and display each stage of the hangman character I recommend using a counter while loop and letting the counter be the number of wrong guesses. This...

  • Help c++ Description: This program is part 1 of a larger program. Eventually, it will be...

    Help c++ Description: This program is part 1 of a larger program. Eventually, it will be a complete Hangman game. For this part, the program will Prompt the user for a game number, Read a specific word from a file, Loop through and display each stage of the hangman character I recommend using a counter while loop and letting the counter be the number of wrong guesses. This will help you prepare for next week Print the final messages of...

  • Please Help, I will rate either way because of the effort. Description: help in c++, this...

    Please Help, I will rate either way because of the effort. Description: help in c++, this program should have 3 files GuessWho.cpp, Person.cpp, Person.h This program will be a Guess Who game. The program should Read the first line of people.txt, Use the contents to set the members of your Person object, Print the Guess Who People heading, Print the Person that you read earlier Prompt the user for a feature (name, haircolor, hairtype, gender, glasses, eyecolor, or hat) Print...

  • C++ please! (1) Prompt the user to enter the name of the input file. The file...

    C++ please! (1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string. Ex: Enter file name: input1.txt File content: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! (2) Implement a PrintMenu() function,...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Code a program and reads a maze file and exits non-zero if the maze is not...

    Code a program and reads a maze file and exits non-zero if the maze is not valid. Or outputs the maze in a frame if it is good. The name of the file containg the maze is the only command-line parameter to this program. Read that file with the stdio functions, and close it propperly. A maze is a rectangular array of characters. It is encoded in the file in 2 parts: header line The header has 2 decimal intergers...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • I want to make a really simple maze game in Python. I need to use tkinter and GUI to make this ha...

    I want to make a really simple maze game in Python. I need to use tkinter and GUI to make this happened. Under you can see the description of the task, but i need help to getting startet. If there is an other easier way I'm just happy for the help!! task Description: You have to create a maze game. The goal of the game is to get out of the maze. The game should read The maze from a...

  • Program In Assembly For this part, your MAL program must be in a file named p5b.mal....

    Program In Assembly For this part, your MAL program must be in a file named p5b.mal. It must have at least one function in addition to the main program. For the purposes of Part (b), you may assume the following 1. Any line of text typed by a user has at most 80 characters including the newline character. 2. A whitespace character refers to a space, a tab or the new line character. 3. A word is any sequence of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT