1

I would like to create a game in java, and I would like your advice: I explain you the goal as simply as possible:

  • It's a game that takes place with the IDE console only

The goal is to define a number of capitals to find : If the player choose 20 capitals, A table will be filled in beforehand (or not if I can find a simpler way other than linking X countries to capitals by hand (if this is the case I don't know how to do it so if you have explanations I'm a taker)

In the table 20 will be drawn at random to avoid that it is all the time the same, the goal being to answer a maximum of answers, with a timer to beat his record

Questions : According to you, should I use a table that I fill by hand with countries & by linking them to capitals? If yes, how? (I tried with: collection/array unfortunately I block on how to link them to capitals) Can I manage the display of countries / linked to capitals via an API (if yes which one?)

Do you have any suggestions to avoid my head on the design of this game?

Thank you in advance, I join you my current code which is that little filled because my tests were carried out but that little conclusive thus useless to show you this

package fr.antyss77.capitalfinder;

import java.util.ArrayList;

public class Utils {

    public static void country() {
        ArrayList<String> country = new ArrayList<>();
        country.add("France");
        country.add("Jordanie");
        country.add("Pays-Bas");
        country.add("Turquie");
        country.add("Irak");
        country.add("Thailande");
        country.add("Serbie");
        country.add("Madagascar");
        country.add("Maroc");
        country.add("Canada");
        country.add("Italie");
        country.add("Chili");
        country.add("Bahamas");
        country.add("Grèce");
        country.add("Espagne");
        country.add("Belgique");
        country.add("Cuba");
        country.add("Mexique");
        country.add("Inde");
    }

    public static void question() {
    }

    public static void capital() {
    }

    public static void points() {
    }
}
vszholobov
  • 2,133
  • 1
  • 8
  • 23
  • 1
    Have a look at the `Map` class in Java. You'll probably need two Maps for your game: one with capitals for keys and countries for values, and another the other way around. – tgdavies Aug 16 '21 at 03:06
  • Oh the maps? Thank you very much, I didn't know that, I must admit that I am still at the learning stage, I am at the regular expressions, this kind of project allows me to solidify my knowledge. :D –  Aug 16 '21 at 03:08
  • Oh right ! Hashmap I didn't understand that xdd –  Aug 16 '21 at 03:09
  • You should consider Guava's BiMap or Apache's BidiMap. – Dawood ibn Kareem Aug 16 '21 at 03:30
  • Poor title. Rewrite to summarize your specific technical issue. – Basil Bourque Aug 16 '21 at 06:29

1 Answers1

0

There are several ways to connect the capital and the country.

  1. Use Map

    public static void main(String[] args) {
        Map<String, String> countries = new HashMap<>();
        countries.put("country", "capital");
        System.out.println(countries.get("country"));
    }
    
  2. Use two Lists, where capital and country have same indexes.

    public static void main(String[] args) {
        List<String> countries = new ArrayList<>();
        List<String> capitals = new ArrayList<>();
        countries.add("country");
        capitals.add("capital");
    
        System.out.println(capitals.get(countries.indexOf("country")));
    }
    
  3. Create new class Country, which will have two fields: name and capital to store objects of this class in some collection

    class Country {
        String name;
        String capital;
    
        public Country(String name, String capital) {
            this.name = name;
            this.capital = capital;
        }
    }
    
    public static void main(String[] args) {
        List<Country> countries = new ArrayList<>();
        countries.add(new Country("country", "capital"));
    
        System.out.println(countries.get(0).name);
        System.out.println(countries.get(0).capital);
    }
    
maloomeister
  • 2,461
  • 1
  • 12
  • 21
vszholobov
  • 2,133
  • 1
  • 8
  • 23
  • Thanks a lot, it's very clear now in my head, but with these 3 techniques according to you for a storage of about 100 (or more) countries/and capitals (to avoid that the program outputs all the time the same countries) what is according to you the most optimized way? I must admit that I am hesitating even though your answer is more than appreciated and helps me a lot! –  Aug 16 '21 at 03:22
  • [Take n random elements from list](https://stackoverflow.com/questions/4702036/take-n-random-elements-from-a-liste). [Take random element from Map](https://stackoverflow.com/questions/12385284/how-to-select-a-random-key-from-a-hashmap-in-java) – vszholobov Aug 16 '21 at 03:25
  • Thank you, this will help me too, but I'm mainly asking which is the most optimized method between the 3 you suggested? - In terms of performance, compactness and speed of execution –  Aug 16 '21 at 03:26
  • The map solution will be the fastest in terms of access, since key access is performed in `O(1)`. To access an element in an array it needs to be found, complexity `O(n)`. [Time complexity](https://en.wikipedia.org/wiki/Time_complexity). – vszholobov Aug 16 '21 at 03:32
  • Thank you, it's the one I was interested in, but please be precise about this method: How can I link an element of my Map : Example 1 country to a capital, but I must admit that I did not understand well because it is not as if I could do : Country n°1 = Capital n°1 Do you see? –  Aug 16 '21 at 03:33
  • 1
    In terms of `Map` `Country USA = Capital Washington` will look like `countries.put("USA", "Washington")`. [Map put method](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V-) – vszholobov Aug 16 '21 at 03:40
  • 1
    I have finally grasped thank you very much : Map countries = new HashMap<>() ; countries.put("First country", "First capital") ; –  Aug 16 '21 at 03:41
  • And how can I get out this way (in a scanner): How many capitals do you want to find : The player answers: 10 -> First Country: France -> Find the capital (France must be associated with Paris) But I can not display no? Or I have to do a foreach? –  Aug 16 '21 at 03:45
  • And if I want to display in my console only the country: I can't either? –  Aug 16 '21 at 03:45
  • [Can I ask only one question per post?](https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post) – vszholobov Aug 16 '21 at 03:46
  • 1
    Okay sorry I understand thank you for your help I'll figure it out and if I don't find it I'll post again thank you for your time and sympathy –  Aug 16 '21 at 03:48