Problem in making a Dungeon Crawl game

Hi everyone,

I made a Dungeon Crawl game with C++, and i got some problems.
1. I made the traps which moves randomly, but they moves in the same direction.
2. how to avoid the traps or goal or player overlapping at starting up.

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
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

void board(char map[]);
void movement(char map[], int &position, int &status);
void single_move(char map[], int &position, int target, int &status);
void trap_move(char map[], int&trap_position, int &status);
void instruction();

int main(){
    srand(time(NULL));

    char map[100];
    int status=0;

    for (int i = 0; i < 100; i++){
        map[i] = '-';
    }
    int position = rand() % 100;
    int trap_position_1 = rand() % 100;
    int trap_position_2 = rand() % 100;
    int trap_position_3 = rand() % 100;
    map[position] = 'U';
    map[trap_position_1] = 'T';
    map[trap_position_2] = 'T';
    map[trap_position_3] = 'T';
    map[rand() % 100] = 'G';

    board(map);
    do{
        movement(map, position, status);
        trap_move(map, trap_position_1, status);
        trap_move(map, trap_position_2, status);
        trap_move(map, trap_position_3, status);
        if (status == -1)
            map[position] = 'X';
        board(map);
    } while (status == 0);
    
    if (status == -1)
        cout << "you lost!!!" << endl;
    else
        cout << "you win!!!" << endl;

}

void board(char map[]){
    system("cls");
    instruction();
    for (int i = 0; i < 10; i++){
        for (int j = 0; j < 10; j++){
            cout << map[i * 10 + j];
        }
        cout << endl;
    }
    cout << endl;
}

void movement(char map[], int &position, int &status){
    int x;
    cout << "please move ([8]-up|[2]-down|[4]-left|[6]-left):";
    cin >> x;
    if (x == 4 && (position % 10) != 0){
        single_move(map, position, position - 1, status);
    }
    else if (x == 6 && position%10!=9){
        single_move(map, position, position + 1, status);
    }
    else if (x == 8 && 9<position){
        single_move(map, position, position - 10, status);
    }
    else if (x == 2 && 90>position){
        single_move(map, position, position + 10, status);
    }
}

void single_move(char map[], int &position, int target, int &status){
    if (map[target] == 'G'){
        map[target] = '*';
        map[position] = '-';
        position = target;
        status = 1;
    }
    else if (map[target] == '-'){
        map[target] = 'U';
        map[position] = '-';
        position = target;
        status = 0;
    }
    else{
        map[target] = 'X';
        map[position] = '-';
        position = target;
        status = -1;
    }

}

void trap_move(char map[], int&trap_position, int &status){
    int x;
    srand(time(NULL));
    x = rand() % 4;
    if (x == 0 && (trap_position % 10) != 0){
        if (map[trap_position - 1] != 'G' && map[trap_position - 1] != 'T'){
            map[trap_position - 1] = 'T';
            map[trap_position] = '-';
            trap_position = trap_position - 1;
        }
        if (map[trap_position - 1] == 'U')
            status = -1;
    }
    else if (x == 1 && trap_position % 10 != 9){
        if (map[trap_position + 1] != 'G' && map[trap_position + 1] != 'T'){
            map[trap_position + 1] = 'T';
            map[trap_position] = '-';
            trap_position = trap_position + 1;
        }
        if (map[trap_position + 1] == 'U')
            status = -1;
    }
    else if (x == 2 && 9<trap_position){
        if (map[trap_position -10] != 'G' && map[trap_position - 10] != 'T'){
            map[trap_position - 10] = 'T';
            map[trap_position] = '-';
            trap_position = trap_position - 10;
        }
        if (map[trap_position - 10] == 'U')
            status = -1;
    }
    else if (x == 3 && 90>trap_position){
        if (map[trap_position +10] != 'G' && map[trap_position +10] != 'T'){
            map[trap_position + 10] = 'T';
            map[trap_position] = '-';
            trap_position = trap_position + 10;
        }
        if (map[trap_position +10] == 'U')
            status = -1;
    }
}

void instruction(){
    cout << "moves the [U] to the goal[G], and avoid the trap[T]!!" << endl << endl;
}
time(NULL) returns the time in seconds so it is very likely that you are using the same seed in each of the three calls to trap_move. That means you will get the same random value x for all of them.

srand should normally not be called more than once. Even if the seed is different each time calling it more than once usually makes it less random.

So to fix your problem simply remove the call to srand inside trap_move.
aryue1945 wrote:
how to avoid the traps or goal or player overlapping at starting up.

Call rand in a loop and let the loop end when you have found an empty position.
1
2
3
4
5
int position;
do
{
	position = rand() % 100;
} while (map[position] != '-');

To avoid copy pasting you should probably put it inside a function. This works well if the map is sparsely populated. If the map is almost full it could be quite slow so in that case there are other ways that are better.
Got it, Thank you Peter!!!!
Topic archived. No new replies allowed.