| | |
errors in game code
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
hey, i have made a game called zombie island. this is what needs to be included in the game:
This is a simple turn based game.
Set up a 2d grid. Onto the grid randomly position holes, zombies and a man (each represented by a simple character e.g. O Z and M).
The man moves according to input from the player (suggest 4=left, 6=right, 8=up and 2=down).
The zombies should move in a random direction.
If the zombies land in a hole then they die and are removed from the game. If all the zombies die the man wins.
If the man lands in a hole or a zombie lands on the man then the man loses.
here is the code i have created:
errors are produced when this is compiled and ran. i am using visual c++ 2005 as my compiler, i would be very grateful for any help given. thank you.
ps. i submitted another thread about the same game, but that was for help with a more advanced version. this is for the basic version of it. sorry for any confusion for people looking at it.
This is a simple turn based game.
Set up a 2d grid. Onto the grid randomly position holes, zombies and a man (each represented by a simple character e.g. O Z and M).
The man moves according to input from the player (suggest 4=left, 6=right, 8=up and 2=down).
The zombies should move in a random direction.
If the zombies land in a hole then they die and are removed from the game. If all the zombies die the man wins.
If the man lands in a hole or a zombie lands on the man then the man loses.
here is the code i have created:
c Syntax (Toggle Plain Text)
#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(); }
errors are produced when this is compiled and ran. i am using visual c++ 2005 as my compiler, i would be very grateful for any help given. thank you.
ps. i submitted another thread about the same game, but that was for help with a more advanced version. this is for the basic version of it. sorry for any confusion for people looking at it.
Last edited by Ancient Dragon; Jul 22nd, 2008 at 5:26 pm. Reason: add code tags
You might wonder why your problem hasn't been answered. It is obvious that you haven't use the code tag which make it very unfriendly to read your code. I recommend you to tag your code and post your error messages.
Here is the key of having a successful question: (if you follow this guide correctly, I am sure that many people will answer your question)
Here is the key of having a successful question: (if you follow this guide correctly, I am sure that many people will answer your question)
•
•
•
•
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.
Last edited by invisal; Jul 22nd, 2008 at 4:01 pm.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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(); }
sorry about that.
the problem i'm getting is :
'getch': identifier not found
i have played around and tried lots of different things, but cannot seem to get it to run.
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
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;} } }
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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;}
}
}Play is a boolean function, so it needs to return true or false. If you don't want it to return anything, change it to a void function.
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
thanks, changing it to void makes it run now, apart from i get a few warnings.
the warnings i dont understand is:
warning C4700: uninitialized local variable 'dead' used
here is the part of the code that the error occurs in.
thank again.
the warnings i dont understand is:
warning C4700: uninitialized local variable 'dead' used
here is the part of the code that the error occurs in.
C++ Syntax (Toggle Plain Text)
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; }
thank again.
![]() |
Similar Threads
- "Craps", Game Help (C++)
- game code help (C++)
- Help with game! & invalid suffix etc (C++)
- help on connect 4 game (C)
- Dating Game (C++)
- guessing game program problem (C++)
- 2 errors (C++)
- help with few errors (C++)
- Question about card game (C++)
- java price is right game help (Java)
Other Threads in the C++ Forum
- Previous Thread: access serial device via network
- Next Thread: What is meaning of this C++ syntax?
Views: 693 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






