My Dungeon Crawl traps move twice per turn.

Warning: Do not watch it if yoy haven't finished the exercise, but plan to(this kind of execution may influence you badly). Secondly, this program may not run operating systems other than windows.
Hello!
I recently made a Dungeon Crawl exercise program, but it behaves a little bit weird. I tried to make traps move after I move the player(G) and make them move only once. What I got now is: when I move the player the traps move too and after that they move once more.
Here is the source code(tried to make it understandable ^.^):
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Dungeon Crawl.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

//Dungeon Crawl
#include <iostream>
#include <time.h>             //To seed random with time
#include <conio.h>            //For getch()
#include <windows.h>          //For clear_screen funtion



int player_pos=1,c,trap_pos1=26,trap_pos2=54,trap_pos3=66;  //variables for player/trap position.
int k,trap_check=0;              //k for loop and trap_check to check if player has stepped on a trap.
char b[69];                      //That array will contain the board.
void board();                    //Function to print board.
void clear_screen();             //function to clear screen.
void trap_move(int a, int b);     //function to change trap_pos variable.
void sleep(unsigned int mseconds); //sleep

int main()
{
	
	for(k=0;k<70;k++){			 //Assign '.' to every "block?" of an array.
		b[k]='.';
	}
	b[player_pos]='G';           //Set the player position.
	b[trap_pos1]='T';            //Set  trap 1 positon.
	b[trap_pos2]='T';            //Set  trap 2 positon.
	b[trap_pos3]='T';            //Set  trap 3 positon.
	b[69]='X';					 //Set treasure position.
	board();

	while(player_pos!=69){     //While not on trasure.
		
		








               c=getch();                       //If I use cin>>c(char) here and swich with w/a/s/d it works as it should, why isn't it working with getch()???? 
		







               b[player_pos]='.';			//Assigns a dot to player value, because player location will be changed.
		
		switch(c){
			case 72:
				player_pos=player_pos-10;          //If up, down, left, right arrow is pressed changes the value
				break;                             //of player_pos. 
			case 80:
				player_pos=player_pos+10;
				break;
			case 75:
				player_pos--;
				break;
			case 77:
				player_pos++;
				break;
		}


		if(b[player_pos]=='T'){                             //To check if player has stepped on T(trap).
			std::cout<<"You stepped on a trap!";
			trap_check=1;
			player_pos=69;
		} //if ends
		else{                                              //If player hasn't stepped on a trap, assigns player position and
		b[player_pos]='G';								   
		
		/*Why does this part take so long time?*/
		
		clear_screen();                //couts the board.            
		board();

		trap_move(trap_pos1, 1);                          //Assigns value to trap1.
		trap_move(trap_pos2, 2);                          //Assigns value to trap1.
		trap_move(trap_pos3, 3);						  //Assigns value to trap1.

		sleep(1000);                                      //Sleeps so you can see my problem with naked eye.

		clear_screen();                                   //Couts the board.
		board();

		

		if(b[player_pos]=='T'){                           //To check if trap has stepped on a player. Should be impossible(you'll see later).
			std::cout<<"You were caught by a trap!";
			trap_check=1;
			player_pos=69;
		}  //if ends

		} //else ends
		
	}  //loop ends   

	if(player_pos==69&&trap_check==0){           //To check if player made it to the treasure.
		std::cout<<"You've made it!!";
	} //if ends
	std::cin.ignore();
	std::cin.get();
	return(0);
} //main ends

void trap_move(int a, int h)//a==trap value to change,h==trap identifier basically              //Assigns value to trap_pos1/2/3.
{
	int c,y;        //c for storing random number and y for changing trap value back to original
	                //in case the loop ends and the condition isn't met but the variable has changed.

	switch(h){                  //Gives every trap a little different seed, so they won't be the same.
		case 1:
			srand(time(NULL)+345);
			break;
		case 2:
			srand(time(NULL)+6786);
			break;
		case 3:
			srand(time(NULL)+6786);
			break;
	} 

	c=rand()% 4+1;      //Creating random number 1-4(up,down,left right on the board)
	y=a;				//Storing the original value, because a is going to be changed and it might be needed.
	
	do{
	a=y;

	switch (c){          //Change the trap value (aka move the trap)  according to the random number.
	case 1:              //up
		if(a>=10){
			a=a-10;
		}
		break;
	case 2:              //down
		if(a<59){
			a=a+10;
		}
		break;
	case 3:              //left
		if(a>0){
		a--;
		}
		break;
	case 4:              //right
		if(a<67){
		a++;
		}
		break;
	}

	if(b[a]=='T'){}     //If the trap happens to move on another trap, the move will be randomized again
						//(moves back to the beginning of the loop.

	else{               //Else, it'll change the old value to '.' 
		b[y]='.';

	switch(h){          //and makes new value to trap value according to its identifier(h).  
	case 1:
		trap_pos1=a;
		b[trap_pos1]='T';
		break;
	case 2:
		trap_pos2=a;
		b[trap_pos2]='T';
		break;
	case 3:
		trap_pos3=a;
		b[trap_pos3]='T';
		break;
	} //switch ends
	} //else ends
	}while(b[a]=='X'||b[a]=='G'||a>68||a<0);      //Loop lasts until new value is X(trasure) or G(player) 
												  //or exceeds 68(goes out of the gameboard or is below 0(same).
}

void board()                                                            //Couts the board.
{
	int n;
	for(n=0;n<70;n++){
		std::cout<<b[n];
		if(n==9||n==19||n==29||n==39||n==49||n==59||n==69){
		std::cout<<"\n";
		}
	}
}



void clear_screen()                                                   //Clears the screen.(Nothing to check)
{
DWORD n; /* Number of characters written */
DWORD size; /* number of visible characters */
COORD coord = {0}; /* Top left screen position */
CONSOLE_SCREEN_BUFFER_INFO csbi;
/* Get a handle to the console */
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo ( h, &csbi );
/* Find the number of characters to overwrite */
size = csbi.dwSize.X * csbi.dwSize.Y;
/* Overwrite the screen buffer with whitespace */
FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
GetConsoleScreenBufferInfo ( h, &csbi );
FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
/* Reset the cursor to the top left position */
SetConsoleCursorPosition ( h, coord );
}


void sleep(unsigned int mseconds)                         //Sleeps(nothing to check).
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}    


Here is a code in case you need the numbers for switch(c) with getch(). It creates a text file in the executable direction with the numbers you need:

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
//getch() numbers for arrows
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

#include <iostream>
#include <conio.h>
#include <fstream>



int main()
{
	int a,b,c,d;
	std::cout<<"Press up arrow: ";
	getch();
	a=getch();
	std::cout<<"\nPress down arrow: ";
	getch();
	b=getch();
	std::cout<<"\nPress left arrow: ";
	getch();
	c=getch();
	std::cout<<"\nPress right arrow: ";
	getch();
	d=getch();
	std::ofstream myfile;
	myfile.open("Arrows.txt");
	myfile<<"up: "<<a<<"\n"<<"down: "<<b<<"\n"<<"left: "<<c<<"\n"<<"right: "<<d;
	std::cout<<"\nPress enter to exit";
	std::cin.get();
	return(0);
}

(I heard that the values may vary through systems/compilers, is it true?)

Now if anyone knows why my Dungeon Crawl acts like that, please let me know. (I want traps to move once after I've made my move)

Thanks in advance,
Vanapapi
Last edited on
If I replace the getch() with a cin it works properly, but that means I'll have to enter w/a/s/d and then hit enter. Has it something to do with the fact that getch() has 2 values for arrows but I'm using only one? (e.g. when I cin>>getch() I get numbers like 257 80, but if I do c=getch() I get 80 and later use only the 80, leaving 257 out). Any idea how to get this work with getch()?
Topic archived. No new replies allowed.