D&D Style Game

Pages: 12
I am given an assignment that I am to write a D&D Style Game using pointers and char arrays. The requirements are to have 3 files, 1) Character.h (which he gives the class, variables, and functions to be used), 2) Character.cpp (which the functions are defined), 3) main.cpp (which has the coding to execute the program). I am not grasping how I am to link the 3 files first of all, secondly I am not sure how what the functions are supposed to do. here is what I have thus far.

main.cpp has nothing but the basic template.
Character.cpp has the #include "Character.h" and all of the functions listed but nothing in any of them.
Character.h:
#include <iostream>
#include <string>
#include "Character.h"

class Character
{
private:
char m_sName[64];
int m_iClass, m_iAlignment, m_iHitPoints;
int m_iCharTraits[6];

public:
Character();
~Character();
Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr);
void getVariable();
void setVaraible();
void getName(char *name);
void setName(char *name);
void getClass(int& cl);
void setClass(int cl);
void getAlignment(int& al);
void setAlignment(int al);
void getHitPoints(int& hp);
void setHitPoints(int hp);
void getStrength(int *str);
void setStrength(int str);
void getDexterity(int *dex);
void setDexterity(int dex);
void getConstitution(int *cn);
void setConstitution(int cn);
void getIntelligence(int *itl);
void setIntelligence(int itl);
void getWisdom(int *wis);
void setWisdom(int wis);
void getCharisma(int *chr);
void setCharisma(int chr);
void printAll();
};
Character.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include "Character.h"

class Character
{
private:
	char m_sName[64];
	int m_iClass, m_iAlignment, m_iHitPoints;
	int m_iCharTraits[6];

public:
	Character();
	~Character();
	Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr);
	void getVariable();
	void setVaraible();
	void getName(char *name);
	void setName(char *name);
	void getClass(int& cl);
	void setClass(int cl);
	void getAlignment(int& al);
	void setAlignment(int al);
	void getHitPoints(int& hp);
	void setHitPoints(int hp);
	void getStrength(int *str);
	void setStrength(int str);
	void getDexterity(int *dex);
	void setDexterity(int dex);
	void getConstitution(int *cn);
	void setConstitution(int cn);
	void getIntelligence(int *itl);
	void setIntelligence(int itl);
	void getWisdom(int *wis);
	void setWisdom(int wis);
	void getCharisma(int *chr);
	void setCharisma(int chr);
	void printAll();
};
Character.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>
#include "Character.h"


	void Character::getVariable()
	{

	}

	void Character::setVaraible()
	{

	}

	void Character::getName(char *name)
	{

	}

	void Character::setName(char *name)
	{

	}
	
	void Character::getClass(int& cl)
	{

	}
	
	void Character::setClass(int cl)
	{

	}

	void Character::getAlignment(int& al)
	{

	}

	void Character::setAlignment(int al)
	{

	}

	void Character::getHitPoints(int& hp)
	{

	}

	void Character::setHitPoints(int hp)
	{

	}

	void Character::getStrength(int *str)
	{

	}

	void Character::setStrength(int str)
	{

	}

	void Character::getDexterity(int *dex)
	{

	}

	void Character::setDexterity(int dex)
	{

	}

	void Character::getConstitution(int *cn)
	{

	}

	void Character::setConstitution(int cn)
	{

	}

	void Character::getIntelligence(int *itl)
	{

	}

	void Character::setIntelligence(int itl)
	{

	}

	void Character::getWisdom(int *wis)
	{

	}

	void Character::setWisdom(int wis)
	{

	}

	void Character::getCharisma(int *chr)
	{

	}

	void Character::setCharisma(int chr)
	{

	}

	void Character::printAll()
	{

	}
main.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

using namespace std;

int main()
{

	return 0;
}
Hi,

Surely you know how to write those functions? They are pretty trivial.

If you don't, should I be asking if your slept through all your classes?

Try the tutorial at the top left of this page :+)

I am not grasping how I am to link the 3 files first of all


Just include the Character.h file in main.cpp. The compiler will do the rest.

Given your previous posts, I am sorry, but this Topic seems downright lazy.
Let me rephrase, (by no means am I looking for someone to do the work for me, as I will not learn that way.) I somewhat understand what the functions are supposed to do, I'm just way unfamiliar with pointers. I find them a bit confusing. I get that a pointer "points" to a reference of a variable in its memory location, I am just not sure how to implement them in this particular program. I have never used pointers, as I have only really did very basic, one file programs that have never required pointers. I switched from a community college to a university and they teach way differently. I'm just looking for an example or 2 on how to implement pointers across different files. My instructor told me not to include the Character.h in main.cpp. He said that the compiler will know they go together as long as they are in the same project folder.
Hi,

Well that is a much better question :+) We often get people wanting us to do all their homework, and this looked like another one of those.

Here is a tutorial on pointers:

http://www.cplusplus.com/doc/tutorial/pointers/


There is also reference material and articles there too. Also read up about classes, you need to write a constructor with a member initialisation list.

http://www.cplusplus.com/doc/tutorial/classes/


About two thirds of the way down there is a section "Member initialization in constructors"

My instructor told me not to include the Character.h in main.cpp. He said that the compiler will know they go together as long as they are in the same project folder.


Well that is wrong. You have to #include any header file for a class that you want to use.

Regards :+)
My instructor told me not to include the Character.h in main.cpp. He said that the compiler will know they go together as long as they are in the same project folder.

That makes no sense at all. Are you sure he wasn't telling you not to include Character.cpp in main.cpp?
Thats what I thought as well. I figured there has to be an instance of Character.h in main.cpp in order for the program to know what to look for. Just didnt make sense to me. Let me give those tutorials a whirl and see what I can come up with. I'll post again when I have something more concrete.
"instance" is not the right word to use here. The point is that, when compiling main.cpp, the compiler can only understand understand what Character means if the symbol has already been defined in that translation unit. We include that definition, by #include -ing the header file that contains the definition.

Similarly, when compiling Character.cpp, the definition of Character will also need to be part of that translation unit - which means we also include the header file in Character.cpp.

This is the whole reason we have header files - so that we can make sure every translation unit can have the definitions they need, without actually copying and pasting the definitions into every .cpp file.
Last edited on
ok.... so...... Since Character.h has the functions initialized but not defined, and Character.cpp has #include "Character.h" and defines the functions (or will when I write that portion of code, if I #include "Character.h" in main.cpp, then I will have access to the function definitions from Character.cpp in main.cpp? Is that what you are telling me?
Not quite - and again, you're not using the right terminology. Including Character.h will give you the definition of the class, which includes the declarations of the functions. That's all the compiler needs to compile code within main.cpp or Character.cpp.

It's the linker that allows the code in main.cpp to actually call the functions defined in Character.cpp. You need to ensure your linker is linking the object code created from both Character.cpp and main.cpp, when creating the executable.

How you do that depends on what compiler and/or IDE you're using.

To put it simply:

- Header files enable the compiler to compile a single source file into object code
- Correct compiler/IDE settings enable the linker to connect a function call in one object file, to the function definition in another object file.
Last edited on
Got you now. Sorry for the incorrect terms. I'm trying to relearn a bunch of stuff plus new stuff all over again. One of my problems is that my instructor has developed a program of his own to test ours. Of course, I do not know what his program looks like. There are no cin >> or cout << statements. Everything that will be displayed is coming from his hard coded program that runs through ours to check if we hit all the marks. I just have a difficult time visualizing this program without actually having those statements to reference while I'm writing the code. Should I just write the code with those statements and then comment them out later when the program is running properly?
Sorry, I'm not clear. Is he expecting your program to write certain information out? Or is his magical program going to somehow do that?

Actually, since he's clearly specified the Character interface, I suspect he's planning to take your Character class, and use it with his own test runner program. In other words, he won't use your main function at all. That's just my guess, though.
Last edited on
Here, I'll post the assignment exactly as he has it, because I'm still not 100% clear as to what he wants me to actually write.

Statement of Work
Programming Assignment 1

1.0 Overview
In role-playing adventure games such as Dungeons and Dragons each person creates a player character which they play in the game. In this program a C++ class will be created which can be used to define a player character according to the rules of Dungeons and Dragons. This programming assignment requires an understanding of pointers, and C++ functions and classes. Simply put, a class is a data structure which contains data and the functions to work on that data. A pointer is a variable that holds the memory address of another variable, data structure, or class. This program will provide the opportunity to explore pointers, functions, and classes and learn how to use them.

2.0 Requirements
The student shall define, develop, document, prototype, test, and modify as required the software system.
2.1 This software system shall define a player character class called Character (file names shall be Character.h and Character.cpp) which contains all information to define and maintain a player character for a Dungeons and Dragons style role playing adventure game.
2.1.1 The Character class shall contain private variables as described below:
2.1.1.1 A character array, called m_sName capable of holding strings of up to 64 characters.
2.1.1.2 Three ints called m_iClass, m_iAlignment, and m_iHitPoints.
2.1.1.3 An array of six integers called m_iCharTraits used to represent the character qualities of: Strength, Dexterity, Constitution, Intelligence, Wisdom, and Charisma
2.1.2 The Character class shall contain public functions as described below:
2.1.2.1 Character(), ~Character() A default constructor and destructor.
2.1.2.2 Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr) A parameterized constructor which shall take as arguments a character array giving the name and integer arguments specifying the class, alignment, hit points, strength, dexterity, constitution, intelligence, wisdom, and charisma of the character.
2.1.2.3 Get/Set functions There will be a getVariable() and a setVaraible() designed to return the value stored in or set the value stored in a character's data. Specifically these functions shall be prototyped as:
2.1.2.3.1 void getName(char *name), void setName(char *name) - Get and set the player name.
2.1.2.3.2 void getClass(int& cl), void setClass(int cl) - Get and set the player class. The get function shall be a reference function.
2.1.2.3.3 void getAlignment(int& al), void setAlignment(int al) - Get and set the player alignment value. The get function shall be a reference function.
2.1.2.3.4 void getHitPoints(int& hp), void setHitPoints(int hp) - Get and set the player hit points. The get function shall be a reference function.
2.1.2.3.5 Six pairs of functions to get and set the various character traits stored in the m_iCharTraits array. All of the get functions shall be pointer functions. The functions are:
void getStrength(int *str), void setStrength(int str) - Get and set the player Strength score (m_iCharTraits[0]).
void getDexterity(int *dex), void setDexterity(int dex) - Get and set the player Dexterity score (m_iCharTraits[1]).
void getConstitution(int *cn), void setConstitution(int cn) - Get and set the player Constitution score (m_iCharTraits[2]).
void getIntelligence(int *itl), void setIntelligence(int itl) - Get and set the player Intelligence score (m_iCharTraits[3]).
void getWisdom(int *wis), void setWisdom(int wis) - Get and set the player Wisdom score (m_iCharTraits[4]).
void getCharisma(int *chr), void setCharisma(int chr) - Get and set the player Charisma score (m_iCharTraits[5]).
2.1.2.3.6 void printAll() - Print all information on this character. This includes, Name, Class, Alignment, Hit points, and all 6 character trait values.
2.2 The class file and its' associated header file must be capable of being compiled and linked in with the instructor's driver program (which will contain main) for testing. Do not turn in your source file containing main. It will not be used for testing.
3.0 Deliverables
These products shall be delivered to the instructor electronically via e-mail as specified below.
3.1 Sprint Report -- The student shall provide filled out Sprint Report form for instructor approval NLT (Not Later Than) Thursday, February 4.
3.2 Program source files -- The student shall provide fully tested electronic copies of the .cpp and .h files. These files must be submitted to the instructor via e-mail. The files shall be delivered NLT Thursday, February 4.
4.0 Period of Performance
The period of performance of this assignment is 21 days from the date of assignment. No deliverables be accepted after midnight of the DDD date posted in the Course Syllabus.
A note on program grading: The instructor will test your program by compiling your Character.cpp and Character.h files with a driver program containing a main() function. This driver will exhaustively test your source code.


Nevermind the due dates. It's late, but I cannot continue with my next project until this one works as it should. All of the projects in this class stack on each other. So, if one does not work, I'm doomed for the rest for sure!
That looks pretty clear and explicit. Which bits aren't you clear about?

The class file and its' associated header file must be capable of being compiled and linked in with the instructor's driver program (which will contain main) for testing. Do not turn in your source file containing main. It will not be used for testing.

Called it :)
Last edited on
Like I said, in my main, should I have cin and cout statements for my own personal testing?
That would be the most obvious way to fulfill the instruction that you must test your software.

The instructions tell you not to submit your main.cpp to your instructor, so it doesn't technically matter what you put in there.

Ok... so this is what I got. Thoughts? And also, I am trying to figure out how to write that for loop at the bottom to fill the array for m_sName.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <string>
#include "Character.h"

using namespace std;

class Character 
{
private:
	char m_sName[64];
	int m_iClass, m_iAlignment, m_iHitPoints;
	int m_iCharTraits[6];

public:
	Character(char name[], int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr)
	{
		for(int i = 0; i < 64; i++)
		{
			m_sName[i] = name[i];
		}
		
		m_iClass = cl;
		m_iAlignment = al;
		m_iHitPoints = hp;
		m_iCharTraits[0] = str;
		m_iCharTraits[1] = dex;
		m_iCharTraits[0] = con;
		m_iCharTraits[0] = itl;
		m_iCharTraits[0] = wis;
		m_iCharTraits[0] = chr;
	}
	
	char getName(char m_sName)
	{
		return  m_sName;
	}
	
	void setName(char name[]) 
	{
		for(int i = 0; i < 64; i++)
		{
			m_sName[i] = name[i];
		}
	}
	
	int getClass()
	{
		return m_iClass;
	}
	
	void setClass(int cl)
	{
		m_iClass = cl;
	}
	
	int getAlignment()
	{
		return m_iAlignment;
	}
	
	void setAlignment(int al) 
	{
		m_iAlignment = al;
	}
	
	int getHitPoints()
	{
		 return m_iHitPoints;
	}
	
	void setHitPoints(int hp)
	{
		m_iHitPoints = hp;
	}
	
	int getStrength()
	{
		return m_iCharTraits[0];
	}
	
	void setStrength(int str)
	{
		m_iCharTraits[0] = str;
	}
	
	int getDexterity(int *dex)
	{
		return m_iCharTraits[1];
	}
	
	void setDexterity(int dex) 
	{
		m_iCharTraits[1] = dex;
	}
	
	int getConstitution(int *cn)
	{
		return m_iCharTraits[2];
	}
	
	void setConstitution(int cn) 
	{
		m_iCharTraits[2] = cn;
	}
	
	int getIntelligence()
	{
		return m_iCharTraits[3];
	}
	
	void setIntelligence(int itl) 
	{
		m_iCharTraits[3] = itl;
	}
	
	int getWisdom()
	{
		return m_iCharTraits[4];
	}
	
	void setWisdom(int wis)
	{
		m_iCharTraits[4] = wis;
	}
	
	int getCharisma()
	{
		return m_iCharTraits[5];
	}
	
	void setCharisma(int chr) 
	{
		m_iCharTraits[5] = chr;
	}
	
	void printAll() 
	{
		cout << "Name: ";
		for(int i = 0; i < m_sName; i++)
		{
			cout << m_sName[i];
		}
		cout << "Class: " << m_iClass << endl;
		cout << "Alignment: " << m_iAlignment << endl;
		cout << "Hit Points: " << m_iHitPoints << endl;
		cout << "Strength: " << m_iCharTraits[0] << endl;
		cout << "Dexterity: " << m_iCharTraits[1] << endl;
		cout << "Constitution: " <<  m_iCharTraits[2] << endl;
		cout << "Intelligence: " << m_iCharTraits[3] << endl;
		cout << "Wisdom: " << m_iCharTraits[4] << endl;
		cout << "Charisma: " << m_iCharTraits[5] << endl;
	}
};
Pages: 12