Mantasha Asg 4
Mantasha Asg 4
Scanner;
// Base class for all game entities (player, enemy, health potion, power boost)
class GameEntity {
protected int x, y;
public GameEntity(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
// Method to check if two entities are at the same position
public boolean isAtSamePosition(GameEntity other) {
return this.x == other.getX() && this.y == other.getY();
}
}
// Player class, inherits from GameEntity
class Player extends GameEntity {
private int health;
private boolean hasShield;
public Player(int x, int y) {
super(x, y);
this.health = 100; // Starting health
this.hasShield = false; // Shield starts off as false
}
// Move player based on input direction (W, A, S, D)
public void move(char direction, int gridSize) {
switch (direction) {
case 'W':
if (x > 0) x--;
break;
case 'S':
if (x < gridSize - 1) x++;
break;
case 'A':
if (y > 0) y--;
break;
case 'D':
if (y < gridSize - 1) y++;
break;
default:
System.out.println("Invalid move!");
}
}
public int getHealth() {
return health;
}
public boolean hasShield() {
return hasShield;
}
public void increaseHealth() {
this.health += 50; // Increase health by 50 points
System.out.println("Health increased! Current health: " + this.health);
}
public void giveShield() {
this.hasShield = true;
System.out.println("You now have a shield! It will protect you from one enemy encounter.");
}
public void useShield() {
this.hasShield = false;
System.out.println("Your shield protected you from the enemy! The shield is now gone.");
}
}
// Enemy class, inherits from GameEntity
class Enemy extends GameEntity {
public Enemy(int x, int y) {
super(x, y);
}
// Move the enemy towards the player by adjusting x or y coordinates
public void moveTowardsPlayer(Player player) {
if (this.x < player.getX()) {
this.x++; // Move down towards the player
} else if (this.x > player.getX()) {
this.x--; // Move up towards the player
}
if (this.y < player.getY()) {
this.y++; // Move right towards the player
} else if (this.y > player.getY()) {
this.y--; // Move left towards the player
}
}
}
// HealthPotion class, inherits from GameEntity
class HealthPotion extends GameEntity {
public HealthPotion(int x, int y) {
super(x, y);
}
}
// PowerBoost class, inherits from GameEntity
class PowerBoost extends GameEntity {
public PowerBoost(int x, int y) {
super(x, y);
}
}
// Dungeon Adventure Game class
public class DungeonAdventure {
private static final int GRID_SIZE = 5;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Initialize entities
Player player = new Player(0, 0);
Enemy enemy = new Enemy(3, 3);
HealthPotion healthPotion = new HealthPotion(2, 2);
PowerBoost powerBoost = new PowerBoost(4, 4);
boolean gameOver = false;
// Game loop
while (!gameOver) {
// Display the grid
displayGrid(player, enemy, healthPotion, powerBoost);
// Display player's health and shield status
System.out.println("Player Health: " + player.getHealth());
System.out.println("Player has shield: " + (player.hasShield() ? "Yes" : "No"));
// Get player's move input
System.out.println("Enter your move (W: up, A: left, S: down, D: right): ");
char move = scanner.next().toUpperCase().charAt(0);
// Move player
player.move(move, GRID_SIZE);
// Move enemy towards the player
enemy.moveTowardsPlayer(player);
// Check for game over condition (player encounters enemy)
if (player.isAtSamePosition(enemy)) {
if (player.hasShield()) {
player.useShield(); // Player survives using shield
} else {
System.out.println("You encountered the enemy! Game Over.");
gameOver = true;
continue; // Skip further checks
}
}
// Check for player collecting items (but not the enemy)
if (player.isAtSamePosition(healthPotion)) {
System.out.println("You collected a health potion!");
player.increaseHealth(); // Increase player's health
healthPotion = new HealthPotion(-1, -1); // Remove the health potion from the grid
}
if (player.isAtSamePosition(powerBoost)) {
System.out.println("You collected a power boost!");
player.giveShield(); // Give player a shield
powerBoost = new PowerBoost(-1, -1); // Remove the power boost from the grid
}
}
scanner.close();
}
// Method to display the grid with entities
public static void displayGrid(Player player, Enemy enemy, HealthPotion healthPotion,
PowerBoost
powerBoost) {
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
if (player.getX() == i && player.getY() == j) {
System.out.print("P "); // Player
} else if (enemy.getX() == i && enemy.getY() == j) {
System.out.print("E "); // Enemy
} else if (healthPotion.getX() == i && healthPotion.getY() == j) {
System.out.print("H "); // Health Potion
} else if (powerBoost.getX() == i && powerBoost.getY() == j) {
System.out.print("PWR "); // Power Boost
} else {
System.out.print("- "); // Empty space
}
}
System.out.println();
}
}
}