Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is a small program that roughly represents a robot in a room. The room is

ID: 3738458 • Letter: B

Question

Below is a small program that roughly represents a robot in a room. The room is defined in a
two-dimensional array of characters. Objects in the room are stored as characters in the array
named room that represents the room. 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.

#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
void print(char[][MAXY]);
int main()
{
char c
WINDOW* wnd;
char room[MAXX][MAXY]; // stores the room
int x = MAXX/2, y = MAXY/2; // robot’s initial location
wnd = initscr(); // use curses to initialize the window
clear(); // use curses to clear the window
refresh(); // use curses to reprint the window
setup(room,x,y); // setup the room with the robot
print(room); // write this function!!
2
while ((c = getch()) != ’q’) // will pause until user presses ’q’
; // on the keyboard
endwin(); // frees the screen for normal use
}
/* Sets up the room, adding walls, and the robot */
void setup(char r[][MAXY], int x, int y)
{
for(int i = 0; i < MAXX; i++)
{
for(int j = 0; j < MAXY; j++)
{
r[i][j] = ’ ’; // empty the room
}
}
for(int i = 0; i < MAXY; i++) {
r[0][i] = ’|’; // left wall
r[MAXX-1][i] = ’|’; // right wall
}
for(int i = 0; i < MAXX; i++) {
r[i][0] = ’-’; // top wall
r[i][MAXY-1] = ’-’; // bottom wall
}
r[x][y] = ’R’; // place the robot
}

The function named setup() stores all of the initial characters into the 2 dimensional array
that represents the room. For this lab your task is to build the function named print() that
prints the room to the computer screen. You will need to use the curses library to write your
function that prints the room. A reference for the curses library appears on the course website.
Your print() function should take the room array as an argument and should loop through
the entire room array, moving to the correct location on the screen and printing the character
stored in the array that is to appear in the room at that location. Look at the syntax of the
setup() function to determine how to pass the room array into your print() function. Please
read the curses reference carefully, because you will need to use functions contained in the curses
library to accomplish your goal. For example, your print() 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. If you have questions regarding the use of the curses library, you should ask your
instructor, or execute the command “man ncurses” on wormulon to read the manual for the
curses library.
To complete this lab you must do the following:
• Copy the program shown above into a file named ‘lab8.cpp’.
• Create your function named print to print the room as described above and add it to the
program.
• Test your program to ensure that it functions correctly.

Explanation / Answer

CODE :

#include <curses.h>
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
void print(char[][MAXY]);
int main()
{
char c
WINDOW* wnd;
char room[MAXX][MAXY]; // stores the room
int x = MAXX/2, y = MAXY/2; // robot’s initial location
wnd = initscr(); // use curses to initialize the window
clear(); // use curses to clear the window
refresh(); // use curses to reprint the window
setup(room,x,y); // setup the room with the robot
print(room); // write this function!!
2
while ((c = getch()) != ’q’) // will pause until user presses ’q’
; // on the keyboard
endwin(); // frees the screen for normal use
}
/* Sets up the room, adding walls, and the robot */
void setup(char r[][MAXY], int x, int y)
{
for(int i = 0; i < MAXX; i++)
{
for(int j = 0; j < MAXY; j++)
{
r[i][j] = ’ ’; // empty the room
}
}
for(int i = 0; i < MAXY; i++) {
r[0][i] = ’|’; // left wall
r[MAXX-1][i] = ’|’; // right wall
}
for(int i = 0; i < MAXX; i++) {
r[i][0] = ’-’; // top wall
r[i][MAXY-1] = ’-’; // bottom wall
}
r[x][y] = ’R’; // place the robot
}
void print(char room[][MAXY])
{
int r,c;
for(r=0;r<MAXX;r++)
{
for(c=0;c<MAXY;c++)
{
move(r,c);//move to location r,c
addch(room[r][c]);//print value of room at [r][c]'th location
}
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote