Alex Rivera | Logout

Java Simon Says

Asked 2011-03-27T00:13:35.213
12

I currently have the GUI made for a simon says game, the only problem I'm having is implementing the game logic (my current code will generate a sequence and display user input, but won't save the generated sequence, or compare it to the input). I know I have to use either a queue or a stack, but I can't figure out how to implement either of these to make a working game.

Can someone please help, here's what I got so far:

Driver:

import javax.swing.JFrame;

public class Location 
{
   public static void main (String[] args) 
   {
      JFrame frame = new JFrame ("Location");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new LocationPanel());
      frame.pack();
      frame.setVisible(true);
   }
}

"Location Panel" (Simon says game logic):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Random;

public class LocationPanel extends JPanel 
{

    private final int WIDTH =300, HEIGHT=300;   // dimensions of window
    private int[] xLoc, yLoc;       // locations for target boxes
        /*      0       1
                2       3   */

    private int timer;      // timer for displaying user clicks
    private int xClick, yClick; // location of a user click

    private int sTimer;     // timer for displaying target
    private int[] target;       // choice of target

    private int currTarget;
    private int numTargets;

    private JButton pushButton; // button for playing next sequence

    public LocationPanel() 
    {
        xLoc = new int[4];
        yLoc = new int[4];
        xLoc[0] = 100;  yLoc[0] = 100;
        xLoc[1] = 200;  yLoc[1] = 100;
        xLoc[2] = 100;  yLoc[2] = 200;
        xLoc[3] = 200;  yLoc[3] = 200;

        timer = 0;
        sTimer = 0;

        xClick = -100;  yClick = -100;
Edit
Report

1 Answer

4

I would save the generated sequence in a List. Then get an Iterator of this List. (*) Now you just compare the user input with the List item the Iterator points to. If it's different: abort, if it's the same: call next() on the iterator and repeat the process at * until you walked through the complete list. Well you could use a Queue, too as gmoore wrote. The principle remains the same, instead using an iterator you would call remove() or poll() from the queue.

answered 2011-03-28T01:00:35.910

Your Answer