Coding Assignment

Added on - 20 Sep 2019

  • 4

    pages

  • 1383

    words

  • 143

    views

  • 0

    downloads

Showing pages 1 to 2 of 4 pages
#include "Maze.h"static int mazeStructure[500][500] = { 0 };static int numTotalKeys = 0;static int numKeysHeld = 0;static int maxRow = 0;static int maxCol = 0;static int playerRow = -1;static int playerCol = -1;static int exitRow = -1;static int exitCol = -1;static int readMazeRan = 0;static int numSteps = 0;int getCurrentKeys(){return numKeysHeld;}int getTotalKeys(){return numTotalKeys;}int getNumSteps(){return numSteps;}void getCurrentPosition(int * arr){arr[0] = playerRow;arr[1] = playerCol;}int abs(int x){if (x < 0) return 0-x;return x;}int dangerous(int x, int y){return x < 0 || y < 0 || x >= 500 || y >= 500;}//fills the maze based on the file givenvoid readMaze(char* fileName){if(readMazeRan){printf("ERROR: Multiple calls to readMaze\n");return;}readMazeRan = 1;//stuff too complicated to cover in classFILE *inputFile = fopen(fileName, "r");char currentChar;int currentRow = 0;int currentCol = 0;while((currentChar = fgetc(inputFile)) != EOF){if(currentChar == '\n'){if(currentCol > maxCol){
maxCol = currentCol;}currentRow += 1;currentCol = 0;} else {//3 is a keyif(currentChar == '3'){numTotalKeys++;//2 is a player, but only one is allowed}else if(currentChar == '2'){if(playerRow != -1){printf("ERROR: You can't have more than one player! The instance at(%d, %d) was ignored in favor of the instance at (%d, %d).\n", currentRow,currentCol, playerRow, playerCol);currentChar = '1';}else{playerRow = currentRow;playerCol = currentCol;}//4 is an exit, but only one is allowed}else if(currentChar == '4'){if(exitRow != -1){printf("ERROR: You can't have more than one exit! The instance at(%d, %d) was ignored in favor of the instance at (%d, %d).\n", currentRow,currentCol, exitRow, exitCol);currentChar = '1';}else{exitRow = currentRow;exitCol = currentCol;}}if(currentChar >= '0' && currentChar <= '4'){mazeStructure[currentRow][currentCol] = currentChar - 48;}else{printf("ERROR: Unrecognized character at (%d, %d)\n", currentRow,currentCol);}currentCol+=1;}}if(playerRow == -1){printf("ERROR: No player object was found\n");}if(exitRow == -1){printf("ERROR: No exit object was found\n");}maxRow = currentRow;}void printMaze(){
desklib-logo
You’re reading a preview
card-image

To View Complete Document

Become a Desklib Library Member.
Subscribe to our plans

Download This Document