1

This is my first post on this website so I'm extremely sorry if I break rules or stuff I don't know. That said, I have been assigned to make a Simon Says program by my teacher and I am struggling with the class so I am looking for other avenues of help. The rules are as follows:

The means of completing this assignment is entirely up to you. The code must work exactly as specified to receive proper credit. You will design the game Simon. In Simon there are four colors: red, blue, green and yellow. The user has to pick the same color(s) that Simon picks and in the correct sequence. The colors Simon chooses must be random. Each successive round Simon adds another color into the sequence. When the user fails, Simon should tell the user how many rounds they lasted. There should be no limit to how many rounds you can play. GUI is not required for this assignment. If you are using JOptionPane for choosing colors you must use the letter shortcuts of Red, Blue, Green and Yellow. You must also include instructions to the game. You must also keep track of the high score for each play and allow the user to type in their name for their high score. You do not have to keep score if you close the program; only while the program is open.

When I execute my code, it doesn't continue the game when the user gives the right answer but instead terminates after one loop. Weirdly, it only seems to loop if i give it the wrong answer. Lastly, I wanted to point out that my program is really basic because this is honestly all i know how to use. Thank you for your help

package stuff;

import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Simon {
    String s = "";
    String a = "";
    String c = "";
    String d = "";
    boolean b = true;
    boolean b2 = true;
    String obj[] = { "Red", "Yellow", "Green", "Blue" };
    ArrayList<String> colors = new ArrayList<String>();
    String[] options = new String[] { "Red", "Yellow", "Green", "Blue" };
    int count = 0;
    int count2 = 0;
    int score = 0;

    public String simon() {
            do {
                int c = ((int) (Math.random() * 4));
                if (c == 0)
                    colors.add(obj[0]);
                else if (c == 1)
                    colors.add(obj[1]);
                else if (c == 2)
                    colors.add(obj[2]);
                else if (c == 3)
                    colors.add(obj[3]);
                else {
                    JOptionPane.showMessageDialog(null,
                            "weird stuff happened bruh");
                }
                JOptionPane.showMessageDialog(null, colors.get(count));
                b2 = true;
                while (b2) {
                    for (int i2 = 0; i2 < colors.size(); i2++) {
                        a = JOptionPane.showInputDialog("put letter");
                        if(a != null){
                        if (a.equalsIgnoreCase(colors.get(i2))) {
                            JOptionPane.showMessageDialog(null,
                                    "Your color  is right" + score);
                            score += 1;
                            if (i2 == colors.size() - 1)
                                b2 = false;
                            else {
                                JOptionPane.showMessageDialog(null, "weird stuff happened bruh fo realz");  
                            }
                        } else {
                            b2 = false;
                            b = false;
                        }
                    }else{
                       System.exit(0);
                   }
                }
                }
                count++;
            } while (b == false);

        return s;
    }
}
  • 3
    Why are you using a ```try-catch``` block for a ```NullPointerException```, then just exiting without logging anything about the exception? At least use ```npe.printStackTrace()``` so you can see what caused the exception. – Caleb Brinkman Nov 17 '15 at 23:13
  • When I googled the error the error that I was getting, I found that solution on other websites, which has been working perfectly for me. If there other ways to avoid this error, can you inform me? – Charan Kotha Nov 17 '15 at 23:16
  • 2
    The appropriate way to avoid this error is to not try to call object methods on objects which are null. Which means you go to the line that's throwing the error and figure out which object is null, and why it isn't initialized to the value you expected. – Steve K Nov 17 '15 at 23:17
  • Start [here](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). Basically a ```NullPointerException``` occurs when you try to call a method on an object which is ```null```. The fact that you're exiting when you catch that exception is a strong indicator that an object set to ```null``` is part of your problem – Caleb Brinkman Nov 17 '15 at 23:18

1 Answers1

0

Alot of your code not needed, and your making it alot harder than it should be :).

I posted code to showcase how about i would go about making the program using two single for loops, and a check to combat that nasty NullPointerException that was being created when a user cancel a input.

public String simon() {
    while(true){
        generateColor();
        for(int i = 0; i < colors.size(); i++){
            JOptionPane.showMessageDialog(null, "Color #"+i+" is: "+colors.get(i));
        }
        for(int i = 0; i < colors.size(); i++){
            String input = JOptionPane.showInputDialog("Type in #"+i+" color: ");
            if(input == null){
                System.out.println("User typed in wrong input");
                return "Program ended unsuspectively";
            }
            if(input.equalsIgnoreCase(colors.get(i))){
                System.out.println("You were correct!");
            }
            else{
                System.out.println("you wasnt correct");
                return "You ended with a score of: "+score;
            }
        }
        score++;
    }
}

private void generateColor(){
    int c = (int)(Math.random() * 4);
    colors.add(obj[c]);
}     

Hope this helped and have a nice day :)

Møssi
  • 18
  • 2