Question
I need assistance writing these functions. I have everything else in place but these are throwing me off. 2.1.2.2.2 bool LoadGame(char *fileName) - This function
I need assistance writing these functions. I have everything else in place but these are throwing me off.
2.1.2.2.2 bool LoadGame(char *fileName) - This function shall take a single argument which is a character array giving the name of the graph data file. It shall open the file, read all data, and build the graph.
2.1.2.3.2 void setLink(int roomIdx, int linkIdx, char dCode) - This utility function takes an index of a "link from" Room and a "link to" Room and sets the appropriate code in the AdjMatrix to define a link and its type (North, South, East, West, Up or Down) in the adjacency matrix. 2.1.2.3.3 void describeRoom(int roomIdx) - This utility function takes an index of a Room in the m_Rms array and prints a complete description of the room, its contents and its exits. See details below.
The rest of the instructions are posted below just so you know what my instructor is looking for. I will also post the code I do have below that.
1.0 Overview In the popular role playing game of Dungeons and Dragons players move from room to room in a dungeon where they encounter creatures, treasure, and various obstacles. The underlying functionality of such a computer game lies in the gaming engine. This Statement of Work presents the requirements for a "Scenario Mapping and Player Movement" class for a gaming engine. 2.0 Requirements The student shall define, develop, document, prototype, test, and modify as required the software system. 2.1 Functional Requirements 2.1.1 The software shall be capable of representing the layout for a role playing game scenario as a graph implemented as an adjacency matrix. The scenario will typically be an inside location such as a dungeon or castle divided into a number of rooms. For this implementation it will be assumed that the number of rooms will be 20. 2.1.2 The scenario graph shall be implemented as a class called GameGraph (defined in files GameGraph.h and GameGraph.cpp). 2.1.2.1 The GameGraph class shall contain the following private variables. 2.1.2.1.1 An ifstream object called m_InFile which shall reference the data file defining the game scenario. 2.1.2.1.2 A 2D array of chars called m_cAdjMatrix which shall be a 20x20 matrix used to define the links between rooms in the scenario. Data stored in the adjacency matrix will not only indicate where there are doors from the current room to other rooms, i.e links, but also will define the direction of the link. In other words the adjacency matrix will be able to store definitions of up to 6 doors and stairways in a room. This means a room can have, up to 4 doors located on the North, South, East, or West walls, as well as stairways going up or down. 2.1.2.1.3 An array called m_Rms of 20 Room structures as defined in 2.1.3 below. 2.1.2.1.4 An int called m_iLocation which shall define the index into the m_Rms array giving the current location of the gaming party . 10/30/2017 Programming Assignment 4 http://www .cs.uah.edu/~rcoleman/CS221/Section1/ProgAssign/cs221Prog4.html 2/9 2.1.2.2 The GameGraph class shall contain the following public functions. 2.1.2.2.1 GameGraph() and ~GameGraph() - Default constructor and destructor. 2.1.2.2.2 bool LoadGame(char *fileName) - This function shall take a single argument which is a character array giving the name of the graph data file. It shall open the file, read all data, and build the graph. 2.1.2.2.3 bool doCommand(char *cmd) - This function shall take a single argument which is a character array giving a command string the user has input from the keyboard. It shall parse and execute the command. This function always returns false unless the command is QUIT. (Details are given below) 2.1.2.2.4 void PrintAll() - This function shall print all information in each Room structure and all the links in the graph. This function is to be used primarily during development and testing to make sure all data have been read and stored correctly . 2.1.2.3 The GameGraph class shall contain the following private functions. 2.1.2.3.1 void getNextLine(char *line, int lineLen) - This utility function reads lines from the graph definition data file. It will be provided by the instructor. 2.1.2.3.2 void setLink(int roomIdx, int linkIdx, char dCode) - This utility function takes an index of a "link from" Room and a "link to" Room and sets the appropriate code in the AdjMatrix to define a link and its type (North, South, East, West, Up or Down) in the adjacency matrix. 2.1.2.3.3 void describeRoom(int roomIdx) - This utility function takes an index of a Room in the m_Rms array and prints a complete description of the room, its contents and its exits. See details below. 2.1.3 Rooms in the scenario shall be represented by a Room structure (defined in a GameGraph.h file). The Room structure shall contain the following fields: 2.1.3.1 A character array called m_sRoomName which shall be capable of storing room names of up to 63 characters in length. For example: "Banquet Hall". 2.1.3.2 A character array called m_sRoomDesc which shall be capable of storing room descriptions of up to 127 characters in length. For example: "There is a long oak table filled with plates of food. Wood benches line the walls. Burning torches are mounted on the walls." 2.1.3.3 A character array called m_sItemName which shall be capable of storing the name of a single item of up to 31 characters in length. For example: "Golden sword". 2.1.3.4 A character array called m_sCreatureName which shall be capable of storing the name of a single creature of up to 63 characters in length. For example: "Mountain troll". 2.1.4 Data defining a gaming scenario shall be read from a data file named gamelayout.txt. A sample data file for development and testing will be provided. 2.1.5 A command line interface shall be included in the software which meets the following criteria: 2.1.5.1 When the game is started the user shall be placed in the room which was first defined in the data file. 10/30/2017 Programming Assignment 4 http://www .cs.uah.edu/~rcoleman/CS221/Section1/ProgAssign/cs221Prog4.html 3/9 2.1.5.2 When a user enters a room a description of the room shall be given which includes the name of the room, a brief description of the room, the item found in the room, the creature found in the room, and the number of doors and stairways and where they are located. This information will be presented in a narrative style. For example: You are in the Banquet Hall. T here is a long oak table filled with plates of food. Wood benches line the walls. Burning torches are mounted on the walls. T here is a golden sword in the room. T here is a Mountain troll in the room. T here is a door in the North wall. T here is a door in the South wall. T here is a door in the East wall. T here is a stairway going down. 2.1.5.3 After printing the description of the room as described in 2.1.5.2 the user shall be prompted to enter a command. For example: What do you want to do? 2.1.5.4 The user shall be allowed to enter commands from the keyboard. The command is to be parsed and executed. Commands must meet the following criteria: 2.1.5.4.1 Valid first words of the command shall be: GO, TAKE, FIGHT and QUIT. 2.1.5.4.2 If the command is GO then valid second words of the command shall be: NORTH, SOUTH, EAST, WEST, UP , or DOWN. Indicating a desire to go through the north, south, east, or west doors, or up or down a stairway . 2.1.5.4.3 If the command is TAKE or FIGHT print a message to inform the user that this command has not been implemented yet. 2.1.5.4.4 If the command is QUIT exit the program. 2.1.5.4.5 If any other command is issued or a direction other than those listed in section 2.1.5.4.1 is given then print a message to inform the user that this command is not understood. 2.1.6 Movement from one room to another, printing of room descriptions, inputing and parsing of commands shall continue until the user enters the command QUIT. 2.1.7 This program must be capable of being tested using a text file of commands and I/O redirection, therefore no other commands requiring input from the keyboard will be allowed except for the single cin.getline(line, 128, ' '); function call in the "player movement" loop. (Note: You must use this form of the cin stream reference in order to get a full line including spaces. The ' ' is the character to terminate the input. In this case the Enter key .
GameMain.cpp
#include
#include
#include
#include "GameGraph.h"
#define DataFile "gamelayout.txt"
void makeUpper(char *line);
int main(int argc, char **argv)
{
GameGraph *game;
bool done = false;
char command[64];
game = new GameGraph();
cout << "Welcome to Program Assignment 4 for CS 221. ";
// Add your name in the line below
cout << "Programming by: ************ ";
cout << "This is a Dungeons and Dragons style role ";
cout << "playing game. At the prompt enter enter your commands. ";
game->LoadGame(DataFile); // Load the game
while(!done)
{
cout << " What do you want to do? ";
cin.getline(command, 64, ' ');
makeUpper(command);
if((game->doCommand(command))) // If doCommand returns true end game
done = true;
}
return 0;
}
//--------------------------------------
// makeUpper()
// Converts input line to all upper case
//--------------------------------------
void makeUpper(char *line)
{
char *temp;
temp = line;
while(*temp != '\0')
{
*temp = toupper(*temp);
temp++;
}
}
GameGraph.h
#ifndef GAMEGRAPH_H
#define GAMEGRAPH_H
#include
#include
#include
#include
using namespace std;
#define NUMROOMS 20
struct Room
{
char m_sRoomName[64];
char m_sRoomDesc[128];
char m_sItemName[32];
char m_sCreatureName[64];
};
class GameGraph
{
private:
ifstream m_InFile; // File to read from
char m_cAdjMatrix[NUMROOMS][NUMROOMS];
Room m_Rms[NUMROOMS];
int m_iLocation; // Index of current room location
public:
GameGraph(); // Class constructor
~GameGraph(); // Class destructor
bool LoadGame(char *filename); // Read game file, build scenario
// and describe first room.
bool doCommand(char *cmd); // Execute a command
void PrintAll();
private:
void getNextLine(char *line, int lineLen);
void setLink(int roomIdx, int linkIdx, char dCode);
void describeRoom(int roomIdx);
};
#endif
GameGraph.cpp
#include
#include
#include
#include
#include "GameGraph.h"
using namespace std;
//Class Constructor
GameGraph::GameGraph()
{
for(int i = 0; i < NUMROOMS; i++)
for(int j = 0; j < NUMROOMS; j++)
m_cAdjMatrix[i][j] = 0;
return;
}
//Class Destructor
GameGraph::~GameGraph()
{
return;
}
//bool LoadGame
bool GameGraph::LoadGame(char *filename)
{
char line[128];
int link;
bool done;
m_InFile.open(filename, ifstream::in);
if(!m_InFile.is_open())
{
// m_InFile.is_open() returns false if the file could not be found or
// if for some other reason the open failed.
return false;
}
done = false;
for(int i=0; i<20; i++)
{
// Read room name
getNextLine(line, 128);
/* --- Copy room name into data structure i --- */
// Read room description
getNextLine(line, 128);
/* --- Copy room description into data structure i --- */
// Read room item
getNextLine(line, 128);
/* --- Copy room item name into data structure i --- */
// Read room creature
getNextLine(line, 128);
/* --- Copy room creature name into data structure i --- */
// Read North door
getNextLine(line, 128);
link = atoi(line); // Convert to room index
// Call function to set link in adjacency matrix
// Read South door
getNextLine(line, 128);
link = atoi(line); // Convert to room index
// Call function to set link in adjacency matrix
// Read East door
getNextLine(line, 128);
link = atoi(line); // Convert to room index
// Call function to set link in adjacency matrix
// Read West door
getNextLine(line, 128);
link = atoi(line); // Convert to room index
// Call function to set link in adjacency matrix
// Read up stairway
getNextLine(line, 128);
link = atoi(line); // Convert to room index
// Call function to set link in adjacency matrix
// Read down stairway
getNextLine(line, 128);
link = atoi(line); // Convert to room index
// Call function to set link in adjacency matrix
}
return true;
}
//bool doCommand
bool GameGraph::doCommand(char *cmd)
{
char cmd1[128];
char cmd2[128];
sscanf(cmd, "%s", cmd1);
if(strcmp("QUIT", cmd1) == 0)
{
exit(EXIT_SUCCESS);
return true; //Flag quit playing
}
else if((strcmp("TAKE", cmd1) ==0) || (strcmp("FIGHT", cmd1) == 0))
{
cout << "This command has not been implemented yet.";
return false; //Flag keep playing
}
else
{
sscanf(cmd, "%s%s", cmd1, cmd2);
if(strcmp(cmd1, "GO") == 0)
{
if(strcmp("NORTH", cmd2) == 0)
{
for(int i = 0; i < 20; i++)
if(m_cAdjMatrix[m_iLocation][i] == 'N')
{
m_iLocation = i;
break;
}
}
if(strcmp("SOUTH", cmd2) == 0)
{
for(int i = 0; i < 20; i++)
if(m_cAdjMatrix[m_iLocation][i] == 'S')
{
m_iLocation = i;
break;
}
}
if(strcmp("EAST", cmd2) == 0)
{
for(int i = 0; i < 20; i++)
if(m_cAdjMatrix[m_iLocation][i] == 'E')
{
m_iLocation = i;
break;
}
}
if(strcmp("WEST", cmd2) == 0)
{
for(int i = 0; i < 20; i++)
if(m_cAdjMatrix[m_iLocation][i] == 'W')
{
m_iLocation = i;
break;
}
}
cout << endl;
describeRoom(m_iLocation);
}
else
{
cout << "The Command is not understood by this program.";
}
return false; //Flag keep playing
}
}
//void PrintAll
void GameGraph::PrintAll()
{
int i, j;
cout << " GRAPH DATA ";
cout << "------------------------------------------------------------ ";
cout << "Index Data String Links to ";
cout << "------------------------------------------------------------ ";
for(i=0; i < NUMROOMS; i++)
{
cout << " " << m_Rms[i].m_sRoomName << " " <<
m_Rms[i].m_sRoomDesc << "\t";
for(j = 0; j < NUMROOMS; j++)
{
if(m_cAdjMatrix[i][j] == 1)
cout << m_Rms[j].m_sRoomName << " ";
}
}
cout << "------------------------------------------------------------ ";
cout << " Adjacency Matrix: ";
cout << " 0 1 2 3 4 5 6 7 ";
cout << " +---------------+ ";
for(i=0; i { cout << i << "|"; for(j=0; j { cout << m_cAdjMatrix[i][j] << "|"; } cout << " +---------------+ "; } } //PRIVATE Void getNextLine void GameGraph::getNextLine(char *line, int lineLen) { int done = false; while(!done) { m_InFile.getline(line, lineLen); // Note: m_InFile is an ifstream object that is part of the GameGraph class if(m_InFile.good()) // If a line was successfully read { if(strlen(line) == 0) // Skip any blank lines continue; else if(line[0] == '#') // Skip any comment lines continue; else done = true; // Got a valid data line so return with this line } else { strcpy(line, ""); return; } } // end while } //PRIVATE Void setLink void GameGraph::setLink(int roomIdx, int linkIdx, char dCode) { } //PRIVATE Void describeRoom void GameGraph::describeRoom(int roomIdx) { }
Step 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