In: Computer Science
import java.util.Scanner;
public class ZombieApocalypse{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
boolean gameOver = false;
int colSize = 10;
int rowSize= 10;
String floorTile= ".";
int playerX = 0;
int playerY= 0;
String playerTile="@";
int exitX= colSize-1;
int exitY= rowSize-1;
String exitTile="# ";
int zombieX=5;
int zombieY=5;
// Defining Second Zombie
int zombie2Y= 8;
int zombie2X= 3;
// Defining third zombie
int zombie3Y= 1;
int zombie3X= 7;
String zombieTile="*";
String zombie2Tile="*";
String zombie3Tile="*";
while(gameOver== false){
for(int y=0; y<rowSize; y++){
for(int x=0; x<colSize; x++){
if(x== playerX && y==
playerY){
System.out.print(playerTile);
}
else if(x== exitX &&
y== exitY){
System.out.print(exitTile);
}
else if(x== zombieX
&& y== zombieY){
System.out.print(zombieTile);
}
// Printing second zombie on map
else if(x== zombie2X && y== zombie2Y){
System.out.print(zombie2Tile);
}
// Printing third zombie
else if(x== zombie3X && y== zombie3Y){
System.out.print(zombie3Tile);
}
else{
System.out.print(floorTile);
}
}
System.out.print("\n");
}
//player input
String choice = input.nextLine();
//execute action
if(choice.equals("w")){
// constraining player to map
if (playerY>0)
playerY--;
}
else if(choice.equals("s")){
// constraining player to map
if(playerY<9)
playerY++;
}
else if(choice.equals("d")){
// constraining player to map
if(playerX<9)
playerX++;
}
else if(choice.equals("a")){
// constraining player to map
if(playerX>0)
playerX--;
}
// check win
if(zombieX== playerX && zombieY== playerY){
gameOver= true;
System.out.println("Your brains were eaten by the zombie");
}
// Defining the loss the the second zombie
if(zombie2X== playerX && zombie2Y== playerY){
gameOver= true;
System.out.println("Your brains were eaten by the zombie");
}
// Defining loss to third zombie
if(zombie3X== playerX && zombie3Y== playerY){
gameOver= true;
System.out.println("Your brains were eaten by the zombie");
}
if(playerX== exitX && playerY==exitY){
gameOver= true;
System.out.println("You survived and made it to
the exit!");
}
// execute monster action
int zombieChoice=(int)(Math.random()*4);
if(zombieChoice==0){
zombieX=(zombieX+1)% colSize;
}
else if( zombieChoice==1){
zombieX=--zombieX>=0 ? zombieX:
(colSize-1);
}
else if( zombieChoice==2){
zombieY=--zombieY>=0 ? zombieY:
(rowSize-1);
}
else if(zombieChoice==3){
zombieY= (zombieY+1)% rowSize;
}
// Second zombie's movement
int zombie2Choice=(int)(Math.random()*4);
if(zombie2Choice==0){
zombie2X=(zombie2X+1)% colSize;
}
else if( zombie2Choice==1){
zombie2X=--zombie2X>=0 ? zombie2X: (colSize-1);
}
else if( zombie2Choice==2){
zombie2Y=--zombie2Y>=0 ? zombie2Y: (rowSize-1);
}
else if(zombie2Choice==3){
zombie2Y= (zombie2Y+1)% rowSize;
}
// Third zombie's movement
int zombie3Choice=(int)(Math.random()*4);
if(zombie3Choice==0){
zombie3X=(zombie3X+1)% colSize;
}
else if( zombie3Choice==1){
zombie3X=--zombie3X>=0 ? zombie3X: (colSize-1);
}
else if( zombieChoice==2){
zombie3Y=--zombie3Y>=0 ? zombie3Y: (rowSize-1);
}
else if(zombie3Choice==3){
zombie3Y= (zombie3Y+1)% rowSize;
}
}
}
}
How would I add obstacles to this game in java? I understand it would be a move constraint, but I cant figure out exactly how to implement it. It is written in java
I believe you are working on 2D matrix for prototyping this game.
Its a good strategy for a beginner to start like this. I appreciate your efforts.
Now, as i can see, you have dedicated zombie tiles into the 2D array
To help you with obstacles part, i would suggest, while you initialize the array, or matrix denote some cells with - 1 or 'o'
These notations would, indicate, the obstacles in the matrix.
Now whenever you ask the user to press a key, and move in any direction, you increment on decrement X, Y right, that means you are moving towards any cell.
Now you have already written the code for checking if at that cell zombie is present or not, now just add this condition too in each if clause,
Matrix [X] [Y] == - 1, its an obstacle and ask, user to again input,
But the catch here is., do this all in while(true) loop as you are making your game interactive.
As you have Written logic correctly, just make few extensions i told you, and viola, lets play??.
Hope i have helped you.
Happy learning.