0

I am trying to write a guessing game that repeatedly picks a random object from an array and compares it to user input (gives the user the country name and has them guess the capital) but I'm relatively new to coding and I can't seem to figure out how to get a random object from the array and how to read the data from said object. Here is my code:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int yesNo;
    CountryCard a = new CountryCard();
    a.setName("Canada");
    a.setCapital("Ottawa");
    CountryCard b = new CountryCard();
    b.setName("United States");
    b.setCapital("Washington");
    CountryCard c = new CountryCard();
    c.setName("Denmark");
    c.setCapital("Copenhagen");
    CountryCard d = new CountryCard();
    d.setName("Egypt");
    d.setCapital("Cairo");
    CountryCard e = new CountryCard();
    e.setName("Finland");
    e.setCapital("Helsinki");
    CountryCard f = new CountryCard();
    f.setName("Germany");
    f.setCapital("Berlin");
    CountryCard g = new CountryCard();
    g.setName("Thailand");
    g.setCapital("Bangkok");
    CountryCard h = new CountryCard();
    h.setName("Syria");
    h.setCapital("Damascus");
    CountryCard i = new CountryCard();
    i.setName("Uganda");
    i.setCapital("Kampala");
    CountryCard j = new CountryCard();
    j.setName("Latvia");
    j.setCapital("Riga");
    CountryCard k = new CountryCard();
    k.setName("Japan");
    k.setCapital("Tokyo");
    CountryCard l = new CountryCard();
    l.setName("China");
    l.setCapital("Beijing");
    CountryCard m = new CountryCard();
    m.setName("Costa Rica");
    m.setCapital("San Jose");
    CountryCard n = new CountryCard();
    n.setName("Brazil");
    n.setCapital("Brasilia");
    CountryCard o = new CountryCard();
    o.setName("New Zealand");
    o.setCapital("Wellington");
    CountryCard[] game = {a, b, c, d, e, f, g, f, i, j, k, l, m, n, o};

    //I will need to implement some form of a loop here
    System.out.println("Would you like to guess the capital of a country?"
            + " Hit 1 for yes, or 2 for no");
    yesNo = input.nextInt();
    if (yesNo != 1) {
        System.out.println("Goodbye :(");
        System.exit(2);
    }
    if (yesNo == 1) {
        // this portion should be the guessing game,
        // get data from array compare
    }
}//end of main
// template for the playing cards
class CountryCard {
    //name of the country
    String name;
    //capital of the country
    String capital;
    //has the card been used or not
    Boolean used = false;
    //number of instances created within the class
    static int instances = 0;

    //sets name for a card
    public void setName(String name) {
        this.name = name;
        instances += 1;
    }//end of setName

    //gets the name of a card, returns name
    public String getName() {
        return name;
    }//end of getName

    //set capital for a card
    public void setCapital(String capital) {
        this.capital = capital;
        instances += 1;
    }//end of setCapital

    //gets capital of a card, returns capital
    public String getCapital() {
        return capital;
    }//end of getCapital

    //determines whether or not card has
    public boolean usedCard(boolean used) {
        //been used, returns boolean value
        return used;
    }

    //returns the amount of instances in class
    public int getInstances() {
        return instances;
    }//end of getInstances
}//end of class country card

4 Answers4

0

First, you'll need to generate a random number to pick a random CountryCard object from the array. Here's a method that can do that -

class SomeClass {
  // instantiate only once
  private Random random;
  ...
  private static CountryCard generateRandomCard(CountryCard[] array) {
      int randomIndex = random.nextInt(array.length);
      return array[randomIndex];
  }

  ...

Then, in the main() method, you should use a while loop to execute the game and ask for user input at the very end of the loop, so that the game continues as long as the user enters 1 for YesNo.

public static void main(String[] args) {
    ....
    YesNo = input.nextInt();
    while (YesNo == 1) {
      // game logic here
      CountryCard card = generateRandomCard();
      ....
      // at the end, ask user if they'd like to continue again
      YesNow = input.nextInt();
}
Manish Giri
  • 3,562
  • 8
  • 45
  • 81
0

From my understanding, generate a random number from (0 - array length), then access the array element from the generated number.

As shown below.

Random generator = new Random();
int randomIndex = generator.nextInt(myArray.length);
myArray[randomIndex];
0

You can use the random generator to generate random int values for your array

//initialization
Random generator = new Random();
int randomIndex = generator.nextInt(myArray.length);
return myArray[randomIndex];
Community
  • 1
  • 1
RAVE CLO
  • 14
  • 3
0

You can convert this array to a list and then use the Collections.shuffle method to permute the order of the elements. Once you have shuffled this list, you can return objects from it one-by-one without repeating. Also you can simplify your code by adding a constructor with two fields to the class of this object. Your code might look something like this:

class CountryCard {
    String name;
    String capital;

    public CountryCard(String name, String capital) {
        this.name = name;
        this.capital = capital;
    }

    @Override
    public String toString() {
        return name + "=" + capital;
    }
}
public static void main(String[] args) {
    ArrayList<CountryCard> cards = new ArrayList<>(Arrays.asList(
            new CountryCard("Canada", "Ottawa"),
            new CountryCard("United States", "Washington"),
            new CountryCard("Denmark", "Copenhagen"),
            new CountryCard("Egypt", "Cairo"),
            new CountryCard("Finland", "Helsinki"),
            new CountryCard("Germany", "Berlin"),
            new CountryCard("Thailand", "Bangkok"),
            new CountryCard("Syria", "Damascus"),
            new CountryCard("Uganda", "Kampala"),
            new CountryCard("Latvia", "Riga"),
            new CountryCard("Japan", "Tokyo"),
            new CountryCard("China", "Beijing"),
            new CountryCard("Costa Rica", "San Jose"),
            new CountryCard("Brazil", "Brasilia"),
            new CountryCard("New Zealand", "Wellington")));

    Collections.shuffle(cards);

    // output in a column
    cards.forEach(System.out::println);
}

Output:

Denmark=Copenhagen
Costa Rica=San Jose
Canada=Ottawa
United States=Washington
Latvia=Riga
Finland=Helsinki
Syria=Damascus
Uganda=Kampala
Germany=Berlin
New Zealand=Wellington
Egypt=Cairo
Japan=Tokyo
Thailand=Bangkok
Brazil=Brasilia
China=Beijing