Simple Game

Any thoughts or comments about this simple game?

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
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

struct hero {
	unsigned char x;
	unsigned char y;
};

struct enemy {
	unsigned char x;
	unsigned char y;
};

int random(int n) {
	return rand() % n;
}

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	cout << "Welcome to Dungeon Crawl. You have been thrown down here and must reach the exit which is marked by an X. Watch out for traps and enemies. " << endl;
	bool hero_win = false;
	int l, m, o;
	
	srand(time(NULL));
	
	unsigned char maze[10][10] = {
		{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
		{1, 0, 0, 0, 0, 0, 0, 3, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
		{1, 0, 0, 0, 0, 0, 0, 0, 3, 1}, 
		{1, 3, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 3, 0, 0, 1},
		{1, 0, 3, 0, 0, 0, 0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0, 0, 0, 4, 1},
		{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
	};
	
	hero logan;
	logan.x = 1;
	logan.y = 1;
	
	enemy joe;
	joe.x = 6;
	joe.y = 2;
	
	enemy brian; 
	brian.x = 2;
	brian.y = 5;
	
	enemy liam;
	liam.x = 4;
	liam.y = 8;
	
	char key = 0;
	while(key != 'q') {
		if(hero_win = true) {
				for(int y = 0; y < 10; y ++) {
					cout << endl;
					for(int x = 0; x < 10; x++) {
						if(x == logan.x && y == logan.y) {
							cout << (unsigned char)'P';
						} else if(x == joe.x && y == joe.y) {
							cout << (unsigned char)'E';
						} else if(x == brian.x && y == brian.y) {
							cout << (unsigned char)'E';
						} else if(x == liam.x && y == liam.y) {
							cout << (unsigned char)'E';
						} else if(maze[y][x] == 1) {
							cout << (unsigned char)'#';
						} else if(maze[y][x] == 0) {
							cout << (unsigned char)'.';
						} else if(maze[y][x] == 3) {
							cout << (unsigned char)',';
						} else if(maze[y][x] == 4) {
							cout << (unsigned char)'X';
						} 
						hero_win = false;
					}
				}
			}
			
		cout << endl;
		
		cout << "To move upwards press 'W', left 'A', down 'S', right 'D': ";
		cin >> key;
		
		if(key == 's' && logan.y < 9 && maze[logan.y + 1][logan.x] != 1) {
			logan.y++; system("cls");
		}
		if(key == 'w' && logan.y > 0 && maze[logan.y - 1][logan.x] != 1) {
			logan.y--; system("cls");
		}
		if(key == 'a' && logan.x > 0 && maze[logan.y][logan.x - 1] != 1) {
			logan.x--; system("cls");
		}
		if(key == 'd' && logan.x < 9 && maze[logan.y][logan.x + 1] != 1) {
			logan.x++; system("cls");
		}
		if(maze[logan.y][logan.x] == 3) {
			cout << "You have run into a trap. GAME OVER." << endl;
			system("PAUSE");
			exit(1);
		}
		if (maze[logan.y] == maze[joe.y] && maze[logan.x] == maze[joe.x]) {
			cout << "You have been defeated by an enemy. GAME OVER." << endl;
			system("PAUSE");
			exit(1);
		}
		if (maze[logan.y] == maze[brian.y] && maze[logan.x] == maze[brian.x]) {
			cout << "You have been defeated by an enemy. GAME OVER." << endl;
			system("PAUSE");
			exit(1);
		}
		if (maze[logan.y] == maze[liam.y] && maze[logan.x] == maze[liam.x]) {
			cout << "You have been defeated by an enemy. GAME OVER." << endl;
			system("PAUSE");
			exit(1);
		}
		if(maze[logan.y][logan.x] == 4) {
			cout << "You win the game! You have discovered the treasure. Good job! " << endl;
			system("PAUSE");
			exit(1);
		}
		l = random(4) + 1;
		
		if(l == 1 && maze[joe.y + 1][joe.x] != 1 && maze[joe.y + 1][joe.x] != 3) {
			joe.y++; system("cls");
		}
		if(l == 2 && maze[joe.y - 1][joe.x] != 1 && maze[joe.y - 1][joe.x] != 3) {
			joe.y--; system("cls");
		}
		if(l == 3 && maze[joe.y][joe.x - 1] != 1 && maze[joe.y][joe.x - 1] != 3) {
			joe.x--; system("cls");
		}
		if(l == 4 && maze[joe.y][joe.x + 1] != 1 && maze[joe.y][joe.x + 1] != 3) {
			joe.x++; system("cls");
		}
		
		m = random(4) + 1;
		
		if(m == 1 && maze[brian.y + 1][brian.x] != 1 && maze[brian.y + 1][brian.x] != 3) {
			brian.y++; system("cls");
		}
		if(m == 2 && maze[brian.y - 1][brian.x] != 1 && maze[brian.y - 1][brian.x] != 3) {
			brian.y--; system("cls");
		}
		if(m == 3 && maze[brian.y][brian.x - 1] != 1 && maze[brian.y][brian.x - 1] != 3) {
			brian.x--; system("cls");
		}
		if(m == 4 && maze[brian.y][brian.x + 1] != 1 && maze[brian.y][brian.x + 1] != 3) {
			brian.x++; system("cls");
		}
		
		o = random(4) + 1;
		
		if(m == 1 && maze[liam.y + 1][liam.x] != 1 && maze[liam.y + 1][liam.x] != 3) {
			liam.y++; system("cls");
		}
		if(m == 2 && maze[liam.y - 1][liam.x] != 1 && maze[liam.y - 1][liam.x] != 3) {
			liam.y--; system("cls");
		}
		if(m == 3 && maze[liam.y][liam.x - 1] != 1 && maze[liam.y][liam.x - 1] != 3) {
			liam.x--; system("cls");
		}
		if(m == 4 && maze[liam.y][liam.x + 1] != 1 && maze[liam.y][liam.x + 1] != 3) {
			liam.x++; system("cls");
		}	
	}
		
	
	return 0;
}
Last edited on
nice game :)
Some things I noticed right off the bat. Choose better variable names. Especially if you aren't commenting your code. Also, comment your code.
Nice game. Just some corrections/suggestions

(1) Line 63: If-condition has an assignment statement. Are you sure it shouldn't be: if(hero_win == true) ?

(2) Dont use using namespace std;. Use the scope resolution operator (::) for anything within the namespace std.

(3) Don't use system.

(4) Add code to check validity of user input. (what If user enters integer instead of WASD ?)
Last edited on
closed account (48T7M4Gy)
Excellent. Instead of a comma for a trap maybe a 'T' or 'O'?
Topic archived. No new replies allowed.