Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need help with C++, thank you in advance! //------------------------------------------------------------------------------- // Name: hw5.cpp // Purpose: To play a simple Pokemon game using 2D arrays // Authors:
Need help with C++, thank you in advance!
//-------------------------------------------------------------------------------
// Name: hw5.cpp
// Purpose: To play a simple Pokemon game using 2D arrays
// Authors: starter code: Joshua Burbridge and Susan Gauch
// completed code: YOUR NAME(S) HERE
// Date: April, 2018
//-------------------------------------------------------------------------------
#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
else
{
//Set the player location
playerRow = ROWS - 1;
playerCol = 0;
//Iterate over the rows and the columns
for(int row = 0; row
for(int col = 0; 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
- warp right, ^ - warp up, v - warp down "
// print the map top border
cout
for(int col = 0; col
cout
cout
// print the path top border
for(int col = 0; 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
{
// print the map
cout
for(int col = 0; col
{
if (playerRow == row && playerCol == col)
cout
else
cout
}
// print the path
cout
cout
for(int col = 0; col
{
if (playerRow == row && playerCol == col)
cout
else
cout
}
// print the map bottom border
cout
for(int col = 0; col
cout
// print the path bottom border
cout
for(int col = 0; col
cout
cout
Objective In this assignment, you will read a file to load a map into a 2-dimensional array. You will then allow the user to make moves around the map to navigate to the finish. Unlike the previous homework assignments, this time you will have two options: Work in pairs and write one extra function Work alone, but you don't have to write the extra function 1. 2. Prerequisites To complete this assignment, you should be familiar with the following concepts: While loops . Functions .2-dimensional arrays Reading and writing text files Getting Started You can copy the code and map to your directory with: turings cp sgauch/public_html/2004/S18/hw/hw5.cpp. [there is a dot for "current dir"] turingS cp sgauch/public_html/2004/S18/hw/map.txt. [there is a dot for "current dir"] You can run our version on turing to see what it does with: turing "sgauch/public_html/2004/S18/hw/hw5.exe Description If you've ever played the original Pokemon Red or Blue games, you might remember solving puzzles that looked like this }
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(???)
{
}
//-----------------------------------------------------------------------------------
// 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(???)
{
}
//-----------------------------------------------------------------------------------
// 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(???)
{
}
//-----------------------------------------------------------------------------------
// 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(???)
{
}
//-----------------------------------------------------------------------------------
// 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(???)
{
}
//-----------------------------------------------------------------------------------
// 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(???)
{
}
int main()
{
//Declare two 2D char arrays for the map and path
//Each array should have ROWS rows and COLS cols
//Declare two ints to keep track of the player location, their row and col in the map
//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
//While we have not won the game by reaching the top right corner
//do
//Print map and path
//Get a valid type of move from the player
//If the user wants to quit, save the game and set done to true
//Check the move to see if it is legal
//If the move is legal, make it
//Check to see if the user has won after the move
//while(the user has not won and the user has not quit
//If the player won the game, print a congratulatory message
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started