I'm new to Maps and I am trying to make an adventure game using maps and am having an issue when trying to retrieve one of my locations in the game. I am trying to get the value of a map that is an Integer and store it in an integer, but when I attempt to do so, it says that it found an object and not an Integer.
package com.example.AdventureGame;
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner input = new Scanner(System.in);
Location home = new Location("Home", "Welcome home", 1);
Location road = new Location("Road", "You are standing in a road", 2);
Location valley = new Location("Valley", "You are standing at the bottom of a valley, just staring up at it", 3);
Location hill = new Location("Hill", "You are in the middle of a hill, lost with nowhere to go", 4);
Location forest = new Location("Forest", "You are lost in a forest, with only a compass", 5);
Map<Integer, Location> currentLocations = new HashMap<>();
currentLocations.put(home.getLocationID(), home);
currentLocations.put(road.getLocationID(), road);
currentLocations.put(valley.getLocationID(), valley);
currentLocations.put(hill.getLocationID(), hill);
currentLocations.put(forest.getLocationID(), forest);
Map<String, Integer> directionsForHome = new HashMap<>();
directionsForHome.put("W", 5);
Map<String, Integer> directionsForRoad = new HashMap<>();
directionsForRoad.put("N", 1);
directionsForRoad.put("S", 3);
directionsForRoad.put("E", 4);
Map<String, Integer> directionsForValley = new HashMap<>();
directionsForValley.put("N", 2);
directionsForValley.put("W", 5);
Map<String, Integer> directionsForHill = new HashMap<>();
directionsForHill.put("W", 2);
Map<String, Integer> directionsForForest = new HashMap<>();
directionsForForest.put("N", 1);
List<Map> directions = new ArrayList<>();
directions.add(directionsForHome);
directions.add(directionsForRoad);
directions.add(directionsForValley);
directions.add(directionsForHill);
directions.add(directionsForForest);
Integer loc = 2;
String directionChosen = "";
while (true) {
if (loc == 1) {
break;
}
System.out.println(currentLocations.get(loc).getDescription());
System.out.println("Current locations are: " + directions.get(loc - 1).keySet());
directionChosen = input.nextLine().toUpperCase();
// loc = directions.get
if (directions.get(loc - 1).containsKey(directionChosen)) {
loc = directions.get(loc - 1).get(directionChosen);
} else {
System.out.println("Invalid DIRECTION");
}
break;
}
}
}
And here is the code for the Location class:
package com.example.AdventureGame;
import java.util.HashMap;
import java.util.Map;
public class Location {
private String locationName;
private String description;
private int locationID;
private Map<String, Integer> locations;
public Location(String locationName, String description, int locationID) {
this.locationName = locationName;
this.description = description;
this.locationID = locationID;
this.locations = new HashMap<>();
this.locations.put("Q", 0);
}
public String getLocationName() {
return locationName;
}
public String getDescription() {
return description;
}
public int getLocationID() {
return locationID;
}
}
The error occurs on the line in the Main class that reads:
loc = directions.get(loc -1).get(directionChosen);
I really appreciate any help :D Thanks!