2

I'm trying to make a program with a 'new game' button which when pressed asks the user a new question. It's a question about state capitals and I want each new game to just change the name of the state in the question. This is probably a dumb way to do it but I'm just experimenting and trying to learn more about java.

The problem I have is that it only works once. I press new game and it changes ok the first time and then it does nothing. I've tried using various combination of while and for loops but with no luck. Sometimes the program even crashed when I tried using counter++ in some combination of for loop! So, yeah I'm pretty stuck so any hel would be appreciated greatly thanks.

Main

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args){

        Gui a = new Gui();

        List<String> stateList = new ArrayList<String>();
        List<String> capsList = new ArrayList<String>();

        for(String x: StateData.states)
            stateList.add(x);

        for(String z: StateData.caps)
            capsList.add(z);
    }
}

Gui

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Gui extends JFrame {
    private static final long serialVersionUID = 1L;

    JLabel instructions = new JLabel("What is the capital of Alabama?");

    JLabel aLabel = new JLabel("Answer: ");
    JTextField aField = new JTextField(16);

    JLabel result = new JLabel();

    JButton submit = new JButton("Submit");
    JButton reset = new JButton("Reset");
    JButton ng = new JButton("New Game");

    final Random rand = new Random();
    final int randPairNo = rand.nextInt(50);

    final Rng rngOb1 = new Rng();

    final int[] shuffList = rngOb1.numFinder();

    public Gui() {

        super("State Capitals Game");
        setLookAndFeel();
        setSize(325, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);

        setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        gc.insets = new Insets(5, 5, 5, 5);

        gc.gridx = 0;
        gc.gridy = 0;
        add(instructions, gc);

        gc.gridx = 0;
        gc.gridy = 1;
        add(aLabel, gc);

        gc.gridx = 0;
        gc.gridy = 2;
        add(aField, gc);

        gc.gridx = 0;
        gc.gridy = 3;
        add(result, gc);

        gc.gridwidth = 3;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.gridx = 0;
        gc.gridy = 4;
        add(submit, gc);

        submit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String userIP = aField.getText();

                String mg = StateData.caps[randPairNo];

                if (userIP.equalsIgnoreCase(mg)) {
                    result.setText("That's correct!");
                } else {
                    result.setText("Sorry, that's incorrect.");
                }

            }
        });

        gc.gridwidth = 3;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.gridx = 0;
        gc.gridy = 5;
        add(reset, gc);

        reset.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                aField.setText("");
            }
        });

        gc.gridwidth = 3;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.gridx = 0;
        gc.gridy = 6;
        add(ng, gc);


//This is the new game button I'm trying to get to work.

        ng.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int counter = 0;

                if (counter < shuffList.length) {
                    instructions.setText("What is the capital of "
                            + StateData.states[shuffList[counter]] + "?");
                    counter++;
                } else {
                    counter = 0;
                }


            }
        });
    }

    private void setLookAndFeel() {

        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception exc) {
            // ignore error
        }

    }

}

Rng

import java.util.Random;

public class Rng {


    int[] numFinder() {

         int[] list = new int[50];
        for (int i = 0; i < list.length; i++) {
            list[i] = i;
        }

        Random rgen = new Random();

        for (int i = 0; i < list.length; i++) {
            int rnd = rgen.nextInt(list.length);
            int temp = list[i];
            list[i] = list[rnd];
            list[rnd] = temp;
        }

        return list;
    }
}

StateData

public class StateData {

    public static String[] states = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", 
            "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii",
            "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisianna",
            "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
            "Missouri", "Montana", "Nebraska", "Nevada", "New Hampsire", "New Jersey",
            "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
            "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
            "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia",
            "Winsconsin", "Wyoming" };

    public static String [] caps = {"Montgomery", "Juneau", "Pheonix", "Little Rock", "Sacremento", "Denver",
            "Hartford", "Dover", "Talahasee", "Atlanta", "Honolulu", "Boise", "Springfield", "Idianapolis",
            "Des Moines", "Topka", "Franfort", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing",
            "St Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord",
            "Trenton", "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus", "Oklahama City",
            "Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin", "Salt Lake City",
            "Montpelier", "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne"};

}
1
  • 2
    For better help sooner, post one SSCCE (as opposed to 4 separate public classes). Commented Mar 24, 2012 at 15:56

1 Answer 1

4

Your problem seems to lie in the ActionListener. You have

public void actionPerformed(ActionEvent e) {
    int counter = 0;

This will reset counter each time the button is pressed. Consistent with your description.

Declare counter as a field in your Gui class and you should be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

@Matt You are most welcome. You can accept answers to your questions and mark them as solved by using the check mark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.