#include<iostream>
#include <cstdlib>
#include<conio.h>
usingnamespace 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;
while (score != 25){
if (pc == dc && pr == dr) {
score = score + 1;
int newdc = rand() % 5;
int newdr = rand() % 5;
while (newdc == dc || newdc == bc || newdc == br) newdc = rand() % 5;
while (newdr == dr || newdr == bc || newdr == br) newdr = rand() % 5;
dc = newdc;
dr = newdr;
int newbc = rand() % 5;
int newbr = rand() % 5;
while (newbc == bc || newbc == dc || newbc == dr || newbc == dr2 || newbc == dc2) newbc = rand() % 5;
while (newbr == br || newbr == dc || newbr == dr || newbr == dc2 || newbr == dr2)newbr = rand() % 5;
br = newbr;
bc = newbc;
}
if (pc == dc2&& pr == dr2) {
score = score + 1;
int newdc2 = rand() % 5;
int newdr2 = rand() % 5;
while (newdc2 == dc2 || newdc2 == bc || newdc2 == br) newdc2 = rand() % 5;
while (newdr2 == dr2 || newdr2 == bc || newdr2 == br) newdr2 = rand() % 5;
dc2 = newdc2;
dr2 = newdr2;
int newbc = rand() % 5;
int newbr = rand() % 5;
while (newbc == bc || newbc == dc || newbc == dr || newbc == dr2 || newbc == dc2) newbc = rand() % 5;
while (newbr == br || newbr == dc || newbr == dr || newbr == dc2 || newbr == dr2)newbr = rand() % 5;
br = newbr;
bc = newbc;
}
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;
}
elseif (i == dr&&j == dc){
cout << " " << d;
}
elseif (i == dr2 && j == dc2){
cout << " " << d;
}
elseif (i == br&&j == bc){
cout << " " << b;
}
elseif (i == br2&&j == bc2){
cout << " " << b1;
}
else{
cout << " *";
}
}
cout << endl;
}
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;
}
}
if (score == 25){
cout << "Your score is= " << score;
exit(0);
}
}
}
when i move the player to 4x4 diamond the one in the last row. it collides and game gets over. i cant figure out why. i dried run it and i cant find the problem. And second problem i am facing is when i pick a diamond it randmoize 1st bomb.it should also happen with second bomb. When i add the same random fucntion as i did for bomb1 the program just dont run. it hangs. I will be really thankful for your help.