Developing a basic game but I can't generate "traps" properly

So I am writing a somewhat basic game as a project for my C++ class. I know you guys don't like homework but this idea was entirely my own and I've been working on it for awhile. I need to present this later today and I wish i could just finish the darn thing the way I wanted too. The problem is, when I try to generate traps the same way I generate my food for the "hippo", it only places a trap or it places a bunch of food and traps but it doesn't keep track of them right. The program works pretty good with the traps disabled but when i try to turn them on it gets weird. All traps related code is put in /* these things */ Any help would be greatly appreciated. I don't have time to re-write a lot of stuff so any half-assed improper ways or tips that you could come up with would be GREATLY appreciated.

I am writing this in Dev-C++
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
  #include <iostream>
#include <ctime>
#include <cstdlib>
#include <Windows.h>

using namespace std;

void clearscreen(); //stops flicker
void movement(int left, int right, int up, int down, int &y, int &x); //used to move and position hippo

int main() {
	char map[50][90];

		//loads the bottom and top of the map with a border
		for(int i=0; i < 40; ++i) {
			map[0][i] = '#';
			map[19][i] = '#';
		}
		//loads the rest of the map with vertical borders and a space between them
		for(int i=1; i < 19; ++i) {
			for(int j=0; j < 41; ++j) {
				map[i][0] = '#';
				map[i][39] = '#';
					if(j < 39) {
					map[i][j] = ' ';
				}
			}
		}
		char hippo = 'o';
		int y=9, x=19; //used to coordinate the hippo
		int left=0, right=0, up=0, down=0; //used to determine which direction the hippo should go
		int foodcount = 0; //counts ammount of food eaten
		int eaten = 0; //used to check if the hippo ate food

		//generates the food
		char food = '*';
		int a, b; //used to position the food
		srand(time(0));
		a = rand() % 17 + 1;
		Sleep(25);
		b = rand() % 37 + 1;
		map[a][b] = food;
		
		
		
	/*	//generates the traps
		char trap = 'X';
		int c, d; //used to generate the traps
		srand(time(0));
		c = rand() % 18 + 1;
		Sleep(25);
		d = rand() % 38 + 1;
		map[c][d] = trap;      */

		for(;;) { //the game loop
			clearscreen();
					map[y][x] = hippo; //places the hippo

		//displays the map
		for(int i=0; i < 20; ++i) {
			for(int j=0; j < 40; ++j) {
				cout << map[i][j];
					if(j >= 39) {
						cout << endl;
					}
			}
		}
		cout << "Food eaten: " << foodcount << endl; //displays ammount of food eaten

		//checks if the hippo moves in another direction
		if(GetAsyncKeyState(VK_LEFT)) {
			right=0, up=0, down=0;
			left = 1;
		}
		else if(GetAsyncKeyState(VK_RIGHT)) {
			left=0, up=0, down=0;
			right = 1;
		}
		else if(GetAsyncKeyState(VK_UP)) {
			right=0, left=0, down=0;
			up = 1;
		}
		else if(GetAsyncKeyState(VK_DOWN)) {
			left=0, up=0, right=0;
			down = 1;
		}
			//moves the hippo
			map[y][x] = ' ';
		movement(left, right, up, down, y, x);
			map[y][x] = hippo;

			//checks if the player crashed into the border
			if(x == 0 || y == 0 || x == 39 || y == 19) {
				cout << "\n BAM! You charged into a wall!" << endl;
				cin.get();
				return main() ;
			}
			if(map[y][x] == map[a][b]) {
				++foodcount;
				++eaten;
			}
		/*	 if(map[y][x] == map[c][d]) {
                cout << "\n Oh No!, You got snagged in a trap!" << endl;
                eaten++;
                cin.get();
                return main() ;  
            }   */
			//if the hippo ate food, generate a new one
			if(eaten > 0) {
				eaten = 0;
				a = rand() % 17 + 1;
				Sleep(25);
				b = rand() % 37 + 1;
				map[a][b] = food;
			/*	c = rand() % 18 + 1;
				Sleep(25);
				b = rand() % 38 + 1;
				map[c][d] = food;   */
			}
		}
	return 0;
}
// moves hippo, the sleep timers prevent stuttering
void movement(int left, int right, int up, int down, int &y, int &x) {
	if(left > 0) {
		--x;
		Sleep(22);
		return;
	}
	else if(right > 0) {
		++x;
		Sleep(22);
		return;
	}
	else if(up > 0) {
		--y;
		Sleep(24);
		return;
	}
	else if(down > 0) {
		++y;
		Sleep(24);
		return;
	}
}
void clearscreen()
{
    HANDLE hOut;
    COORD Position;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    Position.X = 0;
    Position.Y = 0;
    SetConsoleCursorPosition(hOut, Position);
}
Last edited on
After glancing at your code quickly, I notice you set map[c][d] = food after the player has eaten a piece of food. However you always check if(map[y][x] == map[c][d]) as a trap, regardless of what's actually on there. That's something you might want to fix first.
Good catch, I just corrected it in the code but will test it later. I'll let you know if that helped iron things out :)
Last edited on
there is also no safe gaurd to make sure that map[c][d] is not taken by map[a][b] you could be replacing a food with a trap. then there is no food on screen only traps.
just tested your game.

some bugs to fix...

your visual display does not match you actual version.
what I mean is trap = map[c][d] right? but the value of c and d can change.

you still have what looks like trap on screen but nothing storing its actual location. when a second trap is placed you can freely run over the first because it's map location has been forgotten. It still looks like a trap but it isn't. instead of saying map[c][d] = trap; also add map[c][d] = 'x'; then when hippo moves see if it's current location is storing an 'x'. then he hit a trap.

I hope you follow what I mean because I have trouble.
Thanks Manga, I know it's buggy... It's my first time making something this complicated. It was presentable with the traps disabled and still a decent program compared to the other presentations. I will look at and make the corrections you recommended. Thanks again :)
I think you are doing great, and I admit you are more experienced with c++ than I.
Just a curious question, how did you learn all that? :o That must have taken a lot of time.
Topic archived. No new replies allowed.