#include <stdio.h> #include <math.h> #include <time.h> #include <iostream> #include <windows.h> using namespace std; bool zmbexist(int sqarep[10][10], int posx, int posy) { if (sqarep[posx][posy]==0) return true; else return false; } int nrzombies(int plain[10][10]) { int i,j; int nrzmb; nrzmb =0 ; for (i=0;i<10;i++) for (j=0;j<10;j++) { if (plain[i][j] == 2) nrzmb++; } return nrzmb; } void display(int terain[10][10]) { system("cls"); cout << "\n"; for (int i=0;i<10;i++){ for (int j=0;j<10;j++) { if (terain[i][j] == 2) cout << " Z "; else if (terain[i][j] == 1) cout << " M "; else if (terain[i][j] == 3) cout << " H "; else if (terain[i][j] == -1) cout << " D "; else cout << " * "; } cout<<"\n"; } // Sleep(5000); } bool movezombies(int plain[10][10]) { cout <<"is in movezombie"; bool dead,zombiemove; int i,j,k; for (i=0;i<10;i++) { for (j=0;j<10;j++) { //make sure that all the zombie moves if (plain[i][j]==2) { zombiemove=false; while (!zombiemove){ int rndmove = rand()%4+1; cout << rndmove << "\n"; // Sleep(1000); cout << "now move zombie \n"; //move up if is possible if (rndmove == 1) { cout << "move up \n"; if (i>1) { if (plain[i-1][j]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i-1][j]==1) { dead = true; return true; zombiemove=true; } else if (plain[i-1][j]==0) { plain[i][j]=0; plain[i-1][j]=5; zombiemove=true; break; } } } //move down if is possible if (rndmove == 2) { cout << "move down \n"; if (i<9) { if (plain[i+1][j]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i+1][j]==1) { dead = true; return true; zombiemove=true; true; } else if (plain[i+1][j]==0) { plain[i][j]=0; plain[i+1][j]=5; zombiemove=true; break; } } } //move left if is possible if (rndmove == 3) { cout << "move left \n"; if (j>1) { if (plain[i][j-1]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i][j-1]==1) { dead = true; zombiemove=true; return true; } else if (plain[i][j-1]==0) { plain[i][j]=0; plain[i][j-1]=5; zombiemove=true; break; } } } //move right if is possible if (rndmove == 4) { cout << "move right \n"; if (j<9) { if (plain[i][j+1]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i][j+1]==1) { dead = true; return true; zombiemove=true; } else if (plain[i][j+1]==0) { plain[i][j]=0; plain[i][j+1]=5; zombiemove=true; break ; } } } }//end while } } } for (i=0;i<10;i++) for (j=0;j<10;j++) { if (plain[i][j]==5) plain[i][j]=2; } return dead; } bool movehuman(int plain[10][10]) { int move; bool dead; bool pmove = false; int x,y,i,j; for (i=0;i<10;i++) for (j=0;j<10;j++) { if ((plain[i][j]) == 1){x=i;y=j;} } while (!pmove){ display(plain); cout << "move human (2=down,8=up,4=left,6=right): "; move = getch(); cout<< move; if (move ==52) { if (y>0) { if ((plain[x][y-1]==3)||(plain[x][y-1]==2)) { cout<<"is mort acuma2"; Sleep(2000); dead = true; return dead; } plain[x][y]=0; plain[x][y-1]=1; y--; pmove=true; } } if (move ==54) { if (y<9) { if ((plain[x][y+1]==3)||(plain[x][y+1]==2)) { dead = true; return dead; } plain[x][y]=0; plain[x][y+1]=1; y++; pmove=true; } } if (move ==56) { if (x>0) { if ((plain[x-1][y]==3) ||(plain[x-1][y]==2)) { dead = true; return dead; } plain[x][y]=0; plain[x-1][y]=1; x--; pmove=true; } } if (move ==50) { if (x<9) { if ((plain[x+1][y]==3) ||(plain[x+1][y]==2)) { dead = true; return dead; } plain[x][y]=0; plain[x+1][y]=1; x++; pmove=true; } } } return dead; } bool play(int nrz,int nrholes,int plain[10][10]) { bool dead,endgame; endgame = false; dead = false; while (true) { endgame = movezombies(plain); dead = movehuman(plain); nrz = nrzombies(plain); if (dead) {cout << "human dead" ; break;} if (nrz==0){ cout << "human win" ;break;} } } int main() { srand (time(NULL)); int vectx[10][10]; //initialise the vector for (int i=0;i<10;i++) for (int j=0;j<10;j++) { vectx[i][j]=0; } //convention : hole number is 3 , zombie number is 2, and human number is 1; //ask for nr of holes int nrholes; cout << "number of holes(0 for random) : "; cin >> nrholes; //if type 0 , then the nr of holes is generated automaticaly if (nrholes == 0) nrholes = rand() % 10 + 1; //place the holes int i=0; int xhol,yhol; while (i<nrholes) { xhol = rand()%10 + 1; yhol = rand()%10 + 1; if (vectx[xhol][yhol]==0) { srand (time(NULL)); vectx[xhol][yhol]=3; i++; } } //ask for nr of zombies int nrzombies; cout << "number of zombies(0 for random) : "; cin >> nrzombies; //if nr is 0, then is generated automaticaly if (nrzombies == 0) nrzombies = rand() % 10 + 1; i=0; while (i<nrzombies) { xhol = rand()%9 + 1; yhol = rand()%9 + 1; if (vectx[xhol][yhol]==0) { srand (time(NULL)); vectx[xhol][yhol]=2; i++; } } i=0; cout << "now we place the man"; while (i<1) { xhol = rand()%10 + 1; yhol = rand()%10 + 1; if (vectx[xhol][yhol]==0) { srand (time(NULL)); vectx[xhol][yhol]=1; i++; } } display(vectx); play(nrzombies,nrholes,vectx); getch(); }
Describe your problem clearly and fully!
So you have a great title and you're ready to compose a post. Resist the temptation to copy and paste your code and as an afterthought say "It doesn't work". That's not a description of the problem. If you want help, you have to give us enough information to work with. Include at least all of the following in your description of the problem:
1) An overview of what your program does.
2) The result of your current code.
3) What you expected your code to do.
4) The contents of any input files (if appropriate)
This tells us what context your program is in so that we can offer alternative solutions. It tells us exactly what your code does so that we can more easily pinpoint the problem without performing tedious troubleshooting steps. Finally, it tells us what you wanted your code to do so that we can compare what it does with what you wanted it to do. Most of the time, a knowledgeable member can answer your question almost immediately using just this information.
Keep the code short and sweet!
As much as we don't care, people insist on posting pages and pages of irrelevant code. Keep the code as short as possible. If the code isn't short, make it short by cutting out as much irrelevant code as possible. Ideally, we expect you to write a small test program that exhibits the problem you're having.
The benefit of this is twofold. First, you might end up solving the problem yourself. Second, if you can't solve the problem yourself, there's less code that we have to look at and the problem is more likely to jump out at us.
Use code tags!
There are two ways to wrap code in code tags. The easiest way to do this is to select all of your code and then click the # button on the message editor. This will automatically wrap the selected text in code tags. Without code tags, all leading whitespace will be removed from the code, so nicely formatted code ends up having no indentation. This makes the code very hard to read and many members will refuse to help you because they can't read your code.
#include <stdio.h> #include <math.h> #include <time.h> #include <iostream> #include <windows.h> using namespace std; bool zmbexist(int sqarep[10][10], int posx, int posy) { if (sqarep[posx][posy]==0) return true; else return false; } int nrzombies(int plain[10][10]) { int i,j; int nrzmb; nrzmb =0 ; for (i=0;i<10;i++) for (j=0;j<10;j++) { if (plain[i][j] == 2) nrzmb++; } return nrzmb; } void display(int terain[10][10]) { system("cls"); cout << "\n"; for (int i=0;i<10;i++){ for (int j=0;j<10;j++) { if (terain[i][j] == 2) cout << " Z "; else if (terain[i][j] == 1) cout << " M "; else if (terain[i][j] == 3) cout << " H "; else if (terain[i][j] == -1) cout << " D "; else cout << " * "; } cout<<"\n"; } // Sleep(5000); } bool movezombies(int plain[10][10]) { cout <<"is in movezombie"; bool dead,zombiemove; int i,j,k; for (i=0;i<10;i++) { for (j=0;j<10;j++) { //make sure that all the zombie moves if (plain[i][j]==2) { zombiemove=false; while (!zombiemove){ int rndmove = rand()%4+1; cout << rndmove << "\n"; // Sleep(1000); cout << "now move zombie \n"; //move up if is possible if (rndmove == 1) { cout << "move up \n"; if (i>1) { if (plain[i-1][j]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i-1][j]==1) { dead = true; return true; zombiemove=true; } else if (plain[i-1][j]==0) { plain[i][j]=0; plain[i-1][j]=5; zombiemove=true; break; } } } //move down if is possible if (rndmove == 2) { cout << "move down \n"; if (i<9) { if (plain[i+1][j]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i+1][j]==1) { dead = true; return true; zombiemove=true; true; } else if (plain[i+1][j]==0) { plain[i][j]=0; plain[i+1][j]=5; zombiemove=true; break; } } } //move left if is possible if (rndmove == 3) { cout << "move left \n"; if (j>1) { if (plain[i][j-1]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i][j-1]==1) { dead = true; zombiemove=true; return true; } else if (plain[i][j-1]==0) { plain[i][j]=0; plain[i][j-1]=5; zombiemove=true; break; } } } //move right if is possible if (rndmove == 4) { cout << "move right \n"; if (j<9) { if (plain[i][j+1]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i][j+1]==1) { dead = true; return true; zombiemove=true; } else if (plain[i][j+1]==0) { plain[i][j]=0; plain[i][j+1]=5; zombiemove=true; break ; } } } }//end while } } } for (i=0;i<10;i++) for (j=0;j<10;j++) { if (plain[i][j]==5) plain[i][j]=2; } return dead; } bool movehuman(int plain[10][10]) { int move; bool dead; bool pmove = false; int x,y,i,j; for (i=0;i<10;i++) for (j=0;j<10;j++) { if ((plain[i][j]) == 1){x=i;y=j;} } while (!pmove){ display(plain); cout << "move human (2=down,8=up,4=left,6=right): "; move = getch(); cout<< move; if (move ==52) { if (y>0) { if ((plain[x][y-1]==3)||(plain[x][y-1]==2)) { cout<<"is mort acuma2"; Sleep(2000); dead = true; return dead; } plain[x][y]=0; plain[x][y-1]=1; y--; pmove=true; } } if (move ==54) { if (y<9) { if ((plain[x][y+1]==3)||(plain[x][y+1]==2)) { dead = true; return dead; } plain[x][y]=0; plain[x][y+1]=1; y++; pmove=true; } } if (move ==56) { if (x>0) { if ((plain[x-1][y]==3) ||(plain[x-1][y]==2)) { dead = true; return dead; } plain[x][y]=0; plain[x-1][y]=1; x--; pmove=true; } } if (move ==50) { if (x<9) { if ((plain[x+1][y]==3) ||(plain[x+1][y]==2)) { dead = true; return dead; } plain[x][y]=0; plain[x+1][y]=1; x++; pmove=true; } } } return dead; } bool play(int nrz,int nrholes,int plain[10][10]) { bool dead,endgame; endgame = false; dead = false; while (true) { endgame = movezombies(plain); dead = movehuman(plain); nrz = nrzombies(plain); if (dead) {cout << "human dead" ; break;} if (nrz==0){ cout << "human win" ;break;} } } int main() { srand (time(NULL)); int vectx[10][10]; //initialise the vector for (int i=0;i<10;i++) for (int j=0;j<10;j++) { vectx[i][j]=0; } //convention : hole number is 3 , zombie number is 2, and human number is 1; //ask for nr of holes int nrholes; cout << "number of holes(0 for random) : "; cin >> nrholes; //if type 0 , then the nr of holes is generated automaticaly if (nrholes == 0) nrholes = rand() % 10 + 1; //place the holes int i=0; int xhol,yhol; while (i<nrholes) { xhol = rand()%10 + 1; yhol = rand()%10 + 1; if (vectx[xhol][yhol]==0) { srand (time(NULL)); vectx[xhol][yhol]=3; i++; } } //ask for nr of zombies int nrzombies; cout << "number of zombies(0 for random) : "; cin >> nrzombies; //if nr is 0, then is generated automaticaly if (nrzombies == 0) nrzombies = rand() % 10 + 1; i=0; while (i<nrzombies) { xhol = rand()%9 + 1; yhol = rand()%9 + 1; if (vectx[xhol][yhol]==0) { srand (time(NULL)); vectx[xhol][yhol]=2; i++; } } i=0; cout << "now we place the man"; while (i<1) { xhol = rand()%10 + 1; yhol = rand()%10 + 1; if (vectx[xhol][yhol]==0) { srand (time(NULL)); vectx[xhol][yhol]=1; i++; } } display(vectx); play(nrzombies,nrholes,vectx); getch(); }
bool play(int nrz,int nrholes,int plain[10][10]) { bool dead,endgame; endgame = false; dead = false; while (true) { endgame = movezombies(plain); dead = movehuman(plain); nrz = nrzombies(plain); if (dead) {cout << "human dead" ; break;} if (nrz==0){ cout << "human win" ;break;} } }
ok, i inlcuded <conio.h>
when i run it now, i get this error:
error C4716: 'play' : must return a value
what do i do to solve this?
the error lies in this part of the code
bool play(int nrz,int nrholes,int plain[10][10])
{
bool dead,endgame;
endgame = false;
dead = false;
while (true)
{
endgame = movezombies(plain);
dead = movehuman(plain);
nrz = nrzombies(plain);
if (dead) {cout << "human dead" ; break;}
if (nrz==0){ cout << "human win" ;break;}
}
}
bool movezombies(int plain[10][10]) { cout <<"is in movezombie"; bool dead,zombiemove; int i,j,k; for (i=0;i<10;i++) { for (j=0;j<10;j++) { //make sure that all the zombie moves if (plain[i][j]==2) { zombiemove=false; while (!zombiemove){ int rndmove = rand()%4+1; cout << rndmove << "\n"; Sleep(1000); cout << "now move zombie \n"; //move up if is possible if (rndmove == 1) { cout << "move up \n"; if (i>1) { if (plain[i-1][j]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i-1][j]==1) { dead = true; return true; zombiemove=true; } else if (plain[i-1][j]==0) { plain[i][j]=0; plain[i-1][j]=5; zombiemove=true; break; } } } //move down if is possible if (rndmove == 2) { cout << "move down \n"; if (i<9) { if (plain[i+1][j]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i+1][j]==1) { dead = true; return true; zombiemove=true; true; } else if (plain[i+1][j]==0) { plain[i][j]=0; plain[i+1][j]=5; zombiemove=true; break; } } } //move left if is possible if (rndmove == 3) { cout << "move left \n"; if (j>1) { if (plain[i][j-1]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i][j-1]==1) { dead = true; zombiemove=true; return true; } else if (plain[i][j-1]==0) { plain[i][j]=0; plain[i][j-1]=5; zombiemove=true; break; } } } //move right if is possible if (rndmove == 4) { cout << "move right \n"; if (j<9) { if (plain[i][j+1]==3) { plain[i][j]=0; zombiemove=true; break; } else if (plain[i][j+1]==1) { dead = true; return true; zombiemove=true; } else if (plain[i][j+1]==0) { plain[i][j]=0; plain[i][j+1]=5; zombiemove=true; break ; } } } }//end while } } } for (i=0;i<10;i++) for (j=0;j<10;j++) { if (plain[i][j]==5) plain[i][j]=2; } return dead; } bool movehuman(int plain[10][10]) { int move; bool dead; bool pmove = false; int x,y,i,j; for (i=0;i<10;i++) for (j=0;j<10;j++) { if ((plain[i][j]) == 1){x=i;y=j;} } while (!pmove){ display(plain); cout << "move human (2=down,8=up,4=left,6=right): "; move = getch(); cout<< move; if (move ==52) { if (y>0) { if ((plain[x][y-1]==3)||(plain[x][y-1]==2)) { cout<<"is mort acuma2"; Sleep(2000); dead = true; return dead; } plain[x][y]=0; plain[x][y-1]=1; y--; pmove=true; } } if (move ==54) { if (y<9) { if ((plain[x][y+1]==3)||(plain[x][y+1]==2)) { dead = true; return dead; } plain[x][y]=0; plain[x][y+1]=1; y++; pmove=true; } } if (move ==56) { if (x>0) { if ((plain[x-1][y]==3) ||(plain[x-1][y]==2)) { dead = true; return dead; } plain[x][y]=0; plain[x-1][y]=1; x--; pmove=true; } } if (move ==50) { if (x<9) { if ((plain[x+1][y]==3) ||(plain[x+1][y]==2)) { dead = true; return dead; } plain[x][y]=0; plain[x+1][y]=1; x++; pmove=true; } } } return dead; }
| DaniWeb Message | |
| Cancel Changes | |