Question: 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
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.
Prerequisites
To complete this assignment, you should be familiar with the following concepts:
While loops
Functions
2-dimensional arrays
Reading and writing text files
Description
We will represent the map using simple ASCII characters. Your program will first read a file that looks like 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 corner 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 C are choice tiles. 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 moving, but change direction based on which warp tile is encountered
Choice tiles ( C): stop moving to get a new direction from the user
To make debugging (and 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 2D arrays are printed. The first one contains the map, so they player can see what theyre doing. The second one keeps track of the players 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 given 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 WARP_UP = ^;
const char UP =w;
allows you to make statements such as
char nextMove; char tile = map[r][c]; switch (nextMove)
{
case UP: playerRow--; break; .
}
switch (tile)
{ case WARP_UP: nextMove = UP; break; }
Implemented this way, you can easily change which characters you use to represent certain tiles, and you wont have to change anything else your code. Additionally, your code will look more like 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 hardest):
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 its in the set {w, a, s, d}
legal makes sure a move is legal (doesnt move onto a box or walk off the boundaries of the map)
makeMove updates the board and the path to reflect the users move. Keeps looping making legal moves based on the tile(s) landed on (i.e., warp tiles may change direction). Stops moving when the user lands on a choice tile or the finish tile, or when the user is out of legal moves in that direction (i.e., hits a box tile or the edge of the map)
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 users 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 tile types one at a time (e.g., 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).
Guarantees
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 8 rows by 8 columns (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 right corner (row 0, col 7)
The name of the file you read from will be map.txt
Text file with the map:
> . . . . . C F . X X X X X X X ^ . < X v . . < X X ^ . . < X ^ > . v . < ^ < C . . . . . X X . ^ X . X ^ X X . S > > . 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 << "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(???) //----------------------------------------------------------------------------------- // 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
Get step-by-step solutions from verified subject matter experts
