\"The small program in C++ below roughly represents a robot in a room. The room
ID: 3810724 • Letter: #
Question
"The small program in C++ below roughly represents a robot in a room. The room is dened as a 2 dimensional array of characters. Objects in the room are stored as characters in the array. For example, the location of the robot is stored as an ’R’ in the array. The walls of the room are stored in the array as a - for the top and bottom walls and as | for the side walls. The function named setup() puts all of the initial characters into the 2 dimensional array. *****Use the curses library to write a function that prints the room.***** The function should take the room array as an argument. The function should loop through the entire array, moving to the correct location of the screen and printing the character stored in the array. E.g., the function should ‘move’ to location (r,c) and print the [r][c]’th element of the array for each r and c within the dimensions of the room.
#include <curses.h> // include the curses library
using namespace std;
const int MAXX = 40; // maximum size of the ’room’ const int MAXY = 20;
void Setup( char[][MAXY], int x, int y ); // room setup
int main() { WINDOW *wnd; char room[MAXX][MAXY]; // stores the room int x = MAXX/2, y = MAXY/2; // robot’s initial location
wnd = initscr(); // ’initializes’ the window clear(); // clears the window refresh(); // reprints the window
Setup(room,x,y); // setup the room with the robot
Print(room); // write this function!!*****
WaitForQuit();
endwin(); // frees the screen for normal use
}
2
/* Sets up the room, adding walls, and the robot */ void Setup( char room[][MAXY], int x, int y ) { for(int i = 0; i < MAXX; i++) { for(int j = 0; j < MAXY; j++) { room[i][j] = ’ ’; // empty the room } }
for(int i = 0; i < MAXY; i++) { room[0][i] = ’|’; // left wall room[MAXX-1][i] = ’|’; // right wall }
for(int i = 0; i < MAXX; i++) { room[i][0] = ’-’; // top wall room[i][MAXY-1] = ’-’; // bottom wall }
room[x][y] = ’R’; // place the robot
}
void WaitForQuit() { char ch;
do { cin >> ch; } while( ch != ’q’ );
}
*******Add code to search the room for the robot later on. If it is found, mark it with an ’X’, if not, mark the location with a ’.’.*****"
Explanation / Answer
//main.cpp
=======================================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.