How do I allow the player to move out of the first room?

59 Views Asked by At

I'm trying to move the character from one room to another, The controls are 1, 2, 3 and 4, it is text based, and i've taken a few approaches, but keep running into the same thing, it keeps saying that I can not move up from there, or down, etc. I'm not sure what the issue is, if anyone could help that'd be great!

package com.mycompany.battlegame;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BattleGame {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // Create a new player character
    System.out.println("Welcome to Dungeon Adventure! What is your character's name?");
    String name = input.nextLine();
    Player player = new Player(name);

    // Set up the game loop
    boolean playing = true;
    while (playing) {
      // Print the current location and options
      Room currentRoom = player.getCurrentRoom();
      System.out.println("You are currently in: " + currentRoom.getName());
      System.out.println("What would you like to do?");
      System.out.println("1. Go up");
      System.out.println("2. Go down");
      System.out.println("3. Go right");
      System.out.println("4. Go left");
      System.out.println("5. Pick up item");
      System.out.println("6. Use item");
      System.out.println("7. Check inventory");
      System.out.println("8. Check stats");
      System.out.println("9. Quit game");

      // Get the player's choice
      int choice = input.nextInt();
      input.nextLine(); // Consume the newline character
        // Perform the chosen action
        switch (choice) {
            case 1 ->                 {
                    // Go north
                    Room nextRoom = currentRoom.getNorth();
                    if (nextRoom == null) {
                        System.out.println("You cannot go up from here.");
                    } else {
                        player.setCurrentRoom(nextRoom);
                    }                          }
            case 2 ->                 {
                    // Go south
                    Room nextRoom = currentRoom.getSouth();
                    if (nextRoom == null) {
                        System.out.println("You cannot go down from here.");
                    } else {
                        player.setCurrentRoom(nextRoom);
                    }                          }
            case 3 ->                 {
                    // Go east
                    Room nextRoom = currentRoom.getEast();
                    if (nextRoom == null) {
                        System.out.println("You cannot go right from here.");
                    } else {
                        player.setCurrentRoom(nextRoom);
                    }                          }
            case 4 ->                 {
                    // Go west
                    Room nextRoom = currentRoom.getWest();
                    if (nextRoom == null) {
                        System.out.println("You cannot go left from here.");
                    } else {
                        player.setCurrentRoom(nextRoom);
                    }                          }
            case 5 -> {
                // Pick up item
                if (currentRoom.getItem() == null) {
                    System.out.println("There is no item to pick up.");
                } else {
                    player.addToInventory(currentRoom.getItem());
                    currentRoom.setItem(null);
                    System.out.println("Item added to inventory.");
                }
            }
            case 6 -> {
                // Use item
                if (player.getInventory().isEmpty()) {
                    System.out.println("Your inventory is empty.");
                } else {
                    System.out.println("Which item would you like to use?");
                    for (int i = 0; i < player.getInventory().size(); i++) {
                        System.out.println((i + 1) + ". " + player.getInventory().get(i).getName());
                    }
                    int itemChoice = input.nextInt();
                    input.nextLine(); // Consume the newline character
                    Item item = player.getInventory().get(itemChoice - 1);
                    if (item instanceof HealingPotion healingPotion) {
                        player.setHealth(healingPotion.use());
                        player.removeFromInventory(item);
                        System.out.println("You used a " + item.getName() + " and gained " + healingPotion.getAmountHealed() + " health points.");
                    } else if (item instanceof Weapon weapon) {
                        System.out.println("You equipped the " + item.getName());
                        player.setWeapon(weapon);
                    } else {
                        System.out.println("This item cannot be used.");
                    }
                }
            }
            case 7 -> {
                // Check inventory
                if (player.getInventory().isEmpty()) {
                    System.out.println("Your inventory is empty.");
                } else {
                    System.out.println("Your inventory contains:");
                    for (Item item : player.getInventory()) {
                        System.out.println(item.getName());
                    }
                }
            }
            case 8 -> {
                // Check stats
                System.out.println("Name: " + player.getName());
                System.out.println("Health: " + player.getHealth());
                System.out.println("Attack: " + player.getAttack());
                if (player.getWeapon() == null) {
                    System.out.println("Weapon: None");
                } else {
                    System.out.println("Weapon: " + player.getWeapon().getName());
                }
            }
            case 9 -> // Quit game
                playing = false;
            default -> // Invalid input
                System.out.println("Invalid input. Please try again.");
        }

      // Check if the player has died
      if (player.getHealth() <= 0) {
        playing = false;
        System.out.println("You have died. Game over.");
      }

      // Check if the player has reached the final boss
      if (player.getCurrentRoom().getName().equals("Boss Room")) {
        System.out.println("You have reached the final boss!");
        Monster boss = new Monster("Final Boss", 100, 20);
        while (playing) {
          // Print the current stats
          System.out.println("Boss Health: " + boss.getHealth());
          System.out.println("Your Health: " + player.getHealth());
          System.out.println("What would you like to do");
                    // Print the options
          System.out.println("1. Attack");
          System.out.println("2. Use healing potion");
          System.out.println("3. Run away");

          // Get the player's choice
          choice = input.nextInt();
          input.nextLine(); // Consume the newline character
            // Perform the chosen action
            switch (choice) {
                case 1 -> {
                    // Attack
                    boss.setHealth(boss.getHealth() - player.getAttack());
                    player.setHealth(player.getHealth() - boss.getAttack());
                    if (boss.getHealth() <= 0) {
                        System.out.println("You have defeated the boss!");
                        playing = false;
                    }
                }
                case 2 -> {
                    // Use healing potion
                    boolean usedPotion = false;
                    for (Item item : player.getInventory()) {
                        if (item instanceof HealingPotion healingPotion) {
                            player.setHealth(healingPotion.use());
                            player.removeFromInventory(item);
                            usedPotion = true;
                            break;
                        }
                    }       if (usedPotion) {
                        System.out.println("You used a healing potion.");
                    } else {
                        System.out.println("You do not have any healing potions.");
                    }
                }
                case 3 -> {
                    // Run away
                    System.out.println("You ran away from the boss.");
                    playing = false;
                }
                default -> // Invalid input
                    System.out.println("Invalid input. Please try again.");
            }
        }
      }
    }

    System.out.println("Thank you for playing Dungeon Adventure!");
  }
}

class Player {
  private final String name;
  private int health;
  private final int attack;
  private Room currentRoom;
  private List<Item> inventory;
  private Weapon weapon;

  public Player(String name) {
    this.name = name;
    this.health = 100;
    this.attack = 10;
    this.currentRoom = new Room("Starting Room");
    this.inventory = new ArrayList<>();
    this.weapon = null;
  }

  public String getName() {
    return name;
  }

  public int getHealth() {
    return health;
  }

  public void setHealth(int health) {
    this.health = health;
  }

  public int getAttack() {
    if (weapon == null) {
      return attack;
    } else {
      return attack + weapon.getAttackBoost();
    }
  }

  public Room getCurrentRoom() {
    return currentRoom;
  }

  public void setCurrentRoom(Room currentRoom) {
    this.currentRoom = currentRoom;
  }

  public List<Item> getInventory() {
    return inventory;
  }

  public void addToInventory(Item item) {
    inventory.add(item);
  }

  public void removeFromInventory(Item item) {
    inventory.remove(item);
  }

  public Weapon getWeapon() {
    return weapon;
  }
    public void setWeapon(Weapon weapon) {
    this.weapon = weapon;
  }
}

class Room {
  private final String name;
  private Room north;
  private Room south;
  private Room east;
  private Room west;
  private Item item;
  private Monster monster;

  public Room(String name) {
    this.name = name;
    this.north = null;
    this.south = null;
    this.east = null;
    this.west = null;
    this.item = null;
    this.monster = null;
  }

  public String getName() {
    return name;
  }

  public Room getNorth() {
    return north;
  }

  public void setNorth(Room north) {
    this.north = north;
  }

  public Room getSouth() {
    return south;
  }

  public void setSouth(Room south) {
    this.south = south;
  }

  public Room getEast() {
    return east;
  }

  public void setEast(Room east) {
    this.east = east;
  }

  public Room getWest() {
    return west;
  }

  public void setWest(Room west) {
    this.west = west;
  }

  public Item getItem() {
    return item;
  }

  public void setItem(Item item) {
    this.item = item;
  }

  public Monster getMonster() {
    return monster;
  }

  public void setMonster(Monster monster) {
    this.monster = monster;
  }
}

abstract class Item {
  private final String name;

  public Item(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}

class HealingPotion extends Item {
  private final int amountHealed;

  public HealingPotion(String name, int amountHealed) {
    super(name);
    this.amountHealed = amountHealed;
  }

  public int getAmountHealed() {
    return amountHealed;
  }

  public int use() {
    return amountHealed;
  }
}

class Weapon extends Item {
  private final int attackBoost;

  public Weapon(String name, int attackBoost) {
    super(name);
    this.attackBoost = attackBoost;
  }

  public int getAttackBoost() {
    return attackBoost;
  }
}

class Monster {
  private final String name;
  private int health;
  private final int attack;

  public Monster(String name, int health, int attack) {
    this.name = name;
    this.health = health;
    this.attack = attack;
  }

  public String getName() {
    return name;
  }

  public int getHealth() {
    return health;
  }

  public void setHealth(int health) {
    this.health = health;
  }

  public int getAttack() {
    return attack;
  }
}

1

There are 1 best solutions below

0
Brayden On

In your Room class's constructor, you set the item, monster, and all the rooms to null by default. You've only forgotten to call setNorth(Room north) before trying to call getNorth(). So therefore it returns null because that's all it's been told to hold so far.

Just make sure to initialize the north, south, east, west, etc... rooms before you call them.

Good luck working on your project!