2 Replies - 157 Views - Last Post: 23 June 2019 - 11:08 PM Rate Topic: -----

#1 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

confused about something hashmaps related

Posted 23 June 2019 - 09:48 PM

package com.example.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    private static Map<Integer, Location> locations = new HashMap<Integer, Location>();

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        locations.put(0, new Location(0, "You are sitting in front of a computer learning Java"));
        locations.put(1, new Location(1, "You are standing at the end of a road before a small brick building"));
        locations.put(2, new Location(2, "You are at the top of a hill"));
        locations.put(3, new Location(3, "You are inside a building, a well house for a small spring"));
        locations.put(4, new Location(4, "You are in a valley beside a stream"));
        locations.put(5, new Location(5, "You are in the forest"));

        locations.get(1).addExit("W", 2);
        locations.get(1).addExit("E", 3);
        locations.get(1).addExit("S", 4);
        locations.get(1).addExit("N", 5);
       // locations.get(1).addExit("Q", 0);

        locations.get(2).addExit("N", 5);
       // locations.get(2).addExit("Q", 0);

        locations.get(3).addExit("W", 1);
        //locations.get(3).addExit("Q", 0);

        locations.get(4).addExit("N", 1);
        locations.get(4).addExit("W", 2);
        //locations.get(4).addExit("Q", 0);

        locations.get(5).addExit("S", 1);
        locations.get(5).addExit("W", 2);
        //locations.get(5).addExit("Q", 0);






        int loc = 1;
        while(true) {
            System.out.println(locations.get(loc).getDescription());
            if(loc == 0) {
                break;
            }

            Map<String, Integer> exits = locations.get(loc).getExits();
            System.out.print("Available exits are ");
            for(String exit: exits.keySet()) {
                System.out.print(exit + ", ");
            }
            System.out.println();

            String direction = scanner.nextLine().toUpperCase();

            if(exits.containsKey(direction)) {
                loc = exits.get(direction);
            } else {
                System.out.println("You cannot go in that direction");
            }

        }






    }
}









package com.example.test;

import java.util.HashMap;
import java.util.Map;

public class Location {

    private final int locationID;
    private final String description;
    private final Map<String, Integer> exits;

    public Location(int locationID, String description) {
        this.locationID = locationID;
        this.description = description;
        this.exits = new HashMap<String, Integer>();
        this.exits.put("Q", 0);
    }


    public void addExit(String direction, int location) {
        exits.put(direction, location);
    }


    public int getLocationID() {
        return locationID;
    }

    public String getDescription() {
        return description;
    }

    public Map<String, Integer> getExits() {
        return new HashMap<String, Integer>(exits);
    }
}




I don't understand how it goes from getting a direction using scanner, to changing the locatoin (loc).... if that makes sense.

this particular bit confuses me: loc = exits.get(direction);

I'm not sure how it's changing location by getting direction.... it's not making sense to me.

what i mean to say is "loc" should be a number, not a letter to get a new location, i'm confused.

Is This A Good Question/Topic? 0
  • +

Replies To: confused about something hashmaps related

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11581
  • View blog
  • Posts: 19,697
  • Joined: 19-March 11

Re: confused about something hashmaps related

Posted 23 June 2019 - 10:44 PM

A Location has a field exits which is a mapping from String to integer:

        this.exits = new HashMap<String, Integer>();



A HashMap can be thought of as a collection of key-value pairs, where you can use keys to access values. In this case, the keys are Strings and the values are integers, so I might be able to put in "Bob" and get out 137, or to put in "Ramanujan" and get out 1729. Those would not be useful in this case, but they would be legitimate in Java. In this case, the integers are index numbers for locations, and the strings are the directions that the users enter to get to those rooms.

Note that rather than using integers we could as easily have used a sort of short name, like "Road" or "Top of Hill" and then instead of

        locations.get(1).addExit("W", 2);



you might have had

        locations.get("Road").addExit("W", "Top of Hill");



You might want to consider the advantages and disadvantages of this system, it's an interesting thing to think about.
Was This Post Helpful? 0
  • +
  • -

#3 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Re: confused about something hashmaps related

Posted 23 June 2019 - 11:08 PM

View Postjon.kiparsky, on 23 June 2019 - 10:44 PM, said:

A Location has a field exits which is a mapping from String to integer:

        this.exits = new HashMap<String, Integer>();



A HashMap can be thought of as a collection of key-value pairs, where you can use keys to access values. In this case, the keys are Strings and the values are integers, so I might be able to put in "Bob" and get out 137, or to put in "Ramanujan" and get out 1729. Those would not be useful in this case, but they would be legitimate in Java. In this case, the integers are index numbers for locations, and the strings are the directions that the users enter to get to those rooms.

Note that rather than using integers we could as easily have used a sort of short name, like "Road" or "Top of Hill" and then instead of

        locations.get(1).addExit("W", 2);



you might have had

        locations.get("Road").addExit("W", "Top of Hill");



You might want to consider the advantages and disadvantages of this system, it's an interesting thing to think about.



Yes i understand it now, ty :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1