Blitz coder : dungeon crawler problem

Make a program that outputs a simple grid based gameboard to the screen using either numbers or characters.
i.e.

. . . . . . . . . .
. G . . . . . . . .
. . . . . . T . . .
. . . . . . . . . .
. . . . T . . . . .
. . . . . . T . . .
. . . . . . . . . X


or

0 0 0 0 0 0 0 0 0 0
0 5 0 0 6 0 0 0 0 0
0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 7 0 0 0 0 0 0
0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 0 0 4


Allow the user (marked by G in the example) to move either up, down, left, or right each turn. If the player steps on a trap then they lose. If the make it to the treasure 'X' then they win.

★★ Add enemies that move randomly in any direction once per turn. (enemies just like traps cause the player to lose if touched)

HINT: Don't let the player move off the gameboard! You program will crash if they move off the top or bottom of the board!
(the same holds true for enemies)


I attempted this question from the old forum post http://www.cplusplus.com/forum/articles/12974/

My query :
1)
sometimes my command shift from my player 'G' to enemy 'E'.for example
after a while ,if the enemy passed close by , and i start pressing 'a' the enemy E moves instead of the player G . Player G gets fixed to a place.

2) Is there a better way to do this question or this approach below is fine?
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
  
#include <iostream>

int main()
{
   
    /*
     . . . . . . . . . .
     . G . . . . . . . .
     . . . . . . T . . .
     . . . . . . . . . .
     . . . . T . . . . .
     . . . . . . T . . .
     . . . . . . . . . X
     */

   char crawler[] = " ...........G..............T........E........T...........T............X";
    
    char ch;
    char* G  = &crawler[12];
    char* N  = &crawler[12];
    char* enem = crawler;
    char* enemN;
     
    while(*enem  != 'E'){
        enem++;
    }
    enemN = enem;
    
    for (;;) {
         int enem_nxt_pos = rand()%4;
        
        for(int i = 0; i < sizeof(crawler) -1 ; i++){
            std::cout<<crawler[i];
            if(i%10 == 0) std::cout<<"\n";
        }

        std::cout<<"\n w,s,a,d for your next move and e to exit\n";        
        std::cin>>ch;
              
        if(ch =='w'){
            N -=10;
            if(N < &crawler[0]){
                std::cout<<"cant go more up";
                N +=10;
            }
            if(*N == 'T' || *N == 'E'){
                std::cout<<"you are out";
                exit(0);
            }
            if(*N == 'X') {
                std::cout<<"you won";
                exit(0);
            }
            else{
            std::swap(*G ,*N);
            G = N;
            }
        }
        
        else
        if(ch == 's'){
            N +=10;
            if(N > &crawler[sizeof(crawler)-1]){
                std::cout<<"cant go down";
                N -= 10;
            }
             else if(*N == 'T' || *N == 'E'){
                std::cout<<"you are out";
                exit(0);
            }
             else if(*N == 'X') {
                std::cout<<"you won";
                exit(0);
            }
            else{
                std::swap(*G,*N);
                G = N;
            }
        }
        
        else
        if(ch  == 'a'){
            N -=1;
            if(N<&crawler[1]){
                std::cout<<"cant go back";
                N += 1;
            }
            else if(*N == 'T' || *N == 'E'){
                std::cout<<"you are out";
                exit(0);
            }
            else if(*N == 'X') {
                std::cout<<"you won";
                exit(0);
            }
            else {
                std::swap(*G, *N);
                G = N;
            }
        }
        
       else
        if(ch == 'd'){
            N+= 1;
            if(N>&crawler[sizeof(crawler)-2]){
                std::cout<<"cant go forward";
                N -= 1;
            }
            else if(*N == 'T' || *N == 'E'){
                std::cout<<"you are out";
                exit(0);
            }
            else if(*N == 'X') {
                std::cout<<"you won";
                exit(0);
            }
            else {
                std::swap(*G, *N);
                G = N;
            }

        }
        
        else
        if(ch  == 'e'){
            exit(0);
        }
        
        else {
            std::cout<<"wrong option";
        }
        
        if(enem_nxt_pos == 0){
            enemN -= 10;
            if(enemN < &crawler[1]) enemN += 60;
            if(*enemN == 'T' || *enemN == 'X') enemN -= 10;
            if(*enemN == 'G'){
                std::cout<<"you are out";
                exit(0);
            }
            std::swap(*enem,*enemN);
            enem = enemN;
            
        }
        
        if(enem_nxt_pos == 1){
            
            enemN +=1;
            if(enemN > &crawler[sizeof(crawler)-2]) enemN = &crawler[0];
            if(*enemN == 'T' || *enemN == 'X') enemN += 1;
            if(*enemN == 'G'){
                std::cout<<"you are out";
                exit(0);
            }

            std::swap(*enem,*enemN);
            enem = enemN;
        }
        
        if(enem_nxt_pos == 2){
            
            enemN += 10;
            if(enemN > &crawler[sizeof(crawler)-2]) enemN -= 60;
            if(*enemN == 'T' || *enemN == 'X') enemN += 10;
            if(*enemN == 'G'){
                std::cout<<"you are out";
                exit(0);
            }

            std::swap(*enem,*enemN);
            enem = enemN;
        }
        
        if(enem_nxt_pos == 3){
            enemN -= 1;
            
            if(enemN < &crawler[1]) enemN = &crawler[sizeof(crawler)-2];
            if(*enemN == 'T' || *enem == 'X') enemN -= 1;
            if(*enemN == 'G'){
                std::cout<<"you are out";
                exit(0);
            }

            std::swap(*enem,*enemN);
            enem = enemN;
            
        }    
    }
}


Edit: I think the shifting of the command from 'G' to 'E' has stopped after i put

1
2
3
4
if(*enemN == 'G'){
                std::cout<<"you are out";
                exit(0);
            }


in every condition of the enemy ….
Last edited on
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
// my first game.
#include <iostream>      
#include <algorithm>  
#include <cstdlib>         
#include <ctime>      
using namespace std;
 
int main() 
{
	int i,j, gi, gj, ei, ej;
	srand (time(NULL));
  char ar[10][10];
  for(int i=0; i<10; i++)
  	for(int j=0; j<10; j++)
  		ar[i][j] ='.';
  		
  //put 5 (at most) random trap
  ar[rand()%8+1][rand()%8+1] = 'T';
  ar[rand()%8+1][rand()%8+1] = 'T';
  ar[rand()%8+1][rand()%8+1] = 'T';
  ar[rand()%8+1][rand()%8+1] = 'T';
  ar[rand()%8+1][rand()%8+1] = 'T';
  //enemy
  ei = rand()%8+1; ej = rand()%8+1;
  ar[ei][ej] = 'E';	 	
  
 
	ar[0][0] ='G'; ar[9][9] = 'X';
	gi = 0; gj =0; 

  	
  	char u ='u', d='d', l='l', r='r', key;
  	//cout << "set you keys for movement: up, down, left, right, separated by space.\n";  	
  	//cin >> u >> d >> l >> r; 
	//cin.ignore();
  	
  	cout << "mission: go to destination X\n"
	  	<< "you are G, enemy is E, you will die if go to trap\n"
	  	<< "enemy can swap trap!\n"
		<< "lets start. u=up d=down l=left r=rigth\n\n";
		
	for(int i=0; i<10; i++) ///diagram
  	{
  		for(int j=0; j<10; j++)
  		{cout << ar[i][j];		}
  		cout << endl;
  	} cout << "\n\n";
	 
  	
  	do 
  	{
  		cout << "go.";
		cin>>key; cin.ignore();			  	  	
  		
  			 if (key==u) { if(ar[gi-1][gj] == 'T') { cout <<"entered trap\n"; break; } gi--;  swap ( ar[gi][gj], ar[gi+1][gj]);  }
  		else if (key==d) { if(ar[gi+1][gj] == 'T') { cout <<"entered trap\n"; break; } gi++;  swap ( ar[gi][gj], ar[gi-1][gj]);  }
  		else if (key==l) { if(ar[gi][gj-1] == 'T') { cout <<"entered trap\n"; break; } gj--;  swap ( ar[gi][gj], ar[gi][gj+1]);  }
  		else if (key==r) { if(ar[gi][gj+1] == 'T') { cout <<"entered trap\n"; break; } gj++;  swap ( ar[gi][gj], ar[gi][gj-1]);  } 
  		
  		tryagain:
  	  int erand = rand()%4;	
  		     if (erand == 0) { if(ar[ei-1][ej] == 'G') { cout <<"caugth by enemy\n"; break; } ei--; swap ( ar[ei][ej], ar[ei+1][ej]);	}
  		else if (erand == 1) { if(ar[ei+1][ej] == 'G') { cout <<"caugth by enemy\n"; break; } ei++; swap ( ar[ei][ej], ar[ei-1][ej]);	}
  		else if (erand == 2) { if(ar[ei][ej-1] == 'G') { cout <<"caugth by enemy\n"; break; } ej--; swap ( ar[ei][ej], ar[ei][ej+1]);	}
  		else if (erand == 3) { if(ar[ei][ej+1] == 'G') { cout <<"caugth by enemy\n"; break; } ej++; swap ( ar[ei][ej], ar[ei][ej-1]);	} 
  	  if ( ei<0 || ej<0 || ei>9 || ej>9) { goto tryagain;	}
  			
  		if (gi==9 && gj==9) { cout << "congratulation! you won!\n"; break;	}
  		if ( gi<0 || gj<0 || gi>9 || gj>9) { cout << "went out of field!\n"; break;	}
  		
  		for(int i=0; i<10; i++) ///diagram
  			{
  				for(int j=0; j<10; j++)
  				{cout << ar[i][j];	}
  				cout << endl;
  			} cout << "\n\n";
  	
  	} while (1);
  	cout <<"Game Over\n";
  		
  return 0;
}
i didn't debug your program , but it starts deleting points :

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
G.........
...T......
..T.......
.T........
..........
..........
.T......T.
..........
........E.
.........X


go.r
.G........
...T......
..T.......
.T........
..........
..........
.T......T.
..........
..........
........EX


go.
r
..G.......
...T......
..T.......
.T........
..........
..........
.T......T.
..........
..........
........XE


go.r
r
...G......
...T......
..T.......
.T........
..........
..........
.T......T.
..........
..........
........XE


go.r
....G.....
...T......
..T.......
.T........
..........
..........
.T......T.
..........
..........
........XE


go......G....
...T......
..T.......
.T........
..........
..........
.T......T.
..........
..........
........EX


go.d
..........
...T.G....
..T.......
.T........
..........
..........
.T......T.
..........
..........
........XE


go.d
..........
...T......
..T..G....
.T........
..........
..........
.T......T.
..........
.........E
........X.


go.d
..........
...T......
..T.......
.T...G....
..........
..........
.T......T.
..........
........E.
........X.


go.d
..........
...T......
..T.......
.T........
.....G....
..........
.T......T.
..........
.........E
........X.


go.d
..........
...T......
..T.......
.T........
..........
.....G....
.T......T.
..........
..........
........XE


go.d
..........
...T......
..T.......
.T........
..........
..........
.T...G..T.
..........
..........
.......XE


go.d
..........
...T......
..T.......
.T........
..........
..........
.T......T.
.....G....
..........
....E..X


go.d
..........
...T......
..T.......
.T........
..........
..........
.T......T.
..........
.....G....
....E..X


go.


The last 2 points in the last line are deleted .
Last edited on
Topic archived. No new replies allowed.