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:
#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.