due tommrow quick help program not ending when its suppose to.
easy game i made but this is suppose to end when all treasures '$' are found, sometimes it ends and sometimes it does not.
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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int sta=1;
int end=5;
int gold[10][2];
char board[5][5];
int search(int x, int y){
for(int j=0; j<10; j++){
if(x==gold[j][0] && y==gold[j][1])
return 1;
}
return 0;
}
void setup(){
int x=0;
int y=0;
int count=1;
for(int i=0; i<5; i++){
for(int w=0; w<5; w++){
board[i][w]='#';
}
}
for(int i=0; i<10; i++){
gold[i][0]=0;
gold[i][1]=0;
}
while (count<10) {
x=rand()%(end-sta+1)+sta;
y=rand()%(end-sta+1)+sta;
if (search(x,y) == 0)
{
gold[count][0]=x;
gold[count][1]=y;
count++;
}
}
}
void output(){
cout<<" 1 2 3 4 5"<<endl;
for(int i=0; i<5; i++){
cout<<i+1<<" ";
for(int n=0; n<5; n++){
cout<<board[i][n]<<" ";
}
cout<<endl<<endl;
}
}
void play(){
int x,y;
int goldfound=0;
int numguess=0;
int guess=0;
while(goldfound!=10){
cout<<"Enter X coordinates"<<endl;
cin>>x;
cout<<"Enter Y coordinates"<<endl;
cin>>y;
guess=search(x,y);
numguess++;
if (guess==1){
goldfound++;
board[x-1][y-1]='$';
}
else
board[x-1][y-1]=' ';
output();
}
cout<<endl;
cout<<"Guesses taken: "<<numguess<<" guesses";
}
int main(){
srand(time(0));
int x,y,numguesses;
setup();
cout<<endl;
output();
play();
return 0;
}
|
You're placing only 9 gold in the game, but ending when 10 gold are found.
count is 1, and loops till <10 (meaning till 9).
Either initialize count to zero at line 29.
Or end the game when goldfound is 9 at line 73.
Hope this helps.
Topic archived. No new replies allowed.