Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Program Purpose: This program is to solve the maze by controlling the robot. /*********************************************************************/ /* Title: Maze Robot Maneuvering */ /* By Devika A/P

// Program Purpose: This program is to solve the maze by controlling the robot. /*********************************************************************/ /* Title: Maze Robot Maneuvering */ /* By Devika A/P Ramachandiran, Yeoh Wei Loon */ /*********************************************************************/

#include #include #include #include #include #define num_of_rows 10 #define num_of_col 30 #define num_of_turns 75 using namespace std;

// Structure declaration struct maze { string user_name; char maze_option; char maze [num_of_rows][num_of_col]; };

// Function prototype declaration void disp_message (string ); void get_user_info (maze& ); void start_maze (ifstream& , ifstream& , maze ); void save_maze_record (maze , int ); void maze_instruction (); void disp_maze_record (maze , int );

int main() { ifstream fin1 ("Maze.txt"); ifstream fin2 ("Maze2.txt"); // Variable datatype declaration char option; int turns; maze info; string welc_message; disp_message (welc_message); // Function call for displaying the welcoming message get_user_info (info); // Function call for getting user's name main_menu: cout << " "; cout << "\t\t\t\t\tMENU" << endl; cout << "\t\t\t\tA. Start" << endl; cout << "\t\t\t\tB. Instruction" << endl; cout << "\t\t\t\tC. Maze Record" << endl; cout << "\t\t\t\tD. Quit" << endl; cout << " \t\t\t\tIf you are new user, please press on instruction to read" << endl; cout << "\t\t\t\tbefore starting to solve the maze. " << endl; cout << "\t\t\t\tPlease choose your option: "; cin >> option; cin.ignore(100,' '); while (option != 'A' && option != 'a' && option != 'B' && option != 'b' && option != 'C' && option != 'c' && option != 'D' && option != 'd') { cerr << " \t\t\t\tPlease enter the option listed above. " << endl; cout << "\t\t\t\tPlease choose your option: "; cin >> option; } system ("cls"); switch (option) { case 'A': case 'a': { start_maze (fin1, fin2, info); // Function call for starting to solve the maze chosen by user goto main_menu; } break; case 'B': case 'b': { maze_instruction (); // Function call for displaying the maze instruction goto main_menu; } break; case 'C': case 'c': { disp_maze_record (info, turns); // Function call for displaying the maze record goto main_menu; } case 'D': case 'd': exit(0); } fin1.close(); fin2.close(); }

/********************************************************************/ /* Function header for displaying the welcoming message */ /********************************************************************/ void disp_message (string welc_message) { ifstream fin ("Welcoming message.txt"); cout << " "; for (int a = 1; fin.good(); a++) { getline(fin, welc_message); cout << welc_message << endl; } fin.close(); getch(); system ("cls"); }

/********************************************************************/ /* Function header for getting user's name */ /********************************************************************/ void get_user_info (maze& info) { cout << " "; cout << "\t\t\t\tPlease enter your username. " << endl; cout << "\t\t\t\tYour username can only have at most 8 characters." << endl; cout << "\t\t\t\tUsername: "; getline(cin, info.user_name); while (info.user_name.size() > 8) { cerr << " \t\t\t\tYou can only enter at most 8 characters." << endl; cerr << "\t\t\t\tPlease re-enter your username." << endl; cout << "\t\t\t\tUsername: "; getline(cin, info.user_name); } system ("cls"); }

/********************************************************************/ /* Function header for starting to solve the maze chosen by user */ /********************************************************************/ void start_maze (ifstream& fin1, ifstream& fin2, maze info) { char keys, quit_key; // Initialize variables int px = 1; int py = 1; cout << " "; cout << "\t\t\t\tMaze option" << endl; cout << "\t\t\t\t1. Maze 1" << endl; cout << "\t\t\t\t2. Maze 2" << endl; cout << "\t\t\t\tPlease choose your maze option: "; cin >> info.maze_option; while (info.maze_option != '1' && info.maze_option != '2') { cerr << " \t\t\t\tPlease enter the maze option listed above." << endl; cout << "\t\t\t\tPlease choose your maze option: "; cin >> info.maze_option; } system ("cls"); switch (info.maze_option) { case '1': { int turns = 0; for (;turns < num_of_turns; turns++) { cout << " "; cout << setw(56) << "Maze " << info.maze_option << endl << endl; for (int row = 0; row < num_of_rows; row++) { cout << "\t\t\t\t\t"; for (int col = 0; col < num_of_col; col++) { fin1 >> info.maze [row][col]; info.maze [px][py] = '@'; cout << info.maze [row][col]; } cout << endl; } cout << endl; if (info.maze [9][29] == '@') { cout << "\t\t\t\tCongrats on solving the maze successfully!! "; cout << "\t\t\t\tPress any key to continue..."; getch(); save_maze_record (info, turns); // Function call for saving maze record in a file system ("cls"); break; } cout << "\t\t\t\t" << num_of_turns - turns << " turns left. "; cout << "\t\t\t\tKey: "; cin >> keys; if (keys == 'S' || keys == 's') { if (info.maze[px+1][py] != '#') { system ("cls"); info.maze [px][py] = '.'; px++; } else { system ("cls"); cout << " " << setw (65) << "!!!You hit the wall!!!"; turns--; } } else if (keys == 'W' || keys == 'w') { if (info.maze[px-1][py] != '#') { system ("cls"); info.maze [px][py] = '.'; px--; } else { system ("cls"); cout << " " << setw (65) << "!!!You hit the wall!!!"; turns--; } } else if (keys == 'A' || keys == 'a') { if (info.maze[px][py-1] != '#') { system ("cls"); info.maze [px][py] = '.'; py--; } else { system ("cls"); cout << " " << setw (65) << "!!!You hit the wall!!!"; turns--; } } else if (keys == 'D' || keys == 'd') { if (info.maze[px][py+1] != '#') { system ("cls"); info.maze [px][py] = '.'; py++; } else { system ("cls"); cout << " " << setw (65) << "!!!You hit the wall!!!"; turns--; } } else { system ("cls"); cout << " \t\t\t\t" << "!!!You press key wrongly. Please re-enter again!!!"; turns--; } } cout << " "; if (turns == num_of_turns) { cout << "\t\t\t\tSorry. You already use all turns." << endl; cout << "\t\t\t\tPress any key to continue..."; getch(); } system ("cls"); } break; case '2': { int turns = 0; for (;turns < num_of_turns; turns++) { cout << " "; cout << setw(56) << "Maze " << info.maze_option << endl << endl; for (int row = 0; row < num_of_rows; row++) { cout << "\t\t\t\t\t"; for (int col = 0; col < num_of_col; col++) { fin2 >> info.maze [row][col]; info.maze [px][py] = '@'; cout << info.maze [row][col]; } cout << endl; } cout << endl; if (info.maze [9][29] == '@') { cout << "\t\t\t\tCongrats on solving the maze successfully!! "; cout << "\t\t\t\tPress any key to continue..." << endl; getch(); save_maze_record (info, turns); // Function call for saving maze record in a file system ("cls"); break; } cout << "\t\t\t\t" << num_of_turns - turns << " turns left. "; cout << "\t\t\t\tKey: "; cin >> keys; if (keys == 'S' || keys == 's') { if (info.maze[px+1][py] != '#') { system ("cls"); info.maze [px][py] = '.'; px++; } else { system ("cls"); cout << " " << setw (65) << "!!!You hit the wall!!!"; turns--; } } else if (keys == 'W' || keys == 'w') { if (info.maze[px-1][py] != '#') { system ("cls"); info.maze [px][py] = '.'; px--; } else { system ("cls"); cout << " " << setw (65) << "!!!You hit the wall!!!"; turns--; } } else if (keys == 'A' || keys == 'a') { if (info.maze[px][py-1] != '#') { system ("cls"); info.maze [px][py] = '.'; py--; } else { system ("cls"); cout << " " << setw (65) << "!!!You hit the wall!!!"; turns--; } } else if (keys == 'D' || keys == 'd') { if (info.maze[px][py+1] != '#') { system ("cls"); info.maze [px][py] = '.'; py++; } else { system ("cls"); cout << " " << setw (65) << "!!!You hit the wall!!!"; turns--; } } else { system ("cls"); cout << " \t\t\t\t" << "!!!You press key wrongly. Please re-enter again!!!"; turns--; } } cout << " "; if (turns == num_of_turns) { cout << "\t\t\t\tSorry. You already use all turns." << endl; cout << "\t\t\t\tPress any key to continue..."; getch(); } system ("cls"); } break; } }

/********************************************************************/ /* Function header for saving maze record in a file */ /********************************************************************/ void save_maze_record (maze info, int turns) { ofstream fout ("User_maze_record.txt", ios::app); fout << info.user_name << "\t\t\t" << setw (6) << info.maze_option << "\t\t\t" << setw (12) << num_of_turns - turns << endl; fout.close(); system ("cls"); }

/********************************************************************/ /* Function header for displaying the maze instruction */ /********************************************************************/ void maze_instruction () { cout << " "; cout << "\t\tTo solve the maze successfully, you need to read and follow these instructions carefully." << endl << endl; cout << "\t\t\t\t@ ------------- robot (you)" << endl; cout << "\t\t\t\t# ------------- blocking wall" << endl; cout << "\t\t\t\tW ------------- up" << endl; cout << "\t\t\t\tA ------------- left" << endl; cout << "\t\t\t\tS ------------- down" << endl; cout << "\t\t\t\tD ------------- right" << endl << endl; cout << "\t\t\t\tPress any keys to go back to menu"; getch(); system ("cls"); }

/********************************************************************/ /* Function header for displaying the maze record */ /********************************************************************/ void disp_maze_record (maze info, int turns) { string record; ifstream fin3 ("User_maze_record.txt"); cout << "\t\t\t--------------------------------------------------------------------- "; cout << "\t\t\tUsername Maze choice Turns left" << endl; cout << "\t\t\t--------------------------------------------------------------------- "; while (!fin3.eof()) { getline (fin3, record); cout << "\t\t\t" << record << endl; } fin3.close(); cout << "\t\t\tPress any keys to go back to menu"; getch(); system ("cls"); }

(Need methodology for this coding)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 2 Lncs 8056

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013th Edition

3642401724, 978-3642401725

More Books

Students also viewed these Databases questions

Question

Does it avoid use of underlining?

Answered: 1 week ago