Hi,
i am required to make a game, here are the details:
zombie island
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.
i have written the code, and have some errors which i cannot seem to solve. any help would be gratefully appreciated. i am using visual c+ 2005 to compile my program. please could somebody have a look at it and see where i am going wrong. thanks
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <dos.h>
#include <process.h>
#include <windows.h>
#include <fstream>
using namespace std;
bool play(int nrz,int nrholes,int plain[10][10]);
bool smartmovezombies(int plain[10][10]);
bool zmbexist(int sqarep[10][10], int posx, int posy)
{
if (sqarep[posx][posy]==0) return true;
else return false;
}
void savegame(int terain[10][10])
{
int i,j;
ofstream savegame;
savegame.open ("save.sav");
for (i=0;i<10;i++)
for (j=0;j<10;j++)
{
savegame<<terain[i][j];
}
savegame.close();
}
void loadgame(int terain[10][10])
{
int i,j;
ifstream savegame;
savegame.open ("save.sav");
string line="";
getline (savegame,line);
cout << line;
int k=0;
for (i=0;i<10;i++)
for (j=0;j<10;j++)
{
terain[i][j]=int(line[k++])-48;
}
savegame.close();
}
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])
{
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 left 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,0=save,1=loading): ";
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;
}
}
if (move ==48)
{
savegame(plain);
}
if (move ==49)
{
int tt[10][10];
int holes,zombies;
holes=0;
zombies=0;
loadgame(tt);
cout << "now loading the game";
display(tt);
Sleep(2000);
for (i=0;i<10;i++)
for(j=0;j<10;j++)
{
if (tt[i][j]==3) holes++;
if (tt[i][j]==2) zombies++;
}
cout<<"now loading the game";
Sleep(2000);
play(zombies,holes,tt);
}
}
return dead;
}
bool smartmovezombies(int plain[10][10])
{
bool dead,zombiemove;
int i,j,k;
int x,y;
//get human position
for (i=0;i<10;i++)
for (j=0;j<10;j++)
{ if (plain[i][j]==1){x=i;y=j;}}
//for everyzombie possition calculate how to get closer to human quickly
for (i=0;i<10;i++)
for (j=0;j<10;j++)
{
if (plain[i][j]==2)
{
if ((i>x)&&(j>y))
{
if (i-x>j-y)
if (plain[i-1][j]==2)plain[i][j]=0;
else {
if (i>1)
{
plain[i][j]=0;
plain[i-1][j]=5;
}
}
else {
if (plain[i][j-1]==2)plain[i][j]=0;
else {
if (j>1)
{
plain[i][j]=0;
plain[i][j-1]=5;
}
}
}
}
if ((i<x)&&(j<y))
{
if (i-x>j-y)
if (plain[i+1][j]==2)plain[i][j]=0;
else {
if (i<10)
{
plain[i][j]=0;
plain[i+1][j]=5;
}
}
else {
if (plain[i][j+1]==2)plain[i][j]=0;
else {
if (j<10)
{
plain[i][j]=0;
plain[i][j+1]=5;
}
}
}
}
if ((i<x)&&(j>y))
{
if (i-x>j-y)
if (plain[i+1][j]==2)plain[i][j]=0;
else {
if (i<10)
{
plain[i][j]=0;
plain[i+1][j]=5;
}
}
else {
if (plain[i][j-1]==2)plain[i][j]=0;
else {
if (j>1)
{
plain[i][j]=0;
plain[i][j-1]=5;
}
}
}
}
if ((i>x)&&(j<y))
{
if (i-x>j-y)
if (plain[i-1][j]==2)plain[i][j]=0;
else {
if (i>1)
{
plain[i][j]=0;
plain[i-1][j]=5;
}
}
else {
if (plain[i][j+1]==2)plain[i][j]=0;
else {
if (j>1)
{
plain[i][j]=0;
plain[i][j+1]=5;
}
}
}
}
if ( (plain[i-1][j]==1) || (plain[i+1][j]==1) || (plain[i][j+1]==1) || (plain[i][j-1]==1) )
{
return true;
}
}
}
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if (plain[i][j]==5) plain[i][j]=2;
}
return dead;
}
bool play(int nrz,int nrholes,int plain[10][10])
{
bool dead,endgame;
endgame = false;
dead = false;
while (true)
{
endgame = smartmovezombies(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 houls
int nrholes;
cout << "number of houls(0 for random) : ";
cin >> nrholes;
//if type 0 , then the nr of houls is generated automaticaly
if (nrholes == 0) nrholes = rand() % 10 + 1;
//place the houls
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();
}