How do I access a function from another function?

Hi Im making a text based game in C++ console. I have a class for player, a class for monster a level class. I can add the player to the level and I can add the monster to the level but when the player takes the XY position in the level array I want the monster to disappear but I dont know how to do this. Here is my code

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
#include "stdafx.h"
#include <iostream>
#include <ctime>

using namespace std;

const short player = 1; // Player is represented by the number 1
const short monster = 2; // Enemy is represented by the number 2

short randomNum(){
	return rand()%10; 
}

class Player{
private:
	short posX,posY;
public:
	void randomPos(){
		//Put the player in a random place in the level.
		posX = randomNum();
		posY = randomNum();
	}
	void message(){
	cout << "Cannot move in this direction " << endl;
	cout << endl;
	}
	void movePlayer(char choice){
	switch (choice){
		case 'w':
			posX--;
			if (posX < 0){
				posX = 0;
					message();
			}
			break;
		case 's':
			posX++;
			if (posX > 9)
				posX = 9;
					message();
			break;
		case 'a':
			posY--;
			if (posY < 0)
				posY = 0;
					message();
			break;
		case 'd':
			posY++;
			if (posY > 9)
				posY = 9;
					message();
			break;
	}
}
	short getX(){return posX;}
	short getY(){return posY;}
};
class Monster: public Player{
private: 
	short mPosX,mPosY;
public:
	void mRandomPos(){
		//Put the monster in a random place in the level.
		mPosX = randomNum();
		mPosY = randomNum();
	}
	void moveMonster(){

	}
	short getMX(){return mPosX;}
	short getMY(){return mPosY;}
};
class Level{
private:
	short myLevel[10][10];
public:
	void setArray(){
		//set the values in the array to 0.
	for (int i = 0; i < 10; i++)
		for (int j = 0; j < 10; j++)
			myLevel[i][j] = 0;
	}
	void playerPos(int X,int Y, bool newPos){
		if (!newPos){
			myLevel[X][Y] = 0;
		}
		if (newPos){
			if (myLevel[X][Y] == 2)
			{
			cout << "You Killed Monster" << endl;
//At this point I want the XY coordinates of the monster object to 0. or stop
calling the function that is adding the monster in the gameLoop.
			cout << endl;
			}
			myLevel[X][Y] = player;
		}
	}
	void monsterPos(int X, int Y){
	myLevel [X][Y] = monster;
	}
	void print(){
	for (int i = 0; i < 10; i++)
		for (int j = 0; j < 10; j++)
			cout << myLevel [i][j] << "\t";
	}
};

void gameLoop(Player p, Monster m, Level l){
	bool loop = false;
	char choice;

		while (!loop)
		{
		cout << "Enter Direction ";
		cin >> choice;
		l.playerPos(p.getX(),p.getY(),false);
		p.movePlayer(choice);
		l.playerPos(p.getX(),p.getY(),true);

		l.monsterPos(m.getMX(),m.getMY()); //This keeps on looping and
adding the monster to the level. I need to set this value to 0 when the player
is in the array containing the monster. 

		l.print();
		}
}

int _tmain(int argc, _TCHAR* argv[])
{
	srand(time(NULL));

	Player player;
	Monster monster;

	player.randomPos();
	monster.mRandomPos();
	Level level;
	level.setArray();

	gameLoop(player, monster, level);

	system ("PAUSE");
	return 0;
}


Can someone please help me out with this.
Last edited on
Anyone?
closed account (D80DSL3A)
I'm having trouble understanding the problem.
Best I can get is from lines 121-123:
1
2
3
l.monsterPos(m.getMX(),m.getMY()); //This keeps on looping and
adding the monster to the level. I need to set this value to 0 when the player
is in the array containing the monster.

Is "this value" = monsterPos(m.getMX(),m.getMY()) ?
As in monsterPos(m.getMX(),m.getMY()) = 0; when the player
is in the array containing the monster?

What is the test for determining if "the player
is in the array containing the monster"?

"Honey, I'm gonna need my chest waders for this one!"
Last edited on
Yes that is what I want to do. At line 89,

1
2
3
4
5
6
7
8
if (myLevel[X][Y] == 2)
			{
			cout << "You Killed Monster" << endl;
//At this point I want the XY coordinates of the monster object to 0. or stop
calling the function that is adding the monster in the gameLoop.
			cout << endl;
			}
			myLevel[X][Y] = player;


If the array contains the number 2 which represents the monster then set its value to 0 but I can set it to 0 because in the gameLoop function keeps on getting the position of the monster and setting this to 2 again. I need to set the position of the monster in the monster class to 0. I tried making a constructor but it gives me this error

Member function may not be declared outside of class.

This is driving me crazy, someone please help =(
Topic archived. No new replies allowed.