Question
Need help C++ Text file with the map: > . . . . . C F . X X X X X X X ^
Need help C++
Text file with the map:
> . . . . . C F . X X X X X X X ^ . . v . > . C > . ^
//-------------------------------------------------------------------------------
// 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
}
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;
}
In our version of the puzzle, we will represent the map using simple ASCII characters. Your program will first read a file that looks ike map.txt, which is included with this assignment. The file will contain 8 rows of 8 characters separated by spaces. These characters will represent the map. Note: map.txt stores for spaces. When this file is read by init, we check for "" and store "(space) in the array instead so the map looks nicer when displayed on the screen. The starting tile will always be in the lower left hand comer of the map, and the finishing tile will always be in the upper right hand corner. Spaces marked with X are boxes. You cannot land on these and they stop forward progress. Spaces marked with ,, or v are warp tiles and stepping onto one will possibly change your direction. Tiles marked with Care choice bles. These stop forward progress and the user must choose a direction to go, which they will indicate by typing w, a, s, or d. For a full list of the tile symbols and what they mean, refer to the const char definitions at the beginning of the starter code Legal moves: you cannot move to a wall or a box; check that a next location is legal before you move the user. All moves are checked to be sure that they are legal before they are taken When you make a move, here is the effect of each type of the tile you land on on future movement Space tiles( keep moving in the current direction chosen by the user. warp tiles ( <.>.*,V): keep mong, but change direction based on which warp tile encountered Choice tiles (C): stop moving to get a new direction from the user To make debugging land grading) easier, you should also keep track of which tiles the player has already visited. In the sample output below, you can see that on every turn, two 20 arrays are printed. The first one contains the map, so they player can see what they're doing. The second one keeps track of the player's path through the board. Every time a user makes a move, you will need to update both arrays appropriately. In the starter code, you have been given a main function that is heavily commented. Your main function should follow this general layout. You have also been gven two functions: init and print. Init opens a text file and reads the map into the map array Print prints everything out neatly. You have also been given some define statements that will make your code easier to read and update. For example, const char HARPUP- ; const char uP - allows you to make statements such as char nextNove char tile naplrllel case UP: plaezRow-break; case MARP UP: nextMoveUps break Implemented this way, you can easily change which characters you use to represent certain tiles, and you won't have to change anything ese your code. Additionally, your code will look more Ske plain English. However, please do not actually change the const char statements at the top, because then you will be unable to read the map that will be used to test your code. In addition to the starter code, you will also need to write the following 4 functions (ordered easiest to gameOver-returns true or false based on whether or not the player has reached the finish tile getMove- gets a move from the player and makes sure it's in the set (w, a, s d . .legal-makes sure a move is legal (doesn't move onto a box or walk off the boundaries of the mapl makeMove- updates the board and the path to reflect the user's move. Keeps looping making legal moves based on the bilels) landed on (ie, warp tiles may change direction). Stops moving e when the user lands on a choice tile or the finish tile, or when the user is out of legal moves in that direction (ie, hits a box tile or the edge of the map) save (only if you're working with a partner)-This function should let the user save and quit midway through the game. In order to do this, you will need to write the player's location (current row and column) and both the map array and the path array to a text fle. If you're working alone, you do not need to write this function. You need to write a for any""(space) stored in the # load (only if you're working with a partner)-This function should let the user load a saved game from afile. In order to do this, you will need to read the player's location (current row and collumn) and both the map array and the path array from a text file. you're working alone, you do not need to write this function. Just ike in init, you need to store "(space) in the array for any stored in the file. It is recommended that you start with the easiest functions, and make sure those are working before you move onto the harder functions In particular, you should start by just doing one move at a time in the user's direction, ignoring the type of tile that the user lands on. Just keep the user on the map. Then allow the user to move one at a time until they get to the finish. Then, add handling ble types one at a time (eg, spaces- those need a loop to keep moving in the same direction; then box tiles to stop movement, then choice tiles to stop movement; finally warp tiles to change direction). When grading your code, we will use the character map defined at the top of the sample code The size of all boards will be 8rows by 8columns (for fun, you can design your own maps and test on larger boards) The starting position will be in the lower left corner (row 7, col 0) The finishing position will be in the top night corner [row O, col 7 The name of the fle you read from will be "map.txt You will not encounter any wild pokemon on your way through the maze In our version of the puzzle, we will represent the map using simple ASCII characters. Your program will first read a file that looks ike map.txt, which is included with this assignment. The file will contain 8 rows of 8 characters separated by spaces. These characters will represent the map. Note: map.txt stores for spaces. When this file is read by init, we check for "" and store "(space) in the array instead so the map looks nicer when displayed on the screen. The starting tile will always be in the lower left hand comer of the map, and the finishing tile will always be in the upper right hand corner. Spaces marked with X are boxes. You cannot land on these and they stop forward progress. Spaces marked with ,, or v are warp tiles and stepping onto one will possibly change your direction. Tiles marked with Care choice bles. These stop forward progress and the user must choose a direction to go, which they will indicate by typing w, a, s, or d. For a full list of the tile symbols and what they mean, refer to the const char definitions at the beginning of the starter code Legal moves: you cannot move to a wall or a box; check that a next location is legal before you move the user. All moves are checked to be sure that they are legal before they are taken When you make a move, here is the effect of each type of the tile you land on on future movement Space tiles( keep moving in the current direction chosen by the user. warp tiles ( <.>.*,V): keep mong, but change direction based on which warp tile encountered Choice tiles (C): stop moving to get a new direction from the user To make debugging land grading) easier, you should also keep track of which tiles the player has already visited. In the sample output below, you can see that on every turn, two 20 arrays are printed. The first one contains the map, so they player can see what they're doing. The second one keeps track of the player's path through the board. Every time a user makes a move, you will need to update both arrays appropriately. In the starter code, you have been given a main function that is heavily commented. Your main function should follow this general layout. You have also been gven two functions: init and print. Init opens a text file and reads the map into the map array Print prints everything out neatly. You have also been given some define statements that will make your code easier to read and update. For example, const char HARPUP- ; const char uP - allows you to make statements such as char nextNove char tile naplrllel case UP: plaezRow-break; case MARP UP: nextMoveUps break Implemented this way, you can easily change which characters you use to represent certain tiles, and you won't have to change anything ese your code. Additionally, your code will look more Ske plain English. However, please do not actually change the const char statements at the top, because then you will be unable to read the map that will be used to test your code. In addition to the starter code, you will also need to write the following 4 functions (ordered easiest to gameOver-returns true or false based on whether or not the player has reached the finish tile getMove- gets a move from the player and makes sure it's in the set (w, a, s d . .legal-makes sure a move is legal (doesn't move onto a box or walk off the boundaries of the mapl makeMove- updates the board and the path to reflect the user's move. Keeps looping making legal moves based on the bilels) landed on (ie, warp tiles may change direction). Stops moving e when the user lands on a choice tile or the finish tile, or when the user is out of legal moves in that direction (ie, hits a box tile or the edge of the map) save (only if you're working with a partner)-This function should let the user save and quit midway through the game. In order to do this, you will need to write the player's location (current row and column) and both the map array and the path array to a text fle. If you're working alone, you do not need to write this function. You need to write a for any""(space) stored in the # load (only if you're working with a partner)-This function should let the user load a saved game from afile. In order to do this, you will need to read the player's location (current row and collumn) and both the map array and the path array from a text file. you're working alone, you do not need to write this function. Just ike in init, you need to store "(space) in the array for any stored in the file. It is recommended that you start with the easiest functions, and make sure those are working before you move onto the harder functions In particular, you should start by just doing one move at a time in the user's direction, ignoring the type of tile that the user lands on. Just keep the user on the map. Then allow the user to move one at a time until they get to the finish. Then, add handling ble types one at a time (eg, spaces- those need a loop to keep moving in the same direction; then box tiles to stop movement, then choice tiles to stop movement; finally warp tiles to change direction). When grading your code, we will use the character map defined at the top of the sample code The size of all boards will be 8rows by 8columns (for fun, you can design your own maps and test on larger boards) The starting position will be in the lower left corner (row 7, col 0) The finishing position will be in the top night corner [row O, col 7 The name of the fle you read from will be "map.txt You will not encounter any wild pokemon on your way through the mazeStep 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