May 29, 2018 at 9:00pm May 29, 2018 at 9:00pm UTC
i made a program and i cant find root of problem. Code is
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
#include<iostream>
#include <cstdlib>
#include<conio.h>
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;
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;
}
else if (i == dr&&j == dc){
cout << " " << d;
}
else if (i == dr2 && j == dc2){
cout << " " << d;
}
else if (i == br&&j == bc){
cout << " " << b;
}
else if (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.
Last edited on May 29, 2018 at 9:00pm May 29, 2018 at 9:00pm UTC
May 30, 2018 at 8:31am May 30, 2018 at 8:31am UTC
I think what's happen is that when you take the diamond the bomb happens to be moved right were the player is so you lose then right away.
Last edited on May 30, 2018 at 8:36am May 30, 2018 at 8:36am UTC