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

Report
#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
new_map[row][col+maxCol] = TILE_OPEN; } else{ new_map[row][col + maxCol] = map[row][col]; } if(map[row][col] == TILE_PLAYER){ new_map[row + maxRow][col + maxCol] = TILE_OPEN; } else{ new_map[row + maxRow][col+maxCol] = map[row][col]; } if (map[row][col] == TILE_PLAYER){ new_map[row+ maxRow][col] = TILE_OPEN; } else{ new_map[row + maxRow][col] = map[row][col]; } } } deleteMap(map, maxRow); maxRow = nuevomax_row; maxCol = nuevomax_col; return new_map; } /** * TODO: Student implement this function * Checks if the player can move in the specified direction and performs the move if so. * Cannot move out of bounds or onto TILE_PILLAR or TILE_MONSTER. * Cannot move onto TILE_EXIT without at least one treasure. * If TILE_TREASURE, increment treasure by 1. * Remember to update the map tile that the player moves onto and return the appropriate status. * 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). * @param maxCol Number of columns in the dungeon table (aka width). * @param player Player object to by reference to see current location. * @param nextRow Player's next row on the dungeon map (up/down). * @param nextCol Player's next column on dungeon map (left/right). * @return Player's movement status after updating player's position. * @update map contents, player */ int doPlayerMove(char** map, int maxRow, int maxCol, Player& player, int nextRow, int nextCol){ if (nextRow < 0 || nextRow >= maxRow || nextCol < 0 || nextCol >= maxCol){ return STATUS_STAY; } char n_Tile = map[nextRow][nextCol]; if(n_Tile == TILE_PILLAR || n_Tile == TILE_MONSTER){ return STATUS_STAY; } if(n_Tile == TILE_TREASURE){ player.treasure++; map[nextRow][nextCol] = TILE_PLAYER; map[player.row][player.col] = TILE_OPEN;
player.col = nextCol; player.row = nextRow; return STATUS_TREASURE; } if(n_Tile == TILE_AMULET){ map[nextRow][nextCol] = TILE_PLAYER; map[player.row][player.col] = TILE_OPEN; player.col = nextCol; player.row = nextRow; return STATUS_AMULET; } if(n_Tile == TILE_DOOR){ map[nextRow][nextCol] = TILE_PLAYER; map[player.row][player.col] = TILE_OPEN; player.col = nextCol; player.row = nextRow; return STATUS_LEAVE; } if(n_Tile == TILE_EXIT){ if(player.treasure > 0){ map[nextRow][nextCol] = TILE_PLAYER; map[player.row][player.col] = TILE_OPEN; player.row = nextRow; player.col = nextCol; return STATUS_ESCAPE; } else{ return STATUS_STAY; } } map[nextRow][nextCol] = TILE_PLAYER; map[player.row][player.col] = TILE_OPEN; player.row = nextRow; player.col = nextCol; return STATUS_MOVE; } /** * TODO: Student implement this function * Update monster locations: * We check up, down, left, right from the current player position. * If we see an obstacle, there is no line of sight in that direction, and the monster does not move. * If we see a monster before an obstacle, the monster moves one tile toward the player. * We should update the map as the monster moves. * At the end, we check if a monster has moved onto the player's tile. * @param map Dungeon map. * @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 for current location. * @return Boolean value indicating player status: true if monster reaches the player, false if not. * @update map contents */ bool doMonsterAttack(char** map, int maxRow, int maxCol, const Player& player) { int player_r = player.row; int player_c = player.col; for (int r = player_r -1; r >= 0; r-- ){
if (map[r][player_c] == TILE_MONSTER){ map[r][player_c] = TILE_OPEN; map[r + 1][player_c] = TILE_MONSTER; } else if(map[r][player_c] == TILE_PILLAR){ break; } } for (int r = player_r +1; r < maxRow; r++ ){ if (map[r][player_c] == TILE_MONSTER){ map[r][player_c] = TILE_OPEN; map[r - 1][player_c] = TILE_MONSTER; } else if(map[r][player_c] == TILE_PILLAR){ break; } } for (int c = player_c -1; c >= 0; c-- ){ if (map[player_r][c] == TILE_MONSTER){ map[player_r][c] = TILE_OPEN; map[player_r][c+1] = TILE_MONSTER; } else if(map[player_r][c] == TILE_PILLAR){ break; } } for (int c = player_c +1; c < maxCol; c++ ){ if (map[player_r][c] == TILE_MONSTER){ map[player_r][c] = TILE_OPEN; map[player_r][c-1] = TILE_MONSTER; } else if(map[player_r][c] == TILE_PILLAR){ break; } } if (map[player_r][player_c] == TILE_MONSTER){ return true; } return false; }
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