Dungeon Crawl type question

Pages: 12
First of all, thank you for looking at my post. Here is my code
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
#include <stdlib.h>
#include <iostream>
#include <ctime>

#define WIDTH 16
#define HEIGHT 16
#define BOMB 1
#define TREASURE 2
#define PASS 0
using namespace std;

void grid(int,int);
void menu();
int check(int,int);

int bomb1[1][2];
int bomb2[1][2];
int bomb3[1][2];
int bomb4[1][2];
int treasure[1][2];

int main(){
  int x_cord=1,y_cord=1;
  int move=0;

  srand(time(NULL));
  bomb1[0][0] = rand() % WIDTH + 1;
  bomb1[0][1] = rand() % HEIGHT + 1;
  bomb2[0][0] = rand() % WIDTH + 1;
  bomb2[0][1] = rand() % HEIGHT + 1;
  bomb3[0][0] = rand() % WIDTH + 1;
  bomb3[0][1] = rand() % HEIGHT + 1;
  bomb4[0][0] = rand() % WIDTH + 1;
  bomb4[0][1] = rand() % HEIGHT + 1;
  treasure[0][0] = rand() % WIDTH + 1;
  treasure[0][1] = rand() % HEIGHT + 1;

  cout << "\t\t\t\tGrid Movement w/ Keyboard Input!" << endl << endl
       << "The goal is to move around the grid and just enjoy yourself while doing it" << endl
       << "In order to move your player (P) around you press...." << endl << endl
       << "\t2 --> Down" << endl
       << "\t4 --> Left" << endl
       << "\t8 --> Up" << endl
       << "\t6 --> Right" << endl
       << "\t88 --> Instructions (This menu)" << endl
       << "\t99 --> Quit" << endl;


  cout << bomb1[0][0] << ", " << bomb1[0][1] << endl
       << bomb2[0][0] << ", " << bomb2[0][1] << endl
       << bomb3[0][0] << ", " << bomb3[0][1] << endl
       << bomb4[0][0] << ", " << bomb4[0][1] << endl
       << treasure[0][0] << ", " << treasure[0][1] << endl; 

  while(move != 99 && check(x_cord,y_cord) == PASS){
    cout << "Move? (88 for Instructions)" << endl;
    cin >> move;

    switch (move){
      case 8:
        if(y_cord > 1){
          y_cord = y_cord - 1;
   	  grid(x_cord,y_cord);
        }
        else
          cout << "Can't move upward off thy grid...Come on...ith!" << endl;
        break;
      case 4:
        if(x_cord > 1){
   	  x_cord = x_cord - 1;
	  grid(x_cord,y_cord);
        }
	else
	  cout << "You aren't allowed any futher West, try again!" << endl;
        break;
      case 2:
        if(y_cord < HEIGHT){
	  y_cord++;
	  grid(x_cord,y_cord);
        }
	else
	  cout << "You're about to pop off the bottom of the grid!" << endl;
	break;
      case 6:
	if(x_cord < WIDTH){
	  x_cord++;
	  grid(x_cord,y_cord);
	}
	else
	  cout << "Any farther right and you won't even be on the map!" << endl;
	break;
      case 99:
	cout << "Exitting... Have a nice day!" << endl;
      	break;
      case 88:
	menu();
        break;
      default:
	cout << "Invalid option... please do try again!" << endl;
    }
  }
  if(check(x_cord,y_cord) == BOMB)
  {
    cout << "\n\nOoooooh, you totally hit a bomb!" << endl << endl << endl;
    cout << "\tTreasure was at cordinates " << treasure[0][0] << ", " << treasure[0][1] << "! Better luck next time..." << endl;
  }
  else if(check(x_cord,y_cord) == TREASURE)
    cout << "\n\nWhoa, you just found the hidden treasure!" << endl;
return 0;
}
void grid(int x,int y){
  char grid[WIDTH][HEIGHT];
  int i=0,j=0;
  for(i=0;i<HEIGHT;i++){
     for(j=0;j<WIDTH;j++){
        grid[i][j] = 'o';
     }
  }
  grid[y-1][x-1] = 'P';
 
  for(i=0;i<25;i++){
    cout << endl << endl;
  }

   for(i=0;i<HEIGHT;i++){
    for(j=0;j<WIDTH;j++){
      cout << grid[i][j];
    }
    cout << endl;
  }
  return;
}
void menu(){
  cout << "\t2 --> Down" << endl
       << "\t4 --> Left" << endl
       << "\t8 --> Up" << endl
       << "\t6 --> Right" << endl
       << "\t99 --> Quit" << endl;
  return;
}
int check(int x,int y){
  int value;
  if(bomb1[0][0] == x && bomb1[0][1] == y)
    value = BOMB;
  else
    if(bomb2[0][0] == x && bomb2[0][1] == y)
      value = BOMB;
  else
    if(bomb3[0][0] == x && bomb3[0][1] == y)
      value = BOMB;
  else
    if(bomb4[0][0] == x && bomb4[0][1] == y)
      value = BOMB;
  else
    if(treasure[0][0] == x && bomb4[0][1] == y)
      value = TREASURE;
  else
    value = PASS;
 return value;
}


I cant seem to get my treasure, even when i know the cord's.
Well, instead of trying to look through 160 lines of code, how bout you post the problem snippet, or just tell us some line numbers
I would post a snippet, but I'm not sure where the snippet is, because i'm not sure where the error is. Sorry.

My grid works and displays fine, character moves around fine, but i can't get my bomb/treasure portion working. I think i might not be comparing correctly inside my check() function.

I hope that helps
Can you maybe explain what your design was? I don't understand the 1 by 2 matrices.
Sure. They represent bombs and a treasure chest, with the first element in the array being the x coordinate and 2nd being the y coordinate. I then check to see if the x cord and y cord of the player position match the coordinates of any bombs or the chest at the end of the main while loop in my main function.
If it's an array, why are there two indexes? (Row and column, as opposed to a single index like in a list.)
What I mean is a matrix with one row could be better coded as a 1D array. Like:
int bomb1[2];
Then again, you could code all 4 bombs in one matrix like:
int bombs[4][2]; //4 bombs, each with 2 components
Then you can make use of for loops to iterate though the list of bombs.

Just taking a guess here, but what do you think
rand() % WIDTH + 1 does? If you said gives you a number in the range of [1, WIDTH] you would be right. If you want a number in the range of [0, WIDTH] you want
rand() % (WIDTH + 1)
Last edited on
I will definitely be changing them to 1D arrays. And yes, that is actually the range I was after, strangely enough. I have my axis start at 1,1, just because that made more sense to me. Do you see why I can't get the loop to end when I supposedly land on a bomb or the chest. I've ran it and I can only get it to end on a bomb.
Ohk, I read through your code, I think the problem here is that your map is a different array as from the bombs and treasures..
So, I came up with an idea.. Instead, why not select two random numbers between 0 & 16, and then place your bomb at those co-ordinates, like:

1
2
3
4
5
6
7
int x, y;
while(grid[x][y]!='o')
{
   x=rand()%16+1;
   y=rand()%16+1;
}
grid[x][y]=BOMB;


Repeat 6 times for bombs and twice for treasures... Store the location of your bombs in another array maybe, and make sure you don't place two or more bombs/treasures in the same spot (using an "if" condition in a for loop).. Then start your game... The rest of your code would be pretty much the same... (I had made a minesweeper game and used this, and I assume your game is quite similar to it...)

Hope this helps... :)
Last edited on
Thank you guys so much for the help and suggestions. I know it can be daunting trying to comprehend your own code sometimes, so I really do appreciate the time it took you guys to read and troubleshoot someone else's pretty sloppy code!

The problem is typo in line 155.

if(treasure[0][0] == x && bomb4[0][1] == y)

Should be

if(treasure[0][0] == x && treasure[0][1] == y)


On line 112 I think you wanted char grid[HEIGHT][WIDTH]
Just as a note, you might want to look up enums.
lol vin .... i just saw that in my console.... thanks for finding that!!

and thanks for the lookup tip on enum;s, that something completely new to me.
and to be honest i thought it would be easily thrown together after creating the grid, but not so much. I believe ill do a better job using player/bomb/treasure objects.
one last question for you intelligent people. how do you think i should go about making sure i dont put two items into the same coordinates?
1) You could store those co-ordinates in an array, and then before you place anything on any co-ordinates, you first check through the array, to see if there's already something there or no..
2) Or, if you've initialized your grid to a certain character (like a "o" or a " "(blank)), you could do something like this:
1
2
3
4
5
6
7
int x, y;
while(grid[x][y]!='o')   //(or blank) 'This will keep searching for an empty spot on your grid...
{
   x=rand()%16;
   y=rand()%16;
}
grid[x][y]=BOMB;  //Or whatever you wanna place here.. 
Last edited on
What Caprico suggested, store locations in an array, then use nested for-loops

1
2
3
4
5
6
7
8
9
10
11
12
13
const int MAX = 5;
bombLocations[ MAX ][ MAX ];

for( int i = 0; i < MAX; ++i  )
{
    //don't compair the current element against it's self( j = 1 )
    for( int j = 1; j < MAX; ++j )
    {
        //compair x and y values together
        if( bombLocations[ i ][ i ] == bombLocations[ j ][ j ] )
            /* code to replace location */
    }
}


EDIT:
You may want to throw a bool in there. If there is a duplicate location, you'll want to reassign a location and then force a recheck to make sure that the re-assigned location is not another duplicate.
Last edited on
awesome guys. adding array right now. ill post my code in a codepad link when im finished. it works right now but with the chance of dupes.
http://codepad.org/vXf7UJnn

thank you mathhead, caprico, and lynx for your help. My code i ended up using wasnt as elegant looking as you fella, but it seems to be getting the job done.

comments welcome.
You could make a struct for the bombs and save to the collection at the same time. I just got bored and wrote it... lol

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

#define WIDTH 20
#define HEIGHT 20

//create a struct to hold bombs x/y values
struct bomb
{
	int x;
	int y;
};


int main()
{
	//seed the random number generator with time
	srand( unsigned( time( NULL ) ) );

	//change this to make less/more bombs
	const int MAXBOMBS = 5;

	int collection[ MAXBOMBS ][ 2 ];

	//create an array of bombs
	bomb bombs[ MAXBOMBS ];

	//init with random values
	for( int i = 0; i < MAXBOMBS; ++i )
	{
		bombs[ i ].x = rand() % WIDTH + 1;
		bombs[ i ].y = rand() % HEIGHT + 1;

		//save to the collection...
		collection[ i ][ 0 ] = bombs[ i ].x;
		collection[ i ][ 1 ] = bombs[ i ].y;
	}

	//view bombs
	for( int i = 0; i < MAXBOMBS; ++i )
	{
		std::cout << "Bomb " << i + 1 << "\tx: " << bombs[ i ].x << "\ty: " << bombs[ i ].y << '\n';
		std::cout << "Coll " << i + 1 << "\tx: " << collection[ i ][ 0 ] << "\ty: " << collection[ i ][ 1 ] << "\n\n";
	}
}


Also, you define these:
1
2
#define WIDTH 16
#define HEIGHT 16 

You could just:
#define BOX 16

I know it's nit picking, but... lol
Last edited on
Maybe also exclude player start position as a possible location for bomb or treasure.
Pages: 12