Working on simon says game in java. Program compiles fine, but when playing game, gameplay is not working as intended. I've been trying to find the problem for more than two weeks, but I can't find it. I've added code to the program to print to the console when the problematic parts of the program are running. I have done everything I know to do, and I'm really frustrated. Can someone PLEASE take a look, and see if they can help me? I put the entire program so that you can run it yourselves while looking.
package simon2; import javax.swing.*; import javax.swing.Timer; import java.util.*; import java.awt.*; import java.awt.Color.*; import java.awt.event.*; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; // TODO: // ICON IMAGE TESTING // Button sounds. /** * Main Class. * Just creates GUI. * */ public class Simon2 { public static void main(String[] args) { // Creating main game window. JFrame gameWindow = new JFrame("Simon - Created by Chris Mailloux"); gameWindow.setVisible(true); gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gameWindow.setSize(600, 600); gameWindow.setResizable(true); gameWindow.setLocation(DEFAULT_HORIZONTAL_LOCATION, DEFAULT_VERTICAL_LOCATION); // Creating panel to support border layout. JPanel borderPanel = new JPanel(); borderPanel.setLayout(new BorderLayout()); gameWindow.add(borderPanel); // Creating menu panel. JPanel menuPanel = new JPanel(); borderPanel.add(menuPanel, BorderLayout.NORTH); menuPanel.setBackground(Color.BLACK); // Adding buttons to menu panel. menuPanel.add(startButton); menuPanel.add(scoreDisplay); menuPanel.add(highScoreDisplay); // Configuring buttons scoreDisplay.setEnabled(false); highScoreDisplay.setEnabled(false); startButton.setFocusable(false); // Styling buttons startButton.setBackground(Color.BLUE); scoreDisplay.setBackground(Color.BLACK); highScoreDisplay.setBackground(Color.BLACK); startButton.setForeground(Color.WHITE); // Creating buttonsPanel. JPanel buttonsPanel = new JPanel(); borderPanel.add(buttonsPanel, BorderLayout.CENTER); buttonsPanel.setBackground(Color.BLACK); buttonsPanel.setLayout(new GridLayout(2, 2, 20, 20)); // Adding game buttons. buttonsPanel.add(greenButton); buttonsPanel.add(redButton); buttonsPanel.add(blueButton); buttonsPanel.add(yellowButton); // Disabling button focus ability. greenButton.setFocusable(false); redButton.setFocusable(false); blueButton.setFocusable(false); yellowButton.setFocusable(false); // Styling buttons. greenButton.setBackground(Color.GREEN); redButton.setBackground(Color.RED); blueButton.setBackground(Color.BLUE); yellowButton.setBackground(Color.YELLOW); // Attaching actionListeners to buttons startButton.addActionListener(startButtonListener); greenButton.addActionListener(greenButtonListener); redButton.addActionListener(redButtonListener); blueButton.addActionListener(blueButtonListener); yellowButton.addActionListener(yellowButtonListener); } // Creating ActionListeners for buttons. // Start button. static ActionListener startButtonListener = new ActionListener() { public void actionPerformed(ActionEvent evtStart) { ButtonActions.startAction(); } }; // Green button. static ActionListener greenButtonListener = new ActionListener() { public void actionPerformed(ActionEvent evtGreen) { ButtonActions.greenAction(); } }; // Red button. static ActionListener redButtonListener = new ActionListener() { public void actionPerformed(ActionEvent evtRed) { ButtonActions.redAction(); } }; // Blue button. static ActionListener blueButtonListener = new ActionListener() { public void actionPerformed(ActionEvent evtBlue) { ButtonActions.blueAction(); } }; // Yellow button. static ActionListener yellowButtonListener = new ActionListener() { public void actionPerformed(ActionEvent evtYellow) { ButtonActions.yellowAction(); } }; // Class methods // Accessor Methods static String getCurrentScore() { return String.valueOf(Game.currentScore); } static String getHighScore() { return String.valueOf(Game.highScore); } // End class methods // Button objects creation static JButton startButton = new JButton("Start"); static JButton scoreDisplay = new JButton ("Current score: " + getCurrentScore()); static JButton highScoreDisplay = new JButton ("High Score: " + getHighScore()); static JButton greenButton = new JButton(); static JButton redButton = new JButton(); static JButton blueButton = new JButton(); static JButton yellowButton = new JButton(); // Class variables private static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); private static final int DEFAULT_HORIZONTAL_LOCATION = ((screenSize.width - 600) / 2); private static final int DEFAULT_VERTICAL_LOCATION = ((screenSize.height - 600) / 2); } /** * Handles actions of buttons. * Creates timers and ActionListeners for each button to add delay. */ class ButtonActions { static int delay = 400; // Start Button static void startAction() { Game.startGame(); } // Green Button static void greenAction() { ActionListener delayer1 = new ActionListener() { public void actionPerformed(ActionEvent evt1) { Simon2.greenButton.setBackground(Color.GREEN); if (Game.isPlayerTurn) { Simon2.greenButton.setEnabled(true); } } }; Timer timer1 = new Timer(delay, delayer1); timer1.setRepeats(false); Simon2.greenButton.setEnabled(false); Simon2.greenButton.setBackground(Color.WHITE); if (Game.isPlayerTurn) { Game.check("Green"); System.out.println("Passing green to check method."); } timer1.start(); } // Red Button static void redAction() { ActionListener delayer2 = new ActionListener() { public void actionPerformed(ActionEvent evt2) { Simon2.redButton.setBackground(Color.RED); if (Game.isPlayerTurn) { Simon2.redButton.setEnabled(true); } } }; Timer timer2 = new Timer(delay, delayer2); timer2.setRepeats(false); Simon2.redButton.setEnabled(false); Simon2.redButton.setBackground(Color.WHITE); if (Game.isPlayerTurn) { Game.check("Red"); System.out.println("Passing red to check method."); } timer2.start(); } // Blue Button static void blueAction() { ActionListener delayer3 = new ActionListener() { public void actionPerformed(ActionEvent evt3) { Simon2.blueButton.setBackground(Color.BLUE); if (Game.isPlayerTurn) { Simon2.blueButton.setEnabled(true); } } }; Timer timer3 = new Timer(delay, delayer3); timer3.setRepeats(false); Simon2.blueButton.setEnabled(false); Simon2.blueButton.setBackground(Color.WHITE); if (Game.isPlayerTurn) { Game.check("Blue"); System.out.println("Passing Blue to check method"); } timer3.start(); } // Yellow Button static void yellowAction() { ActionListener delayer4 = new ActionListener() { public void actionPerformed(ActionEvent evt4) { Simon2.yellowButton.setBackground(Color.YELLOW); if (Game.isPlayerTurn) { Simon2.yellowButton.setEnabled(true); } } }; Timer timer4 = new Timer(delay, delayer4); timer4.setRepeats(false); Simon2.yellowButton.setEnabled(false); Simon2.yellowButton.setBackground(Color.WHITE); if (Game.isPlayerTurn) { Game.check("Yellow"); System.out.println("Passing yellow to check method."); } timer4.start(); } } /** * Handles game logic. */ class Game { static Timer simonTurnTimer; static void startGame() { pattern.clear(); isPlayerTurn = false; currentScore = 0; gamePlay(); } static void gamePlay() { if (isPlayerTurn == true) { playerTurn(); } else if (isPlayerTurn == false) { simonTurn(); } else { System.out.println("Error: Player turn has not been assigned," + " or has value other than a boolean."); } return; // test } private static void playerTurn() { System.out.println("PlayerTurn"); // First, make sure buttons are enabled. Simon2.greenButton.setEnabled(true); Simon2.redButton.setEnabled(true); Simon2.blueButton.setEnabled(true); Simon2.yellowButton.setEnabled(true); // Since this should only be called once per turn, // set iterator to 0 to initialize turn. iterator = 0; } // Test. static void delayTurn() { System.out.println("Delaying turn"); simonTurnTimer = new Timer(simonTurnDelay, simonListener); simonTurnTimer.setRepeats(false); simonTurnTimer.start(); } private static void simonTurn() { System.out.println("Simon's turn"); // First, disable buttons. Simon2.greenButton.setEnabled(false); Simon2.redButton.setEnabled(false); Simon2.blueButton.setEnabled(false); Simon2.yellowButton.setEnabled(false); // Initialize... iterator = 0; // Simon adds random color to pattern randInt = (int) (Math.random() * 4); System.out.println("RandInt: " + randInt); if (randInt == 0) { pattern.add("Green"); System.out.println("Adding green"); } else if (randInt == 1) { pattern.add("Red"); System.out.println("Adding red"); } else if (randInt == 2) { pattern.add("Blue"); System.out.println("Adding blue"); } else if (randInt == 3) { pattern.add("Yellow"); System.out.println("Adding yellow"); } else { System.out.println("Error: Random generation has failed."); } // Simon shows the new pattern... showPattern(); System.out.println("Called showpattern."); // Now, it is the player's turn. isPlayerTurn = true; gamePlay(); } // Just kinda thrown in here randomly, must reorganize. static ActionListener simonListener = new ActionListener() { public void actionPerformed(ActionEvent simonTurnDelayEvent) { simonTurn(); } }; // BUGNOTE: Not sure if need to repaint currentScore and/or highScore static void check(String lastInput) { if (lastInput.equals(pattern.get(iterator))) { iterator++; currentScore++; System.out.println("Updating current score and iterator"); Simon2.scoreDisplay.repaint(); // Possibly unneccessary if (currentScore > highScore) { highScore++; System.out.println("Updated highscore?"); Simon2.highScoreDisplay.repaint(); // Possibly unneccessary } if (iterator == getPatternLength()) { delayTurn(); // test isPlayerTurn = false; } } else { gameOver(); } } static int getPatternLength() { patternLength = pattern.size(); System.out.println("Get pattern length method" + " called. Length : " + patternLength); return patternLength; } static void showPattern() { iterator = 0; // Activate each button based on pattern and iterator traversal. while (iterator < getPatternLength()) { if (pattern.get(iterator) == "Green") { ButtonActions.greenAction(); System.out.println("Called greenAction from show"); } else if (pattern.get(iterator) == "Red") { ButtonActions.redAction(); System.out.println("Called redAction from show"); } else if (pattern.get(iterator) == "Blue") { ButtonActions.blueAction(); System.out.println("Called blueAction from show"); } else if (pattern.get(iterator) == "Yellow") { ButtonActions.yellowAction(); System.out.println("Called yellowAction from show"); } else { System.out.println("Error: showPattern method has failed. Possible bad value."); } iterator++; } } static void gameOver() { System.out.println("Game over"); // Need to play gameOver sound // Need to write highscore to file } // Class variable declarations. static ArrayList<String> pattern = new ArrayList<String>(); private static int patternLength; static int simonTurnDelay = 1000; // test static int randInt; static int iterator; static int currentScore; static int highScore; static boolean isPlayerTurn; }