logic

.cpp

School

Texas A&M University *

*We aren’t endorsed by this school

Course

121

Subject

Computer Science

Date

Jun 13, 2024

Type

cpp

Pages

6

Uploaded by CoachElement14434

#include <iostream> #include <fstream> #include <string> #include "logic.h" using std::cout, std::endl, std::ifstream, std::string; /** * TODO: Student implement this function * Load representation of the dungeon level from file into the 2D map. * Calls createMap to allocate the 2D array. * @param fileName File name of dungeon level. * @param maxRow Number of rows in the dungeon table (aka height). * @param maxCol Number of columns in the dungeon table (aka width). * @param player Player object by reference to set starting position. * @return pointer to 2D dynamic array representation of dungeon map with player's location., or nullptr if loading fails for any reason * @updates maxRow, maxCol, player */ char** loadLevel(const string& fileName, int& maxRow, int& maxCol, Player& player) { bool valid = false; ifstream input_file(fileName); if (!input_file.is_open()){ return nullptr; }if (!(input_file>> maxRow >> maxCol)){ return nullptr; }if (maxRow <= 0 || maxCol <= 0 || maxRow > (INT32_MAX/maxCol) || maxCol > 999999 || maxRow > 999999){ return nullptr; } char** gamemap = createMap(maxRow, maxCol); if (!gamemap){ return nullptr; } if(!(input_file >> player.row >> player.col)){ return nullptr; }if ( player.col < 0 || player.col >= maxCol || player.row<0 || player.row >= maxRow){ deleteMap(gamemap, maxRow); return nullptr; } for (int row = 0; row < maxRow; ++row){ for(int col = 0; col < maxCol; ++col){ char tile; if (!(input_file >> tile)){ deleteMap(gamemap, maxRow); return nullptr; } gamemap[row][col]=tile; if(!(tile == TILE_AMULET || tile == TILE_EXIT || tile == TILE_MONSTER || tile == TILE_DOOR || tile == TILE_OPEN || tile == TILE_PILLAR || tile == TILE_TREASURE)){ deleteMap(gamemap, maxRow); return nullptr; }if (tile == TILE_EXIT || tile == TILE_DOOR){ valid = true; } }
} if (valid == false){ deleteMap(gamemap, maxRow); return nullptr; } char extravalue; if (input_file >> extravalue){ deleteMap(gamemap, maxRow); return nullptr; }if (gamemap[player.row][player.col] == TILE_OPEN){ gamemap[player.row][player.col] = TILE_PLAYER; }else{ deleteMap(gamemap, maxRow); return nullptr; } input_file.close(); return gamemap; } /** * TODO: Student implement this function * Translate the character direction input by the user into row or column change. * That is, updates the nextRow or nextCol according to the player's movement direction. * @param input Character input by the user which translates to a direction. * @param nextRow Player's next row on the dungeon map (up/down). * @param nextCol Player's next column on dungeon map (left/right). * @updates nextRow, nextCol */ void getDirection(char input, int& nextRow, int& nextCol) { if(input == MOVE_UP){ nextRow += -1; nextCol += 0; }else if(input == MOVE_DOWN){ nextRow += 1; nextCol += 0; }else if(input == MOVE_LEFT){ nextCol += -1; nextRow += 0; }else if(input == MOVE_RIGHT){ nextCol += 1; nextRow += 0; }else{ nextCol += 0; nextRow += 0; } } /** * TODO: [suggested] Student implement this function * Allocate the 2D map array. * Initialize each cell to TILE_OPEN. * @param maxRow Number of rows in the dungeon table (aka height). * @param maxCol Number of columns in the dungeon table (aka width). * @return 2D map array for the dungeon level, holds char type. */ char** createMap(int maxRow, int maxCol) { if(maxCol <= 0 || maxRow <= 0){
return nullptr; } char** gamemap = new char*[maxRow]; for (int row = 0; row < maxRow; ++row){ gamemap[row] = new char [maxCol]; }for (int row = 0; row < maxRow; row++){ for(int col = 0; col < maxCol; col++){ gamemap[row][col] = TILE_OPEN; } } return gamemap; } /** * TODO: Student implement this function * Deallocates the 2D map array. * @param map Dungeon map. * @param maxRow Number of rows in the dungeon table (aka height). * @return None * @update map, maxRow */ void deleteMap(char**& map, int& maxRow) { if((map != nullptr) && (maxRow >0)){ for(int row = 0; row < maxRow; row++){ delete[] map[row]; } delete[] map; } map = nullptr; maxRow = 0; } /** * TODO: Student implement this function * Resize the 2D map by doubling both dimensions. * Copy the current map contents to the right, diagonal down, and below. * Do not duplicate the player, and remember to avoid memory leaks! * You can use the STATUS constants defined in logic.h to help! * @param map Dungeon map. * @param maxRow Number of rows in the dungeon table (aka height), to be doubled. * @param maxCol Number of columns in the dungeon table (aka width), to be doubled. * @return pointer to a dynamically-allocated 2D array (map) that has twice as many columns and rows in size. * @update maxRow, maxCol */ char** resizeMap(char** map, int& maxRow, int& maxCol) { int nuevomax_row = maxRow * 2; int nuevomax_col = maxCol * 2; if (map == nullptr || maxCol < 1 || maxRow < 1){ return nullptr; } char** new_map = createMap(nuevomax_row, nuevomax_col); for (int row = 0; row < maxRow; row++){ for (int col = 0; col < maxCol; col++){ new_map[row][col] = map[row][col]; if (map[row][col] == TILE_PLAYER){
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help