Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone explain what is happening in the C++ bolded/italicized code. #include #include #include using namespace std; const int ROWS = 8; const int COLS

Can someone explain what is happening in the C++ bolded/italicized code.

#include

#include

#include

using namespace std;

const int ROWS = 8;

const int COLS = 8;

//Character map for user moves

const char UP = 'w';

const char LEFT = 'a';

const char DOWN = 's';

const char RIGHT = 'd';

//Character map for tile types

const char START = 'S';

const char FINISH = 'F';

const char SPACE = ' ';

const char WARP_LEFT = '<';

const char WARP_RIGHT = '>';

const char WARP_UP = '^';

const char WARP_DOWN = 'v';

const char BOX = 'X';

const char CHOICE = 'C';

const char TRACK = 'o';

//Character map for tile types

const char PLAYER = 'A';

//-----------------------------------------------------------------------------------

// Name: init

// Purpose: This function opens a file, reads the dimensions of the map, and loads a map into the 2D array

// It prints an error message to cerr if the file cannot be opened.

// Any '.' in the map file are converted to spaces in the array.

// Parameters: filename, const string, the file holding the map

// map, 2D array of characters, the map filled in from the file

// path, 2D array of characters, sets the path to the start location

// playerRow, int, the player row location (updated by fn)

// playerCol, int, the player col location (updated by fn)

// Returns: void

//-----------------------------------------------------------------------------------

void init(const string filename, char map[ROWS][COLS], char path[ROWS][COLS], int &playerRow, int &playerCol)

{

//Open the file

ifstream din(filename.c_str());

if (!din)

cerr << "Could not open " << filename << " .";

else

{

//Set the player location

playerRow = ROWS - 1;

playerCol = 0;

//Iterate over the rows and the columns

for(int row = 0; row < ROWS; row++)

for(int col = 0; col < COLS; col++)

{

//Read the data into the map

din >> map[row][col];

if (map[row][col] == '.')

map[row][col] = SPACE; //convert from the . in the file to SPACE in the array

//Init the path with the START and FINISH locations

//Fill the rest of the path with spaces

if (map[row][col] == START || map[row][col] == FINISH)

path[row][col] = map[row][col];

else

path[row][col] = SPACE;

}

//Close the file

din.close();

}

}

//-----------------------------------------------------------------------------------

// Name: print

// Purpose: This function prints the map to the screen

// Parameters: map, const 2D array of characters, stores the map

// path, const 2D array of characters, stores the player path

// playerRow, const int, the player row location

// playerCol, const int, the player col location

// Returns: void

//-----------------------------------------------------------------------------------

void print(const char map[ROWS][COLS], const char path[ROWS][COLS], const int playerRow, const int playerCol)

{

cout << "Here is the map and path. The legend is: "

<< " S - start, F - finish "

<< " < - warp left, > - warp right, ^ - warp up, v - warp down "

<< " X - box, C - choice "

<< " A - your location, o - your track ";

// print the map top border

cout << " MAP:\t\t\t\t\tPATH: -";

for(int col = 0; col < COLS; col++)

cout << "----";

cout << "\t-";

// print the path top border

for(int col = 0; col < COLS; col++)

cout << "----";

cout << " ";

// print the map contents and the path

// replace any . stored in the array with spaces on screen to look nicer

for(int row= 0; row < ROWS; row++)

{

// print the map

cout << "|";

for(int col = 0; col < COLS; col++)

{

if (playerRow == row && playerCol == col)

cout << setw(2) << PLAYER << setw(2) << " |";

else

cout << setw(2) << map[row][col] << setw(2) << " |";

}

// print the path

cout << "\t";

cout << "|";

for(int col = 0; col < COLS; col++)

{

if (playerRow == row && playerCol == col)

cout << setw(2) << PLAYER << setw(2) << " |";

else

cout << setw(2) << path[row][col] << setw(2) << " |";

}

// print the map bottom border

cout << " -";

for(int col = 0; col < COLS; col++)

cout << "----";

// print the path bottom border

cout << "\t-";

for(int col = 0; col < COLS; col++)

cout << "----";

cout << " ";

}

cout << " ";

}

//-----------------------------------------------------------------------------------

// Name: gameOver

// Purpose: This function checks to see if the game is over, i.e., the player

// has reached the FINISH tile.

// Parameters: ???

// Returns: true if the player has reached the FINISH tile; false otherwise

//-----------------------------------------------------------------------------------

// ??? gameOver(???)

bool gameOver(char map[ROWS][COLS], int r, int c)

{

if (map[r][c] == FINISH)

return true;

else

return false;

}

//-----------------------------------------------------------------------------------

// Name: getMove

// Purpose: This function gets the next move direction from the user.

// It keeps asking until a valid direction is entered

// Parameters: ????

// Returns: move, char, the valid character entered

//-----------------------------------------------------------------------------------

// ??? getMove(???)

char getMove()

{

char c;

cout << "Enter a move(w,a,s,d) or 'q' to save: ";

cin >> c;

while (c != UP && c != LEFT && c != DOWN && c != RIGHT)

{

cout << "Enter a move (w,a,s,d) or 'q' to save: ";

cin >> c;

}

return c;

}

void getnewRowCol(char map[ROWS][COLS], char path[ROWS][COLS], int r, int c, char move, int &newRow, int &newCol, bool update)

{

if (move == UP)

{

newRow = r-1;

newCol = c;

}

else if (move == LEFT)

{

newRow = r;

newCol = c-1;

}

else if (move == DOWN)

{

newRow = r + 1;

newCol = c;

}

else

{

newRow = r;

newCol = c + 1;

}

if (update)path[newRow][newCol] = TRACK;

}

//-----------------------------------------------------------------------------------

// Name: legal

// Purpose: This function checks whether or not the user can make the move requested

// by the user. Illegal moves are those that would move the user off the map

// or land the user in a box.

// Parameters: ???

// Returns: true if the user can make the move, false otherwise

//-----------------------------------------------------------------------------------

// ??? legal(???)

bool legal(char map[ROWS][COLS], char path [ROWS][COLS], int r, int c, char move)

{

int newRow, newCol;

getnewRowCol(map, path, r, c, move, newRow, newCol,false);

if (newRow < 0 || newCol < 0 || newRow >= ROWS || newCol >= COLS || map[newRow][newCol] == BOX)

return false;

else

return true;

}

//-----------------------------------------------------------------------------------

// Name: makeMove

// Purpose: This makes the move selected by the user.

// It assumes that the move has already been checked and is legal.

// It updates the player' location and adds that location to the PATH.

// It then examines the tile the user just moved to to see if it causes any

// side effects.

// If the new tile is a space tile, keep going.

// (i.e., warping to another location); if so, it then

// updates the player location again

// Parameters: ???

// Returns: void

//-----------------------------------------------------------------------------------

// ??? makeMove(???)

void makeMove(char map[ROWS][COLS], char path[ROWS][COLS], int &r, int &c, char &move)

{

getnewRowCol (map, path, r, c, move, r, c, true);

while (map[r][c] != CHOICE && map[r][c] != FINISH)

{

if (map[r][c] == WARP_UP)

{

if (legal(map, path, r, c, UP))

{

move = UP;

getnewRowCol(map, path, r, c, move, r, c, true);

}

}

else if (map[r][c] == WARP_DOWN)

{

if (legal(map, path, r, c, DOWN))

{

move = DOWN;

getnewRowCol(map, path, r, c, move, r, c, true);

}

}

else if (map[r][c] == WARP_RIGHT)

{

if (legal(map, path, r, c, RIGHT))

{

move = RIGHT;

getnewRowCol(map, path, r, c, move, r, c, true);

}

}

else if (map[r][c] == WARP_LEFT)

{

if (legal(map, path, r, c, LEFT))

{

move = LEFT;

getnewRowCol(map, path, r, c, move, r, c, true);

}

}

else if (map[r][c] == SPACE)

{

if (legal(map, path, r, c, move))

{

getnewRowCol(map, path, r, c, move, r, c, true);

}

}

}

}

//-----------------------------------------------------------------------------------

// Name: load PAIRS ONLY

// Purpose: This loads a saved game from file(s).

// It writes an error message to cerr if the file cannot be opened.

// It opens the file and sets playerRow and playerCol.

// It also fills in the map and the path matrices from the file.

// Any '.' in the map or path files are converted to spaces in the array.

// Parameters: ???

// Returns: void

//-----------------------------------------------------------------------------------

// ??? load(???)

void load()

{

}

//-----------------------------------------------------------------------------------

// Name: save PAIRS ONLY

// Purpose: This saves a game to a file.

// It opens the file and writes playerRow and playerCol.

// It prints an error message to cerr if the file cannot be opened.

// It also writes the map and the path matrices to the file.

// Any spaces in the map or path arrays are converted to '.' in the file so they are visible.

// Parameters: ???

// Returns: void

//-----------------------------------------------------------------------------------

// ??? save(???)

void save()

{

}

int main()

{

//Declare two 2D char arrays for the map and path

//Each array should have ROWS rows and COLS cols

char map[ROWS][COLS];

char path[ROWS][COLS];

//Declare two ints to keep track of the player location, their row and col in the map

int r, c;

//Ask the user whether or not they want a new game or to load a game (PAIRS)

//Initialize the map and path and player location for a new game from file "map.txt"

//or set them based on a save file

const string filename = "map.txt";

init(filename, map, path, r, c);

//While we have not won the game by reaching the top right corner

//do

while (!gameOver(map, r, c))

{

//Print map and path

print(map, path, r, c);

//Get a valid type of move from the player

char move = getMove();

//If the user wants to quit, save the game and set done to true

if (move == 'q')

{

save();

break;

}

//Check the move to see if it is legal

bool check = legal(map, path, r, c, move);

//If the move is legal, make it

//Check to see if the user has won after the move

if (check)

{

makeMove(map, path, r, c, move);

}

}

//while(the user has not won and the user has not quit

//If the player won the game, print a congratulatory message

cout << " ------------------------------------------- " << endl;

cout << " CONGRATULATIONS! You found the Silph Scope. "<

cout << " ------------------------------------------- " << endl;

print(map, path, r, c);

return 0;

}

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

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago