Create a game in Java console - CapitalFinder [Advice & Help]

Viewed 128

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() {
    }
}
1 Answers

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);
    }
    
Related