Maze Game

Hi. Im making a game where you can move a player around a maze. It works, i can move all around except it will move through the walls. I have some classes for a player and obstacle, i havent gotten them fully coded yet. Im only 14 and i know, this isnt well written code. I need to change some things and take out the few global variables. just trying to get it to work simply like this. thanks!

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include"stdafx.h"
#include <iostream>
#define WIDTH 16
#define HEIGHT 16
using namespace std;

int playerX = 1;
int playerY = 1;

class GAMEINIT
{
public:
	char currentMaze[WIDTH][HEIGHT];
	int update ();
};

int GAMEINIT::update ()
{

	 char obstacleICON = char(4);
	 const char maze1[HEIGHT][WIDTH] = {'#','#','#','#','#','#','#','#', '#','#','#','#','#','#','#','#',
									   '#',' ','#','#','#','#','#','#', '#','#','#','#','#','#','#','#',
									   '#',' ','#','#','#','#','#','#', '#','#','#','#','#','#','#','#',
									   '#',' ','#','#','#','#','#','#', '#','#','#','#','#','#','#','#',
									   '#',' ','#','#','#','#','#','#', '#','#','#','#','#','#','#','#',
									   '#',' ',' ',' ','#','#',' ',' ', ' ',' ','#',' ',' ',' ','#','#',
									   '#','#','#',' ','#','#',' ','#', '#',' ','#',' ','#',' ','#','#',
									   '#','#','#',' ','#','#',' ','#', '#',' ','#',' ','#',' ','#','#',
									   '#','#','#',' ',' ','#',' ','#', '#',' ',' ',' ','#',' ','#','#',
									   '#','#','#','#',' ','#',' ','#', '#','#','#','#','#',' ','#','#',
									   '#','#','#','#',' ',' ',' ','#', '#','#','#',' ',' ',' ','#','#',
									   '#','#','#','#','#','#','#','#', '#','#','#',' ','#','#','#','#',
									   '#','#','#','#',' ',' ',' ',' ', ' ',' ',' ',' ','#','#','#','#',
									   '#','#','#','#',' ',' ',' ',' ', ' ',' ','#','#','#','#','#','#',
									   '#','#','#','#',' ',' ',' ',' ', ' ',' ','#','#','#','#','#','#',
									   '#','#','#','#','#','#','#','#', '#','#','#','#','#','#','#','#'};

	
	
	for (int i = 0; i < WIDTH; i++)
	{
		for (int j = 0; j <HEIGHT; j++)
		{
			currentMaze[i][j] = maze1[i][j];
		}
	}
	for (int i = 0; i <WIDTH; i ++)
	{
		for (int j = 0; j <HEIGHT; j++)
		{
		currentMaze[playerY][playerX] = char(2);
		currentMaze[2][2] = char(4);
		}
	}
	
	for (int i = 0; i < WIDTH; i++)
	{
		cout <<endl;
		for (int j = 0; j < HEIGHT; j++)
		{
			cout <<currentMaze[i][j];
		}
	}
		
	if (currentMaze[playerY][playerX] == currentMaze[2][2])
		{
			
		}
	return 0;
}
class obstacle
{
public:
	int moveUp();
	int moveDown();
	obstacle(int obX, int obY) : oX(obX), oY(obY) { }

private:
	int oX;
	int oY;
};


int obstacle::moveUp()
{
	oX++;
	return oX;
}
int obstacle::moveDown()
{
	oY++;
	return oY;
}
class player : public GAMEINIT
{
public:
	player (int pHealth) : health (pHealth) { }
	int move ();

private: 
	int health;

};

int player::move () 
{

	char action;
	cout <<"'W' 'A' 'S' 'D' to move: ";
	cin >> action;

		
		
	switch (action)
	{
	case 'w':
		if (currentMaze[playerY-1][playerX] != '#')
		{
		playerY--;
		break;
		}
		else
		{
			break;
		}
	case 'a':
		if (currentMaze[playerY][playerX-1] != '#')
		{
		playerX--;
		break;
		}
		else
		{
			break;
		}
	case 's':
		if (currentMaze[playerY+1][playerX] != '#')
		{
		playerY++;
		break;
		}
		else
		{
			break;
		}
		
	case 'd':
		if (currentMaze[playerY][playerX+1] != '#')
		{
		playerX++;
		break;
		}
	
		break;
		
}

	
	return 0;
}

class enemy
{
public:
	int attack ();
	int getAttacked ();

private:
	int eX;
	int eY;
};
	

int main ()
{
	obstacle ob (14,4);
	player hero (100);
	GAMEINIT INITGAME;

	//****MAIN LOOP****
	for (;;)
	{
		INITGAME.update();
		hero.move();
		system("CLS");
	}

	char f;
	cin >>f;
	
}

Your program architecture is really messed up. I suggest you to rethink it, but if you want only a fix here is it:
- 183 INITGAME.update(); 
+ 183 hero.update();

The problem is thet your player class inherits from GAMEINIT, and therefore has it's own currentmaze variable. In your loop you are updating unrelated INITGAME variable when hero stays uninitialized.
Last edited on
thanks. what do you mean by program architecture? what could I work on to have a better coded program?
Well, for example there is no sense for player to derive from GAMEINIT.
Also in update() function you are copyiing array each step.
I don't think that obstacles should move, and so on.
I decided to have the player inherit from GAMEhINIT so the hero would have acess to the maze and stuff. the alternative would have been making the aray a pointer and passing it in and out of the functions? and yes i know about using header files and closing my classes away in different files.
Your player class can store current maze pointer as one of it members. It will allow it to say, quickly change current maze if it will gone to the next level. And more importanly it will allow several different players/monsters to share same map.
Remember: inheritance is one of the most strong relations in C++ and usually implies is-a relationship, namely you can expect it to be logical to pass player in every instance where GAMEINIT required.
oh makes sense. but isnt it not proper coding to have the maze a member of the player class when the maze is an object of the game initialization function?
It should not be there either. Better to have world class which will store all mazes, manage players and their movement between mazes and will have member initialization function.
so have a class that creates an instance of my player class and manages it?
SOmething like that. Or you can have world and player independed on each other and have only weak relation (world have pointers to all players in it and gives them current maze information)
Topic archived. No new replies allowed.