Write a program zombie game that prompts the user for a 5 x 5 array of zeros and
ID: 3632787 • Letter: W
Question
Write a program zombie game that prompts the user for a 5 x 5 array of zeros and ones (entered consecutively row by row) and run the following game for three times:1) Each element of the array is either a zombie (if it is 0) or a human (if it is 1).
2) The neighbors of each elements are those horizontally, vertically, or diagonally adjacent to it, hence corner elements have three neighbors, elements on the side have five neighbors, and elements in the middle have eight neighbors.
3) At each iteration of the game the following events occur:
* Any human with less than two (namely 0 or 1) human neighbors is attacked by the
zombies and becomes a zombie.
* Any zombie with more than two (namely 3 up to 8) human neighbors is cured by the
humans and becomes a human.
Denoting zombies by "-" and humans by "*", the program prints the initial state of the game (provided by the user), and then runs the above iterations three times, printing the new state each time.
Ex1)
Enter 5x5 array: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0
State 0:
- - - - -
- - - - -
- * * * -
- - - - -
- - - - -
State 1:
- - - - -
- - * - -
- - * - -
- - * - -
- - - - -
State 2:
- - - - -
- - - - -
- * * * -
- - - - -
- - - - -
State 3:
- - - - -
- - * - -
- - * - -
- - * - -
- - - - -
Ex2)
Enter 5x5 array: 1 1 0 1 1 1 0 0 0 1 0 0 1 0 0 1 0 0 0 1 1 1 0 1 1
State 0:
* * - * *
* - - - *
- - * - -
* - - - *
* * - * *
State 1:
* * - * *
* * * * *
- * - * -
* * * * *
* * - * *
State 2:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
State 3:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Explanation / Answer
#include <stdio.h>
//number of rows, columns
//constants throughout the program
#define ROWS 5
#define COLS 5
//displays the array to the console
void display(int a[ROWS][COLS])
{
int i, j;
printf(" ");
for(i = 0; i < ROWS; i++){
for(j = 0; j < COLS; j++){
if(a[i][j] == 0)
printf("%5s", "-");
else
printf("%5s","*");
}
printf(" ");
}
printf(" ");
}
//calculates number of neighbours for an element in the 5*5 matrix
//specified by its row and column
int neighboursof(int a[ROWS][COLS], int row, int col)
{
int tleft = 0, top =0 , tright = 0;
int bleft = 0, bottom = 0, bright =0;
int left = 0, right = 0;
int neighs = 0; //number of neighbours
if(row > 0)
{
top = a[row-1][col]; //top element
if(col > 0)
tleft = a[row-1][col-1]; //leftside top element
if(col < COLS-1)
tright = a[row-1][col+1];//rightside top
}
if(row < ROWS-1)
{
bottom = a[row+1][col]; //bottom
if(col > 0)
bleft = a[row+1][col-1]; //left side bottom
if(col < COLS-1)
bright = a[row+1][col+1]; //right bottom
}
if(col > 0)
left = a[row][col-1]; //left
if(col < COLS-1)
right = a[row][col+1]; //right element
//total num of neighbours
neighs = tleft + top + tright + bleft + bottom + bright + left + right;
return neighs;
}
//main function
int main()
{
int a[ROWS][COLS]; // 5*5 array
int i, j, k; //loop variables
int tmp[ROWS][COLS]; //temperory array to store intermediate values
//reads 5*5 array from the console
printf("enter 5*5 array: ");
//stores the input values into 5*5 array
//and also in tmp array
for( i = 0; i < ROWS; i++)
{
for(j = 0; j < COLS; j++)
{
scanf("%d", & a[i][j]);
tmp[i][j] = a[i][j];
}
}
//loop 4 times for 4 states
for( k =0; k< 4;k++)
{
//displays the array state
display(a);
//for each element in the 5*5 array
//find the number of neighbours
//and changes the corresponding elements in the tmp
for( i = 0; i < ROWS; i++){
for(j = 0;j < COLS; j++)
{
if(a[i][j] == 1)
{
if(neighboursof(a, i, j) < 2)
tmp[i][j] = 0;
}
else
if(neighboursof(a, i ,j) >= 3)
tmp[i][j] = 1;
} //j for
} //i for
//copies the tmp values to a
for(i =0; i<ROWS; i++)
for(j=0; j<COLS; j++)
a[i][j] = tmp[i][j];
}// k for
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.