Dungeon Crawler Monsters

I am generating random monsters, and it takes quite a while until the game is loaded, probably my way for generating random monsters is not very good.

Also I am not sure how to implement monsters random movement and losing after you hit a monster.

Any help will be appreciated. Thank you!

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
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <ctime>
using namespace std;

int main()
{
	int i, j;
	int posX = 1, posY = 1;
	char player = 'G';
	char trap = 'T';
	char enemy = 'M';
	char treasure = 'X';
	char map[7][10];

	//Initializing the map
	for(i=0; i<7; i++){
		for(j=0; j<10; j++){

			map[i][j] = '.';
		}
	}

	//Generating Random Monsters
	for(i=0; i<4; i++)
	{
	for(j=0; j<4; j++)
	{
	srand(time(0));
	i = rand() % 6;
	j = rand() % 9;
	map[i][j] = enemy;
	}
	}

	//Initializing 
	map[1][1] = player;
	map[2][6] = trap;
	map[4][4] = trap;
	map[5][6] = trap;
	map[6][9] = treasure;
	
	//Drawing the Map
		for(i=0; i<7; i++)
		{
			for(j=0; j<10; j++)
			
			cout << map[i][j]<< " ";
			cout << endl;
		}
	
	while(true)
	{
		char move;
		cin >> move;
	
		switch(move)
		{
			// move up;
		case 'w':				
			if(posY <= 0)
			{
				cout << endl;
				cout << "Character is going out of the range! Try again";
				cout << endl;
				cout << endl;
				break;
			}		
			else
			{
			map[posY][posX] = '.';
			posY -=1;
			map[posY][posX]= player;
			}
			break;			
		// move down;
		case 's':
			if(posY >= 6)
			{
				cout << endl;
				cout << "Character is going out of the range! Try again";
				cout << endl;
				cout << endl;
				break;
			}
			else
			{
			map[posY][posX] = '.';
			posY +=1;
			map[posY][posX]= player;
			}
			break;
		// move left;
		case 'a':	
			if (posX <= 0)
			{
				cout << "Character is going out of the range! Try again";
				cout << endl;
				cout << endl;
			}
			else
			{
			map[posY][posX] = '.';
			posX -=1;
			map[posY][posX]= player;
			}
			break;
		//move right
		case 'd':
			if (posX >= 9)
			{
				cout << "Character is going out of the range! Try again";
				cout << endl;
				cout << endl;
			}
			else	
			{
			map[posY][posX] = '.';
			posX +=1;
			map[posY][posX]= player;
			}
			break;
		}
		
		//Re-drawing the map for each turn
		for(i=0; i<7; i++)
		{
			for(j=0; j<10; j++)
			
			cout << map[i][j]<< " ";
			cout << endl;
		}

		// Steping on traps
		if( map[2][6] == 'G' || map[4][4] == 'G' || map[5][6] == 'G' )
		{
			cout << "It's a trap! You died!";
			break;
		}

		// Finding the treasure
		if( map[6][9] == 'G')
		{
			cout << "You found the treasure! You win!";
			break;
		}				
	}
	_getch();
}
Last edited on
1
2
3
4
5
6
	srand(time(0));

	//Generating Random Monsters
	for(i=0; i<4; i++)
		for(j=0; j<4; j++)
			map[rand()%6][rand()%9] = enemy;


Seed the generator once per program invocation. If you seed it in a loop in this fashion, it generates the same seed multiple times in a row (until the time changes) causing your loop to churn on for quite a while. In general, do not change the counters used in your for loop conditionals inside the loop.
Last edited on
Thanks, I understand that perfectly now, it works very fast.

Anyway still I need help for monster random movement and condition for player meeting with the monster.
You should not use <conio.h>, it is not part of the C/C++ standard:
https://en.wikipedia.org/wiki/Conio.h

rand() and srand() are deprecated - prefer to use the <random> header:
http://www.cplusplus.com/reference/random/
http://en.cppreference.com/w/cpp/header/random
Last edited on
I tried to do the meeting with the monster condition and here is what I have.

Let me explain what I did.

1
2
3
4
5
6
7
//Generating Random Monsters
	for(i=0; i<4; i++)
	{
		randomi = rand() % 6;
		randomj = rand() % 9;
  	map[randomi][randomj] = enemy;
	}


We are generating multiple monsters, here we will generate 4 monsters.


1
2
3
4
5
6
// Meeting the monster	
		if (map[randomi][randomj] == 'G')
		{
		cout << "The monster ate you!";
		break;
		}


Only one monster will kill you, and that is because the final value of randomi and randomj is being taken

I see a solution: Generate separate random monsters, and put an if for each monster.

I am not sure that this is the best solution. Yes for 4 monsters is not so much code to write, but what if I have 100 monsters?
Lines 44-51 and 126-133 are redundant. Drawing the map should be a separate function.

Lines 26-35 it's possible that a monster could overlay an existing monster. Small likelyhood (1 in 70) but it can happen.

Lines 38-42: You could also be overlaying a monster here.

Line 49: You're telling the player where the treasure, traps and monsters are. This is fine for debugging, but you probably want to be able to hide these items from the player.

Lines 62-75, 79-92, 96-107, 111-122: Do you see a lot of commonality in these lines? It would make sense to put bounds checking and moving the player in a common function.

IMO, you're looking at testing for monsters, traps and treasure the wrong way. You should looking at the contents of the map when you attempt to move the player to a new location. i.e. Before placing the player in a new location, test if that new location has a trap, monster, or treasure and act accordingly.
Topic archived. No new replies allowed.