errors in game code

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 13
Reputation: redrum237 is an unknown quantity at this point 
Solved Threads: 0
redrum237 redrum237 is offline Offline
Newbie Poster

errors in game code

 
0
  #1
Jul 22nd, 2008
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:
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <time.h>
  4. #include <iostream>
  5. #include <windows.h>
  6. using namespace std;
  7. bool zmbexist(int sqarep[10][10], int posx, int posy)
  8. {
  9. if (sqarep[posx][posy]==0) return true;
  10. else return false;
  11.  
  12. }
  13. int nrzombies(int plain[10][10])
  14. {
  15. int i,j;
  16. int nrzmb;
  17. nrzmb =0 ;
  18. for (i=0;i<10;i++)
  19. for (j=0;j<10;j++)
  20. {
  21. if (plain[i][j] == 2) nrzmb++;
  22. }
  23. return nrzmb;
  24. }
  25. void display(int terain[10][10])
  26. {
  27. system("cls");
  28. cout << "\n";
  29. for (int i=0;i<10;i++){
  30. for (int j=0;j<10;j++)
  31. {
  32. if (terain[i][j] == 2) cout << " Z ";
  33. else
  34. if (terain[i][j] == 1) cout << " M ";
  35. else
  36. if (terain[i][j] == 3) cout << " H ";
  37. else
  38. if (terain[i][j] == -1) cout << " D ";
  39. else cout << " * ";
  40. }
  41. cout<<"\n";
  42. }
  43. // Sleep(5000);
  44. }
  45. bool movezombies(int plain[10][10])
  46. {
  47. cout <<"is in movezombie";
  48. bool dead,zombiemove;
  49. int i,j,k;
  50. for (i=0;i<10;i++)
  51. {
  52. for (j=0;j<10;j++)
  53. {
  54. //make sure that all the zombie moves
  55.  
  56. if (plain[i][j]==2)
  57. {
  58. zombiemove=false;
  59. while (!zombiemove){
  60. int rndmove = rand()%4+1;
  61. cout << rndmove << "\n";
  62. // Sleep(1000);
  63. cout << "now move zombie \n";
  64. //move up if is possible
  65.  
  66. if (rndmove == 1)
  67. {
  68. cout << "move up \n";
  69. if (i>1)
  70. {
  71. if (plain[i-1][j]==3)
  72. {
  73. plain[i][j]=0;
  74. zombiemove=true;
  75. break;
  76. } else
  77. if (plain[i-1][j]==1)
  78. {
  79. dead = true;
  80. return true;
  81. zombiemove=true;
  82.  
  83. } else
  84. if (plain[i-1][j]==0)
  85. {
  86. plain[i][j]=0;
  87. plain[i-1][j]=5;
  88. zombiemove=true;
  89. break;
  90.  
  91. }
  92. }
  93. }
  94. //move down if is possible
  95. if (rndmove == 2)
  96. {
  97. cout << "move down \n";
  98. if (i<9)
  99. {
  100. if (plain[i+1][j]==3)
  101. {
  102. plain[i][j]=0;
  103. zombiemove=true;
  104. break;
  105. } else
  106. if (plain[i+1][j]==1)
  107. {
  108. dead = true;
  109. return true;
  110. zombiemove=true;
  111. true;
  112. } else
  113. if (plain[i+1][j]==0)
  114. {
  115. plain[i][j]=0;
  116. plain[i+1][j]=5;
  117. zombiemove=true;
  118. break;
  119.  
  120. }
  121. }
  122. }
  123. //move left if is possible
  124. if (rndmove == 3)
  125. {
  126. cout << "move left \n";
  127. if (j>1)
  128. {
  129. if (plain[i][j-1]==3)
  130. {
  131. plain[i][j]=0;
  132. zombiemove=true;
  133. break;
  134. } else
  135. if (plain[i][j-1]==1)
  136. {
  137. dead = true;
  138. zombiemove=true;
  139. return true;
  140. } else
  141. if (plain[i][j-1]==0)
  142. {
  143. plain[i][j]=0;
  144. plain[i][j-1]=5;
  145. zombiemove=true;
  146. break;
  147. }
  148. }
  149. }
  150. //move right if is possible
  151. if (rndmove == 4)
  152. {
  153. cout << "move right \n";
  154. if (j<9)
  155. {
  156. if (plain[i][j+1]==3)
  157. {
  158. plain[i][j]=0;
  159. zombiemove=true;
  160. break;
  161. } else
  162. if (plain[i][j+1]==1)
  163. {
  164. dead = true;
  165. return true;
  166. zombiemove=true;
  167. } else
  168. if (plain[i][j+1]==0)
  169. {
  170. plain[i][j]=0;
  171. plain[i][j+1]=5;
  172. zombiemove=true;
  173. break ;
  174. }
  175. }
  176. }
  177. }//end while
  178. }
  179. }
  180. }
  181. for (i=0;i<10;i++)
  182. for (j=0;j<10;j++)
  183. {
  184. if (plain[i][j]==5) plain[i][j]=2;
  185. }
  186. return dead;
  187. }
  188. bool movehuman(int plain[10][10])
  189. {
  190. int move;
  191. bool dead;
  192. bool pmove = false;
  193. int x,y,i,j;
  194. for (i=0;i<10;i++)
  195. for (j=0;j<10;j++)
  196. {
  197. if ((plain[i][j]) == 1){x=i;y=j;}
  198.  
  199. }
  200.  
  201. while (!pmove){
  202. display(plain);
  203. cout << "move human (2=down,8=up,4=left,6=right): ";
  204. move = getch();
  205. cout<< move;
  206. if (move ==52)
  207. {
  208. if (y>0)
  209. {
  210. if ((plain[x][y-1]==3)||(plain[x][y-1]==2))
  211. {
  212. cout<<"is mort acuma2";
  213. Sleep(2000);
  214. dead = true;
  215. return dead;
  216. }
  217. plain[x][y]=0;
  218. plain[x][y-1]=1;
  219. y--;
  220. pmove=true;
  221. }
  222.  
  223. }
  224.  
  225. if (move ==54)
  226. {
  227. if (y<9)
  228. {
  229. if ((plain[x][y+1]==3)||(plain[x][y+1]==2))
  230. {
  231. dead = true;
  232. return dead;
  233. }
  234. plain[x][y]=0;
  235. plain[x][y+1]=1;
  236. y++;
  237. pmove=true;
  238. }
  239.  
  240. }
  241. if (move ==56)
  242. {
  243. if (x>0)
  244. {
  245. if ((plain[x-1][y]==3) ||(plain[x-1][y]==2))
  246. {
  247. dead = true;
  248. return dead;
  249. }
  250. plain[x][y]=0;
  251. plain[x-1][y]=1;
  252. x--;
  253. pmove=true;
  254. }
  255.  
  256. }
  257.  
  258. if (move ==50)
  259. {
  260. if (x<9)
  261. {
  262. if ((plain[x+1][y]==3) ||(plain[x+1][y]==2))
  263. {
  264. dead = true;
  265. return dead;
  266. }
  267. plain[x][y]=0;
  268. plain[x+1][y]=1;
  269. x++;
  270. pmove=true;
  271. }
  272.  
  273. }
  274.  
  275. }
  276. return dead;
  277. }
  278.  
  279.  
  280. bool play(int nrz,int nrholes,int plain[10][10])
  281. {
  282. bool dead,endgame;
  283. endgame = false;
  284. dead = false;
  285. while (true)
  286.  
  287. {
  288. endgame = movezombies(plain);
  289. dead = movehuman(plain);
  290. nrz = nrzombies(plain);
  291. if (dead) {cout << "human dead" ; break;}
  292. if (nrz==0){ cout << "human win" ;break;}
  293. }
  294. }
  295. int main()
  296. {
  297. srand (time(NULL));
  298. int vectx[10][10];
  299. //initialise the vector
  300. for (int i=0;i<10;i++)
  301. for (int j=0;j<10;j++)
  302. {
  303. vectx[i][j]=0;
  304. }
  305.  
  306. //convention : hole number is 3 , zombie number is 2, and human number is 1;
  307. //ask for nr of holes
  308. int nrholes;
  309. cout << "number of holes(0 for random) : ";
  310. cin >> nrholes;
  311. //if type 0 , then the nr of holes is generated automaticaly
  312. if (nrholes == 0) nrholes = rand() % 10 + 1;
  313.  
  314. //place the holes
  315. int i=0;
  316. int xhol,yhol;
  317. while (i<nrholes) {
  318. xhol = rand()%10 + 1;
  319. yhol = rand()%10 + 1;
  320. if (vectx[xhol][yhol]==0)
  321. {
  322. srand (time(NULL));
  323. vectx[xhol][yhol]=3;
  324. i++;
  325. }
  326.  
  327. }
  328.  
  329. //ask for nr of zombies
  330. int nrzombies;
  331. cout << "number of zombies(0 for random) : ";
  332. cin >> nrzombies;
  333. //if nr is 0, then is generated automaticaly
  334. if (nrzombies == 0) nrzombies = rand() % 10 + 1;
  335. i=0;
  336. while (i<nrzombies) {
  337. xhol = rand()%9 + 1;
  338. yhol = rand()%9 + 1;
  339. if (vectx[xhol][yhol]==0)
  340. {
  341. srand (time(NULL));
  342. vectx[xhol][yhol]=2;
  343. i++;
  344. }
  345.  
  346. }
  347.  
  348. i=0;
  349. cout << "now we place the man";
  350. while (i<1) {
  351. xhol = rand()%10 + 1;
  352. yhol = rand()%10 + 1;
  353. if (vectx[xhol][yhol]==0)
  354. {
  355. srand (time(NULL));
  356. vectx[xhol][yhol]=1;
  357. i++;
  358. }
  359.  
  360. }
  361.  
  362. display(vectx);
  363. play(nrzombies,nrholes,vectx);
  364. getch();
  365.  
  366.  
  367. }

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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: errors in game code

 
1
  #2
Jul 22nd, 2008
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)
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: redrum237 is an unknown quantity at this point 
Solved Threads: 0
redrum237 redrum237 is offline Offline
Newbie Poster

Re: errors in game code

 
0
  #3
Jul 22nd, 2008
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <time.h>
  4. #include <iostream>
  5. #include <windows.h>
  6. using namespace std;
  7. bool zmbexist(int sqarep[10][10], int posx, int posy)
  8. {
  9. if (sqarep[posx][posy]==0) return true;
  10. else return false;
  11.  
  12. }
  13. int nrzombies(int plain[10][10])
  14. {
  15. int i,j;
  16. int nrzmb;
  17. nrzmb =0 ;
  18. for (i=0;i<10;i++)
  19. for (j=0;j<10;j++)
  20. {
  21. if (plain[i][j] == 2) nrzmb++;
  22. }
  23. return nrzmb;
  24. }
  25. void display(int terain[10][10])
  26. {
  27. system("cls");
  28. cout << "\n";
  29. for (int i=0;i<10;i++){
  30. for (int j=0;j<10;j++)
  31. {
  32. if (terain[i][j] == 2) cout << " Z ";
  33. else
  34. if (terain[i][j] == 1) cout << " M ";
  35. else
  36. if (terain[i][j] == 3) cout << " H ";
  37. else
  38. if (terain[i][j] == -1) cout << " D ";
  39. else cout << " * ";
  40. }
  41. cout<<"\n";
  42. }
  43. // Sleep(5000);
  44. }
  45. bool movezombies(int plain[10][10])
  46. {
  47. cout <<"is in movezombie";
  48. bool dead,zombiemove;
  49. int i,j,k;
  50. for (i=0;i<10;i++)
  51. {
  52. for (j=0;j<10;j++)
  53. {
  54. //make sure that all the zombie moves
  55.  
  56. if (plain[i][j]==2)
  57. {
  58. zombiemove=false;
  59. while (!zombiemove){
  60. int rndmove = rand()%4+1;
  61. cout << rndmove << "\n";
  62. // Sleep(1000);
  63. cout << "now move zombie \n";
  64. //move up if is possible
  65.  
  66. if (rndmove == 1)
  67. {
  68. cout << "move up \n";
  69. if (i>1)
  70. {
  71. if (plain[i-1][j]==3)
  72. {
  73. plain[i][j]=0;
  74. zombiemove=true;
  75. break;
  76. } else
  77. if (plain[i-1][j]==1)
  78. {
  79. dead = true;
  80. return true;
  81. zombiemove=true;
  82.  
  83. } else
  84. if (plain[i-1][j]==0)
  85. {
  86. plain[i][j]=0;
  87. plain[i-1][j]=5;
  88. zombiemove=true;
  89. break;
  90.  
  91. }
  92. }
  93. }
  94. //move down if is possible
  95. if (rndmove == 2)
  96. {
  97. cout << "move down \n";
  98. if (i<9)
  99. {
  100. if (plain[i+1][j]==3)
  101. {
  102. plain[i][j]=0;
  103. zombiemove=true;
  104. break;
  105. } else
  106. if (plain[i+1][j]==1)
  107. {
  108. dead = true;
  109. return true;
  110. zombiemove=true;
  111. true;
  112. } else
  113. if (plain[i+1][j]==0)
  114. {
  115. plain[i][j]=0;
  116. plain[i+1][j]=5;
  117. zombiemove=true;
  118. break;
  119.  
  120. }
  121. }
  122. }
  123. //move left if is possible
  124. if (rndmove == 3)
  125. {
  126. cout << "move left \n";
  127. if (j>1)
  128. {
  129. if (plain[i][j-1]==3)
  130. {
  131. plain[i][j]=0;
  132. zombiemove=true;
  133. break;
  134. } else
  135. if (plain[i][j-1]==1)
  136. {
  137. dead = true;
  138. zombiemove=true;
  139. return true;
  140. } else
  141. if (plain[i][j-1]==0)
  142. {
  143. plain[i][j]=0;
  144. plain[i][j-1]=5;
  145. zombiemove=true;
  146. break;
  147. }
  148. }
  149. }
  150. //move right if is possible
  151. if (rndmove == 4)
  152. {
  153. cout << "move right \n";
  154. if (j<9)
  155. {
  156. if (plain[i][j+1]==3)
  157. {
  158. plain[i][j]=0;
  159. zombiemove=true;
  160. break;
  161. } else
  162. if (plain[i][j+1]==1)
  163. {
  164. dead = true;
  165. return true;
  166. zombiemove=true;
  167. } else
  168. if (plain[i][j+1]==0)
  169. {
  170. plain[i][j]=0;
  171. plain[i][j+1]=5;
  172. zombiemove=true;
  173. break ;
  174. }
  175. }
  176. }
  177. }//end while
  178. }
  179. }
  180. }
  181. for (i=0;i<10;i++)
  182. for (j=0;j<10;j++)
  183. {
  184. if (plain[i][j]==5) plain[i][j]=2;
  185. }
  186. return dead;
  187. }
  188. bool movehuman(int plain[10][10])
  189. {
  190. int move;
  191. bool dead;
  192. bool pmove = false;
  193. int x,y,i,j;
  194. for (i=0;i<10;i++)
  195. for (j=0;j<10;j++)
  196. {
  197. if ((plain[i][j]) == 1){x=i;y=j;}
  198.  
  199. }
  200.  
  201. while (!pmove){
  202. display(plain);
  203. cout << "move human (2=down,8=up,4=left,6=right): ";
  204. move = getch();
  205. cout<< move;
  206. if (move ==52)
  207. {
  208. if (y>0)
  209. {
  210. if ((plain[x][y-1]==3)||(plain[x][y-1]==2))
  211. {
  212. cout<<"is mort acuma2";
  213. Sleep(2000);
  214. dead = true;
  215. return dead;
  216. }
  217. plain[x][y]=0;
  218. plain[x][y-1]=1;
  219. y--;
  220. pmove=true;
  221. }
  222.  
  223. }
  224.  
  225. if (move ==54)
  226. {
  227. if (y<9)
  228. {
  229. if ((plain[x][y+1]==3)||(plain[x][y+1]==2))
  230. {
  231. dead = true;
  232. return dead;
  233. }
  234. plain[x][y]=0;
  235. plain[x][y+1]=1;
  236. y++;
  237. pmove=true;
  238. }
  239.  
  240. }
  241. if (move ==56)
  242. {
  243. if (x>0)
  244. {
  245. if ((plain[x-1][y]==3) ||(plain[x-1][y]==2))
  246. {
  247. dead = true;
  248. return dead;
  249. }
  250. plain[x][y]=0;
  251. plain[x-1][y]=1;
  252. x--;
  253. pmove=true;
  254. }
  255.  
  256. }
  257.  
  258. if (move ==50)
  259. {
  260. if (x<9)
  261. {
  262. if ((plain[x+1][y]==3) ||(plain[x+1][y]==2))
  263. {
  264. dead = true;
  265. return dead;
  266. }
  267. plain[x][y]=0;
  268. plain[x+1][y]=1;
  269. x++;
  270. pmove=true;
  271. }
  272.  
  273. }
  274.  
  275. }
  276. return dead;
  277. }
  278.  
  279.  
  280. bool play(int nrz,int nrholes,int plain[10][10])
  281. {
  282. bool dead,endgame;
  283. endgame = false;
  284. dead = false;
  285. while (true)
  286.  
  287. {
  288. endgame = movezombies(plain);
  289. dead = movehuman(plain);
  290. nrz = nrzombies(plain);
  291. if (dead) {cout << "human dead" ; break;}
  292. if (nrz==0){ cout << "human win" ;break;}
  293. }
  294. }
  295. int main()
  296. {
  297. srand (time(NULL));
  298. int vectx[10][10];
  299. //initialise the vector
  300. for (int i=0;i<10;i++)
  301. for (int j=0;j<10;j++)
  302. {
  303. vectx[i][j]=0;
  304. }
  305.  
  306. //convention : hole number is 3 , zombie number is 2, and human number is 1;
  307. //ask for nr of holes
  308. int nrholes;
  309. cout << "number of holes(0 for random) : ";
  310. cin >> nrholes;
  311. //if type 0 , then the nr of holes is generated automaticaly
  312. if (nrholes == 0) nrholes = rand() % 10 + 1;
  313.  
  314. //place the holes
  315. int i=0;
  316. int xhol,yhol;
  317. while (i<nrholes) {
  318. xhol = rand()%10 + 1;
  319. yhol = rand()%10 + 1;
  320. if (vectx[xhol][yhol]==0)
  321. {
  322. srand (time(NULL));
  323. vectx[xhol][yhol]=3;
  324. i++;
  325. }
  326.  
  327. }
  328.  
  329. //ask for nr of zombies
  330. int nrzombies;
  331. cout << "number of zombies(0 for random) : ";
  332. cin >> nrzombies;
  333. //if nr is 0, then is generated automaticaly
  334. if (nrzombies == 0) nrzombies = rand() % 10 + 1;
  335. i=0;
  336. while (i<nrzombies) {
  337. xhol = rand()%9 + 1;
  338. yhol = rand()%9 + 1;
  339. if (vectx[xhol][yhol]==0)
  340. {
  341. srand (time(NULL));
  342. vectx[xhol][yhol]=2;
  343. i++;
  344. }
  345.  
  346. }
  347.  
  348. i=0;
  349. cout << "now we place the man";
  350. while (i<1) {
  351. xhol = rand()%10 + 1;
  352. yhol = rand()%10 + 1;
  353. if (vectx[xhol][yhol]==0)
  354. {
  355. srand (time(NULL));
  356. vectx[xhol][yhol]=1;
  357. i++;
  358. }
  359.  
  360. }
  361.  
  362. display(vectx);
  363. play(nrzombies,nrholes,vectx);
  364. getch();
  365.  
  366.  
  367. }

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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: errors in game code

 
0
  #4
Jul 22nd, 2008
First of all, getch() need header <conio.h> if my memory serve my right. However, getch() , somehow, was not recommended to use.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: redrum237 is an unknown quantity at this point 
Solved Threads: 0
redrum237 redrum237 is offline Offline
Newbie Poster

Re: errors in game code

 
0
  #5
Jul 23rd, 2008
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

  1. bool play(int nrz,int nrholes,int plain[10][10])
  2. {
  3. bool dead,endgame;
  4. endgame = false;
  5. dead = false;
  6. while (true)
  7.  
  8. {
  9. endgame = movezombies(plain);
  10. dead = movehuman(plain);
  11. nrz = nrzombies(plain);
  12. if (dead) {cout << "human dead" ; break;}
  13. if (nrz==0){ cout << "human win" ;break;}
  14. }
  15. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: errors in game code

 
0
  #6
Jul 23rd, 2008
Originally Posted by redrum237 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: redrum237 is an unknown quantity at this point 
Solved Threads: 0
redrum237 redrum237 is offline Offline
Newbie Poster

Re: errors in game code

 
0
  #7
Jul 23rd, 2008
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.


  1. bool movezombies(int plain[10][10])
  2. {
  3. cout <<"is in movezombie";
  4. bool dead,zombiemove;
  5. int i,j,k;
  6. for (i=0;i<10;i++)
  7. {
  8. for (j=0;j<10;j++)
  9. {
  10. //make sure that all the zombie moves
  11.  
  12. if (plain[i][j]==2)
  13. {
  14. zombiemove=false;
  15. while (!zombiemove){
  16. int rndmove = rand()%4+1;
  17. cout << rndmove << "\n";
  18. Sleep(1000);
  19. cout << "now move zombie \n";
  20. //move up if is possible
  21.  
  22. if (rndmove == 1)
  23. {
  24. cout << "move up \n";
  25. if (i>1)
  26. {
  27. if (plain[i-1][j]==3)
  28. {
  29. plain[i][j]=0;
  30. zombiemove=true;
  31. break;
  32. } else
  33. if (plain[i-1][j]==1)
  34. {
  35. dead = true;
  36. return true;
  37. zombiemove=true;
  38.  
  39. } else
  40. if (plain[i-1][j]==0)
  41. {
  42. plain[i][j]=0;
  43. plain[i-1][j]=5;
  44. zombiemove=true;
  45. break;
  46.  
  47. }
  48. }
  49. }
  50. //move down if is possible
  51. if (rndmove == 2)
  52. {
  53. cout << "move down \n";
  54. if (i<9)
  55. {
  56. if (plain[i+1][j]==3)
  57. {
  58. plain[i][j]=0;
  59. zombiemove=true;
  60. break;
  61. } else
  62. if (plain[i+1][j]==1)
  63. {
  64. dead = true;
  65. return true;
  66. zombiemove=true;
  67. true;
  68. } else
  69. if (plain[i+1][j]==0)
  70. {
  71. plain[i][j]=0;
  72. plain[i+1][j]=5;
  73. zombiemove=true;
  74. break;
  75.  
  76. }
  77. }
  78. }
  79. //move left if is possible
  80. if (rndmove == 3)
  81. {
  82. cout << "move left \n";
  83. if (j>1)
  84. {
  85. if (plain[i][j-1]==3)
  86. {
  87. plain[i][j]=0;
  88. zombiemove=true;
  89. break;
  90. } else
  91. if (plain[i][j-1]==1)
  92. {
  93. dead = true;
  94. zombiemove=true;
  95. return true;
  96. } else
  97. if (plain[i][j-1]==0)
  98. {
  99. plain[i][j]=0;
  100. plain[i][j-1]=5;
  101. zombiemove=true;
  102. break;
  103. }
  104. }
  105. }
  106. //move right if is possible
  107. if (rndmove == 4)
  108. {
  109. cout << "move right \n";
  110. if (j<9)
  111. {
  112. if (plain[i][j+1]==3)
  113. {
  114. plain[i][j]=0;
  115. zombiemove=true;
  116. break;
  117. } else
  118. if (plain[i][j+1]==1)
  119. {
  120. dead = true;
  121. return true;
  122. zombiemove=true;
  123. } else
  124. if (plain[i][j+1]==0)
  125. {
  126. plain[i][j]=0;
  127. plain[i][j+1]=5;
  128. zombiemove=true;
  129. break ;
  130. }
  131. }
  132. }
  133. }//end while
  134. }
  135. }
  136. }
  137. for (i=0;i<10;i++)
  138. for (j=0;j<10;j++)
  139. {
  140. if (plain[i][j]==5) plain[i][j]=2;
  141. }
  142. return dead;
  143. }
  144. bool movehuman(int plain[10][10])
  145. {
  146. int move;
  147. bool dead;
  148. bool pmove = false;
  149. int x,y,i,j;
  150. for (i=0;i<10;i++)
  151. for (j=0;j<10;j++)
  152. {
  153. if ((plain[i][j]) == 1){x=i;y=j;}
  154.  
  155. }
  156.  
  157. while (!pmove){
  158. display(plain);
  159. cout << "move human (2=down,8=up,4=left,6=right): ";
  160. move = getch();
  161. cout<< move;
  162. if (move ==52)
  163. {
  164. if (y>0)
  165. {
  166. if ((plain[x][y-1]==3)||(plain[x][y-1]==2))
  167. {
  168. cout<<"is mort acuma2";
  169. Sleep(2000);
  170. dead = true;
  171. return dead;
  172. }
  173. plain[x][y]=0;
  174. plain[x][y-1]=1;
  175. y--;
  176. pmove=true;
  177. }
  178.  
  179. }
  180.  
  181. if (move ==54)
  182. {
  183. if (y<9)
  184. {
  185. if ((plain[x][y+1]==3)||(plain[x][y+1]==2))
  186. {
  187. dead = true;
  188. return dead;
  189. }
  190. plain[x][y]=0;
  191. plain[x][y+1]=1;
  192. y++;
  193. pmove=true;
  194. }
  195.  
  196. }
  197. if (move ==56)
  198. {
  199. if (x>0)
  200. {
  201. if ((plain[x-1][y]==3) ||(plain[x-1][y]==2))
  202. {
  203. dead = true;
  204. return dead;
  205. }
  206. plain[x][y]=0;
  207. plain[x-1][y]=1;
  208. x--;
  209. pmove=true;
  210. }
  211.  
  212. }
  213.  
  214. if (move ==50)
  215. {
  216. if (x<9)
  217. {
  218. if ((plain[x+1][y]==3) ||(plain[x+1][y]==2))
  219. {
  220. dead = true;
  221. return dead;
  222. }
  223. plain[x][y]=0;
  224. plain[x+1][y]=1;
  225. x++;
  226. pmove=true;
  227. }
  228.  
  229. }
  230.  
  231. }
  232. return dead;
  233. }

thank again.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 693 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC