Unknown Error In Bomber Game

I am having this unknown error in my code for a bomber game that I am making for a project. The code works and I've assigned a function for randomizing two diamond characters '$' each time the game is played. I've used if condition for if the diamond lands on the player '&' and also if they overlap each other. But when I run the code, most of the time it comes out perfectly, I get two diamonds randomly. But sometimes, I get only one diamond and the score increases to a random number EVEN THOUGH THE SCORE IS JUST BEING DECLARED IN MAIN FUNCTION AND IS BEING DISPLAYED IN THE DISPLAY FUNCTION. It's really weird how the score is being increased and one diamond is just missing. I've attached my code to this post.

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

using namespace std;

void randGenDiamond ( char grid5x5 [5][5] , char diamond );
void displayGame ( char grid5x5 [5][5] , int gridSize , char player , char diamond , char bomb , int &score );

int main ()
{
	
	srand(time(0));
	
	const int gridSize = 5;
	char grid5x5 [5][5] = {{'*','*','*','*','*'},{'*','*','*','*','*'},{'*','*','*','*','*'},{'*','*','*','*','*'},{'*','*','*','*','*'}};
	char player = '&';
	char diamond = '$';
	char bomb = '%';
	int score = 0;
	
	grid5x5 [2][2] = player;
	
	randGenDiamond ( grid5x5 , diamond );
	
	displayGame ( grid5x5 , gridSize , player , diamond , bomb , score );
	
}

void randGenDiamond ( char grid5x5 [5][5] , char diamond )
{
	
	int x = rand() % 5;
	int y = rand() % 5;
	int m = rand() % 5;
	int n = rand() % 5;
	
	if ( grid5x5 [x][y] != grid5x5[2][2] && grid5x5 [m][n] != grid5x5 [2][2] && x != m && y != n )
	{
		
		grid5x5 [x][y] = diamond;
		grid5x5 [m][n] = diamond;
		
	}
	else
	{
	
		grid5x5 [x+1][y-1] = diamond;
		grid5x5 [x-1][y+1] = diamond;
	
	}
	
}

void displayGame ( char grid5x5 [5][5] , int gridSize , char player , char diamond , char bomb , int &score )
	{
		
		cout << endl;
		
		cout << "\n ======== Bomber Game ========\n\n\n";
		
		for ( int x = 0 ; x < gridSize ; x++ )
		{
			
			cout << "    ";
			
			for ( int y = 0 ; y < gridSize ; y++ ) 
			{
				
				cout << "   " << grid5x5 [x][y];
				
			}
			
			cout << endl << endl;
			
		}
		
		cout << "\n -----------------------------\n";
		cout << "           Score: " << score;
		cout << "\n -----------------------------\n\n";
		
	}
Last edited on
What do you think will happen on line 48 if x is 4 or y is 0?
What do you think will happen on line 49 if x is 0 or y is 4?
I've done this but could you please tell me if this is logically correct? if not, what can I do to correct my ugly 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
void randGenDiamond ( char grid5x5 [5][5] , char diamond )
{
	
	int x = rand() % 5;
	int y = rand() % 5;
	int m = rand() % 5;
	int n = rand() % 5;
	
	if ( grid5x5 [x][y] != grid5x5[2][2] && grid5x5 [m][n] != grid5x5 [2][2] && ( x != m || y != n ) )
	{
		
		grid5x5 [x][y] = diamond;
		grid5x5 [m][n] = diamond;
		
	}
	else if ( x == m && y == n )
	{
	
		grid5x5 [1][1] = diamond;
		grid5x5 [3][3] = diamond;
	
	}
	
}


Also, if this is logically correct?

if ( grid5x5 [x][y] != grid5x5[2][2] && grid5x5 [m][n] != grid5x5 [2][2] && ( x != m || y != n ) )
I don't entirely follow what you are trying to do. However, FWIW, I think you would be better calling a function that placed a single point twice than a function that tries to place two points simultaneously and creates contorted logic trying to make sure that they didn't overlap either each other or some (hard-coded) point on the board.

You have also got too many "magic numbers". Think how many lines you would have to change if you ever had to change that 5 and that 2 ...
Could you please help me with my logic? I'm trying to create a bomber game where the game randomly generates 2 diamonds '$' and 2 bombs '%' in the 5x5 grid. But the problem I'm facing is what if the bombs or the diamonds positions overlap? what if one or all of them land on the player's position? This is what I'm trying to figure out.
your logic is fine. depends on what you are trying to do.
@muradyf,
My suggestion is that your function should generate ONE point, [x][y], not two. Compare what you are trying to place at that point with what is already there.

If you want two new positions then simply call the function twice.

Please note my earlier comments about magic numbers, and start thinking in terms of N x N, and not 5 x 5.
Could you please explain in a bit more detail? Presently, I have this. The thing is that, it works but sometimes, the Diamond or the Bomb doesn't get printed because all of the condition doesn't come out true. Please help me out.

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
void generateMap ( char grid5x5 [5][5] , int gridSize , char player , char diamond , char bomb , char &playerPos )
{
	
	int x1 = rand() % 5;  //Diamond 1 (x axis)
	int y1 = rand() % 5;  //Diamond 1 (y axis)
	int x2 = rand() % 5;  //Diamond 2 (x axis)
	int y2 = rand() % 5;  //Diamond 2 (y axis)
	
	int x3 = rand() % 5;  //Bomb 1 (x axis)
	int y3 = rand() % 5;  //Bomb 1 (y axis)
	int x4 = rand() % 5;  //Bomb 2 (x axis)
	int y4 = rand() % 5;  //Bomb 2 (y axis)
	
	
	findPlayerPos ( grid5x5 , gridSize , player , playerPos );
	
	
	//DIAMOND
	
	if (  grid5x5 [x1][y1] != playerPos &&  grid5x5 [x2][y2] != playerPos && ( x1 != x2 || y1 != y2 ) ) //not overlapping player
	{
		cout << "\n\nPRINTED DIAMOND";
		grid5x5 [x1][y1] = diamond;
		grid5x5 [x2][y2] = diamond;
		
	}
	else
	{
		
		if ( x1 > 1 && y1 > 1 && x2 > 1 && y2 > 1 )  //leftmost border overlap
		{
			cout << "\n\nOVERLAP 1!";
			grid5x5 [x1-1][y1-1] = diamond;
			grid5x5 [x2-2][y2-2] = diamond;
			
		}
		
		else if ( x1 < 4 && y1 < 4 && x2 < 4 && y2 < 4 )  //rightmost border overlap
		{
			cout << "\n\nOVERLAP 2!";
			grid5x5 [x1+1][y1+1] = diamond;
			grid5x5 [x2][y2+1] = diamond;
			
		}
		
	}
	
	
	
	//BOMB
	
	
	if (  grid5x5 [x3][y3] != playerPos &&  grid5x5 [x4][y4] != playerPos && ( x3 != x4 || y3 != y4 ) ) //not overlapping player
	{
		if ( grid5x5 [x3][y3] != diamond && grid5x5 [x4][y4] != diamond)
		{
			cout << "\n\nPRINTED BOMB";
			grid5x5 [x3][y3] = bomb;
			grid5x5 [x4][y4] = bomb;
		}
		else
		{
			if ( x3 > 1 && y3 > 1 && x4 > 1 && y4 > 1 )
			{
				cout << "\n\nOVERLAP 3.01!";
				grid5x5 [x3-1][y3-1] = bomb;
				grid5x5 [x4-2][y4-2] = bomb;
			}
			else if ( x3 < 4 && y3 < 4 && x4 < 4 && y4 < 4 )
			{
			
				cout << "\n\nOVERLAP 3.02!";
				grid5x5 [x3+1][y3+1] = bomb;
				grid5x5 [x4+2][y4+2] = bomb;
			
			}
		}
	}
	else
	{
		
		if ( x3 > 1 && y3 > 1 && x4 > 1 && y4 > 1 )  //leftmost border overlap
		{
			
			if ( grid5x5 [x3][y3] != diamond && grid5x5 [x4][y4] != diamond )
			{
				cout << "\n\nOVERLAP 3!";
				grid5x5 [x3-1][y3-2] = bomb;
				grid5x5 [x4-2][y4-1] = bomb;
			}
			else
			{
				cout << "\n\nOVERLAP 3.1";
				grid5x5 [x3-1][y3] = bomb;
				grid5x5 [x4][y4-2] = bomb;
			}
			
		}
		
		else if ( x3 < 4 && y3 < 4 && x4 < 4 && y4 < 4 )  //rightmost border overlap
		{
			
			if ( grid5x5 [x3][y3] != diamond && grid5x5 [x4][y4] != diamond )
			{
				cout << "\n\nOVERLAP 4!";
				grid5x5 [x3+1][y3+1] = bomb;
				grid5x5 [x4+2][y4+2] = bomb;
			}
			else
			{
				cout << "\n\nOVERLAP 4.1";
				grid5x5 [x3+1][y3] = bomb;
				grid5x5 [x4][y4+2] = bomb;
			}
			
		}
		
	}
	
	
}
Muradyf,
Make one function to create one point at a time. Then run your 'if statement'.
1
2
3
4
5
6
flow: 
1. Function: make_point() 
2. A = make_point()
3. B = make_point()
    while B == A{ B = make_point()}
etc... 


For you 'if' condition, I suggest writing them out.
exp: if ( grid5x5 [x1][y1] != playerPos && grid5x5 [x2][y2] != playerPos && ( x1 != x2 || y1 != y2 ) )
1
2
3
if ( True &&  True && ( False || True ) ): = True
or
if ( False &&  True && ( True || True ) ): = False

Could you please help me with this error I'm getting?

error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
moveUp ( grid5x5 , gridSize , player , diamond , bomb , score , playerPos , input , player_x , player_y , end , temp );

note: initializing argument 11 of 'void moveUp(char (*)[5], int, char, char, char, int&, char, char, int&, int&, int&, char)'
void moveUp ( char grid5x5 [5][5] , int gridSize , char player , char diamond , char bomb , int &score , char playerPos , char input , int &player_x , int &player_y , int &end , char temp )
The error message gives you plenty of information. The 11th argument of void moveUp( ) - which, incidentally, you haven't bothered to provide - requires you to have an integer variable into which you can put something. At a rough guess you've called it with some fixed value ... but we'll never know because you haven't provided relevant code.
Here's the code for moveUp and moveDown.

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
void moveUp ( char grid5x5 [5][5] , int gridSize , char player , char diamond , char bomb , int &score , char playerPos , char input , int &player_x , int &player_y , int &end , char temp )
{
	
	if ( grid5x5 [player_x-1][player_y] == diamond )
	{
		grid5x5 [player_x-1][player_y] = player;
		resetArray ( grid5x5 , gridSize );
		generateMap ( grid5x5 , gridSize , player , diamond , bomb , playerPos );
	}
	
	else if ( grid5x5 [player_x-1][player_y] == bomb )
	{
		cout << "\n     GAME OVER!\n";
		end = 1;
	}
	
	else
	{
		temp = grid5x5 [player_x][player_y];
		grid5x5 [player_x][player_y] = grid5x5 [player_x-1][player_y];
		grid5x5 [player_x-1][player_y] = temp;
	}
	
}

void moveDown ( char grid5x5 [5][5] , int gridSize , char player , char diamond , char bomb , int &score , char playerPos , char input , int &player_x , int &player_y , int &end , char temp )
{
	
	if ( grid5x5 [player_x+1][player_y] == diamond )
			{
				grid5x5 [player_x+1][player_y] = player;
				resetArray ( grid5x5 , gridSize );
				generateMap ( grid5x5 , gridSize , player , diamond , bomb , playerPos );
			}
			
			else if ( grid5x5 [player_x+1][player_y] == bomb )
			{
				cout << "\n     GAME OVER!\n";
				end = 1;
			}
			
			else
			{
				temp = grid5x5 [player_x][player_y];
				grid5x5 [player_x][player_y] = grid5x5 [player_x+1][player_y];
				grid5x5 [player_x+1][player_y] = temp;
			}	
	
}
Here is where i'm calling the functions.

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 moveCharacter ( char grid5x5 [5][5] , int gridSize , char player , char diamond , char bomb , char playerPos , int &score )
{
	
	int player_x = 0 , player_y = 0;
	char input;
	char temp;
	char savePos;
	bool end = 0;
	
	findPlayerPos ( grid5x5 , gridSize , player , playerPos , player_x , player_y );
	
	
	while ( end == 0 )
	{
		
		end = 0;
		
		cout << "\n Use wasd to move: ";
		cin >> input;
		
		if ( input == 'w' )
		{
			moveUp ( grid5x5 , gridSize , player , diamond , bomb , score , playerPos , input , player_x , player_y , end , temp );
		}
		else if ( input == 's' )
		{
			moveUp ( grid5x5 , gridSize , player , diamond , bomb , score , playerPos , input , player_x , player_y , end , temp );
		}
		
		displayGame ( grid5x5 , gridSize , score );
		
	}
	
}
The function expects an integer variable ... and, as a reference, must be able to put something back as an integer.

You are calling it, however, with a bool, not an int.

Given the way that you use it, I think you would be better making end a bool variable throughout (i.e. in the function as well as the calling routine). At that point you might as well use true and false, rather than 1 and 0.
Last edited on
Topic archived. No new replies allowed.