Why is bomb getting out of grid.

Hey
this is the game i made.
I run the program but after picking 2 diamonds bomb get off grid and i cant figure out why.
Here is 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
161
162
163
164
165
166
167
168
169
170
171
172
#include<iostream>
#include <cstdlib>  
#include<conio.h>
#include<ctime>
using namespace std;
int main(){
	int r = 5, c = 5;;
	char player = 'P';
	char choice = 0;
	int dr = 1, dc = 1;//diamond in row //dimond in column
	int dr2 = 4, dc2 = 4;
	int br = 2, bc = 2;
	int br2 = 4, bc2 = 1;
	int pr = 0, pc = 0;
	int score = 0;
	char d = 'D';
	char d1 = 'D';
	char b = 'B';
	char b1 = 'B';
	int p = 0;
	srand(time(0));

	while (score != 25){




		if (pc == dc && pr == dr) {
			score = score + 1;

			int newdc = rand() % 5;
			int newdr = rand() % 5;
			int newbc = rand() % 5, newbr = rand() % 5, newbc2 = rand() % 5, newbr2 = rand() % 5;

			while (newdc == dc || newdc == dc2 || newdc == bc || newdc == bc2 || newdc == pc
				|| newdr == dr || newdr == dr2 || newdr == br || newdr == br2 || newdr == pr
				|| newbc == bc || newbc == bc2 || newbc == dc || newbc == dc2 || newbc == pc
				|| newbr == br || newbr == br2 || newbr == dr || newbr == dr2 || newbr == pr
				|| newbc2 == bc || newbc2 == bc2 || newbc2 == dc || newbc2 == dc2 || newbc2 == pc
				|| newbr2 == br || newbr2 == br2 || newbr2 == dr || newbr2 == dr2 || newbr2 == pr) {

				newdc = rand() % 5;
				newdr = rand() % 5;
				newbc = rand() % 5, newbr = rand() % 5, newbc2 = rand() % 5, newbr2 = rand() % 5;
			}
			dc = newdc;
			dr = newdr;
			bc = newbc;
			bc2 = newbc2;
			br = newbr;
			br2 = newbr2;

		}

		if (pc == dc2&& pr == dr2) {
			score = score + 1;
			int newdc2 = rand() % 5;
			int newdr2 = rand() % 5;
			int newbc = rand() % 5, newbr = rand() % 5, newbc2 = rand() % 5, newbr2 = rand() % 5;

			while (newdc2 == dc || newdc2 == dc2 || newdc2 == bc || newdc2 == bc2 || newdc2 == pc
				|| newdr2 == dr || newdr2 == dr2 || newdr2 == br || newdr2 == br2 || newdr2 == pr
				|| newbc == bc || newbc == bc2 || newbc == dc || newbc == dc2 || newbc == pc
				|| newbr == br || newbr == br2 || newbr == dr || newbr == dr2 || newbr == pr
				|| newbc2 == bc || newbc2 == bc2 || newbc2 == dc || newbc2 == dc2 || newbc2 == pc
				|| newbr2 == br || newbr2 == br2 || newbr2 == dr || newbr2 == dr2 || newbr2 == pr) {

				newdc2 = rand() % 5;
				newdr2 = rand() % 5;
				newbc = rand() % 5, newbr = rand() % 5, newbc2 = rand() % 5, newbr2 = rand() % 5;
			}

			dc2 = newdc2;
			dr2 = newdr2;
			bc = newbc;
			bc2 = newbc2;
			br = newbr;
			br2 = newbr2;
		}



		if (pc == bc&&pr == br){
			cout << "Game over due to collsion";
			cout << endl;
			cout << "Your score is= " << score;
			exit(0);
		}
		if (pc == bc2&&pr == br2){
			cout << "Game over dude to collsion";
			cout << endl;
			cout << "Your score is= " << score;
			exit(0);
		}



		for (int i = 0; i < r; i = i + 1){

			for (int j = 0; j < c; j = j + 1){

				if (i == pr&&j == pc){
					cout << " " << player;
				}
				else if (i == dr&&j == dc){
					cout << " " << d;
				}
				else if (i == dr2 && j == dc2){

					cout << " " << d1;
				}
				else if (i == br&&j == bc){
					cout << " " << b;
				}
				else if (i == br2&&j == bc2){
					cout << " " << b1;
				}
				else{
					cout << " *";
				}
			}
			cout << endl;
		}
		if (score == 25){
			cout << "Your score is= " << score;
			exit(0);
		}
		cout << "Your score is= " << score;
		cout << endl;



		cout << "Enter your choice= ";
		choice = _getche();
		cout << endl;
		if (choice == 'd' || choice == 'D'){
			pr = pr + 1;
			if (pr > 4){
				pr = pr - 1;
				cout << "Invalid movment";
				cout << endl;
			}
		}
		if (choice == 'r' || choice == 'R'){
			pc = pc + 1;
			if (pc > 4){
				pc = pc - 1;
				cout << "Invalid movment";
				cout << endl;
			}
		}
		if (choice == 'l' || choice == 'L'){
			pc = pc - 1;
			if (pc < 0){
				pc = pc + 1;
				cout << "Invalid movment";
				cout << endl;
			}
		}
		if (choice == 'u' || choice == 'U'){
			pr = pr - 1;
			if (pr < 0){
				pr = pr + 1;
				cout << "Invalid movment";
				cout << endl;
			}
		}



	}
}


I have checked all conditions that bomb should also not overlape with any other entry but where does it go after playing it two time.
If you will help me i will be grateful.
Last edited on
The loop is only checking the new positions against the previous positions, so if for example newbc == newbc2 and newbr == newbr2 the two bombs will be placed on top of each other.
Last edited on
Topic archived. No new replies allowed.