Dungeon Crawl type question

Pages: 12
back again! so im trying to create a point system, but so far the only idea i have is creating an array with enough space to store every possible coordinate and populate it with coords already landed on, then reference it for points. But the problem with that is the array size can grow to be quite large. Any other idea's, I know you fella's are genius's.
What exactly do you mean by points...? I'm sorry I didn't understand.. Can you explain a lil more..?
It's cool! Now that i think about it, "points".... pretty vague. My point system would include...

1) Every time a unique position on the board is reached, a point is added.

2) Change the marker from "#" to something else to show that the spot has been landed on.

3) Accumulate points and show to screen at every movement.
So what do you need help with! By the looks of it, that ^ right there, that's your algorithm for points, innit? Suppose your entire empty map is initialized to # as you say, the pseudo-code would be something like:

1
2
3
4
5
6
7
8
9
10
11
int points=0;

//After he enters his move
if (position of player is on a hash)
{
	(change '#' to another sign.. Say '*');
	points++;
}
/*In the top right corner (usual location for score)
You could allot the top-right cell of your array for this..*/
cout<<"Points: "<<points;


Does this serve your purpose, or where you looking for something else..?
thats exactly the kinda stuff i was looking for. thanks!
http://codepad.org/6XBvUpe7

so thats my new code, trying to make the bomb creation capable of handling different amounts of bombs... I seem to be creating the bombs alright, but my int check function is having serious problems catching bombs and sometimes treasures.

note: the game seems to be able to only find the last bomb in the array, if any
Last edited on
You can use the duplication function to find out if you hit/collide with something.

Here's something I wrote:
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
//total bombs
const int bMAX = 30;

//console size
const int wMAX = 80;
const int hMAX = 22;

//bomb coords
struct bombLocations
{
	int x;
	int y;
};

bool duplicate( bombLocations b[], int x, int y )
{
	for( int i = 0; i < bMAX; ++i )
		if( ( b[ i ].x == x ) && ( b[ i ].y == y ) )
				return true; //duplicate encountered

	return false;
}

void run()
{
	bombLocations bombs[ bMAX ];

	//init bomb locations all to null
	for( int i = 0; i < bMAX; ++i )
	{
		bombs[ i ].x = '\0';
		bombs[ i ].y = '\0';
	}

	//forward declaration, because the do-while
	//wont recognise variables made within the loop
	int x = 0;
	int y = 0;

	//populate locations with random numbers
	//while there are no duplicates
	for( int i = 0; i < bMAX; ++i )
	{
		do
		{
			x = randomRange( 0, wMAX - 1 );
			y = randomRange( 0, hMAX - 1 );

		}while( duplicate( bombs, x, y ) );

		bombs[ i ].x = x;
		bombs[ i ].y = y;
	}

	outputLocations( bombs );

	//create some random numbers and loop until 5
	//bombs are hit and print the location of the hits
	for( int i = 0; i < 5; ++i )
	{
		do
		{
			x = randomRange( 0, wMAX - 1 );
			y = randomRange( 0, hMAX - 1 );

		//loop until a duplicate( a bomb hit ) is found
		}while( ! duplicate( bombs, x, y ) );

		std::cout << "Bomb hit at: " << x << '\t' << y << '\n';
	}
}


If you pass the players location and it comes back as a duplicate, then you can do something else with the return value, rather than just using the function to find duplicates of the bombs.

This code is not the complete code, I've pasted snippets together. You can get the full code here:
http://sites.google.com/site/davevisone/home/cplusplus/random_cpp

Hope this helps in some way. (:
i really appreciate that lynx, thanks! I was really hoping that someone could help me to troubleshoot my existing code, to point out why my code doesn't accomplish what I want it to.
well i looked at my function... in a debugger this time(dur)

my check function would reset the value variable to whatever the last repetition was, instead of keeping track. heres the new check function, the rest of the code is the same

1
2
3
4
5
6
7
8
9
10
11
12
13
int check(int x,int y,int n){   //check function comparing player cords to item cords
  int value,i,b=0,t=0;
  for(i=0;i<n;i++){
    if(bomb_array[i][0] == x && bomb_array[i][1] == y)
      b++;
    if(bomb_array[n][0] == x && bomb_array[n][1] == y)
      t++;
  }
  if(b>0)value=BOMB;
    else if(t>0)value=TREASURE;
    else value=PASS;
 return value;
}
My next task is to integrate the point system that was discussed earlier. My first step is to allow for all spaces that have been landed to be shown with a different character than non-landed on spaces. Im having issues again....... sorry in advance for my code being so beginner-ish.

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
void grid(int x,int y){
  char grid[WIDTH][HEIGHT];
  int i=0,j=0,l=0;
  
  for(i=0;i<uniqlands;i++){
	  if(landed[i][0] == x && landed[i][1] == y)
	    l++;
  }
  if(l==0){
	uniqlands++;
    landed[uniqlands-1][0] = x;  
    landed[uniqlands-1][1] = y;
  }
  for(i=0;i<HEIGHT;i++){
     for(j=0;j<WIDTH;j++){
        grid[i][j] = 'e';
     }
  }
  for(i=0;i<uniqlands;i++){
	  grid[landed[i][0]-1][landed[i][1]-1] = '*';
  }
  grid[y-1][x-1] = 'P';
  for(i=0;i<25;i++){       //clear screen; quite portable
    cout << endl << endl;
  }

   for(i=0;i<HEIGHT;i++){
    for(j=0;j<WIDTH;j++){
      cout << grid[i][j];
    }
    cout << endl;
  }
  return;
}


and heres the snippet from decl/init the uniqlands variable and landed[ ]

1
2
3
4
5
6
int landed[256][2],uniqlands=0;  //list of cords already landed on, not to be displayed as normal, and counter for how many unique lands

int main(){
  int x_cord=1,y_cord=1,move=0,i,n=0;
  landed[0][0] = 1;landed[0][1] = 1;uniqlands++;  //starting position
...
Instead of the increments, you should just return ...;.

I'd do that instead, because there's no point in carrying on with a check if you're already on a bomb or treasure, because you're player won't be in more than one place.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int check(int x,int y,int n){   //check function comparing player cords to item cords
  int value,i,b=0,t=0;//no need for these, except i
  int i;
  for(i=0;i<n;i++){
    if(bomb_array[i][0] == x && bomb_array[i][1] == y)
      return BOMB;
    if(bomb_array[n][0] == x && bomb_array[n][1] == y)
      return TREASURE;
  }

  //no real need for these now, as it will return above
  //if(b>0)value=BOMB;
    //else if(t>0)value=TREASURE;
    //else value=PASS;
 //return value;

  //if it hasn't already returned by this point, then the return
  //has to be PASS
  return PASS;
}
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
void grid(int x,int y){
  char grid[WIDTH][HEIGHT];
  int i=0,j=0,l=0;
  
  for(i=0;i<uniqlands;i++){
	  if(landed[i][0] == x && landed[i][1] == y)
	    l++;
  }
  if(l==0){
	uniqlands++;
    landed[uniqlands-1][0] = x;  
    landed[uniqlands-1][1] = y;
  }
  l=0;                      //resets to 0, not sure if needed because l goes out scope and should "reset"
  for(i=0;i<HEIGHT;i++){
     for(j=0;j<WIDTH;j++){
        grid[i][j] = 'e';
     }
  }
  for(i=0;i<uniqlands;i++){
	  grid[landed[i][1]-1][landed[i][0]-1] = '*';     //swapped the x cord for the y cord, forgot thats the way the array is set up
  }
  grid[y-1][x-1] = 'P';
  for(i=0;i<25;i++){       //clear screen; quite portable
    cout << endl << endl;
  }

   for(i=0;i<HEIGHT;i++){
    for(j=0;j<WIDTH;j++){
      cout << grid[i][j];
    }
    cout << endl;
  }
  return;
}


thanks lynx, i changed it to have multiple return points and it helped to shorten my code.
Im having issues again.......

From looking at your code, it seems ok. What is the issue you're having?
Topic archived. No new replies allowed.
Pages: 12