【问题标题】:Java Simon Says爪哇西蒙说
【发布时间】:2011-07-23 17:01:47
【问题描述】:

我目前有为 simon say 游戏制作的 GUI,我遇到的唯一问题是实现游戏逻辑(我当前的代码将生成一个序列并显示用户输入,但不会保存生成的序列,或者将其与输入进行比较)。我知道我必须使用队列或堆栈,但我不知道如何实现其中任何一个来制作可运行的游戏。

有人可以帮忙吗,这是我目前得到的:

司机:

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);
   }
}

“位置面板”(西蒙说游戏逻辑):

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;
        target = new int[10];
        currTarget = -1;

        pushButton = new JButton("Next Sequence");
        pushButton.addActionListener(new ButtonListener());
        add(pushButton);

        addMouseListener(new MouseHandler()); 
        setPreferredSize (new Dimension(WIDTH, HEIGHT));
        setBackground (Color.white);
    }

    public void paintComponent(Graphics page) 
    {
        super.paintComponent(page);

        // draw four target area boxes
        page.setColor(Color.black);
        for (int i=0; i<4; i++) 
        {
            page.drawRect(xLoc[i]-20, yLoc[i]-20, 40, 40);
        }

        // if are displaying a target, draw it
        if (sTimer>0)
        {
            page.setColor(Color.blue);
            page.fillOval(xLoc[target[currTarget]]-15, yLoc[target[currTarget]]-15, 30, 30);
            sTimer--;
            repaint();
        }
        if (sTimer == 0) 
        {
            if (currTarget < numTargets - 1) 
            {
                currTarget++;
                sTimer = 500;
            } 
            else 
            {
                sTimer--;
            }
        }

        // if are displaying a user click, draw it
        if (timer>0) 
        {
            page.setColor(Color.red);
            page.fillOval(xClick-15, yClick-15, 30, 30);
            timer--;
            repaint();
        }
    }

    // when the button is pressed, currently the program selects a single
    // random target location and displays the target there for a short time
    private class ButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent event) 
        {
            Random gen = new Random();
            numTargets = target.length;
            for (int i = 0; i < target.length; i++) 
            {
                int insert = gen.nextInt(4);

                if(i == 0)
                    target[i] = insert;
                else
                {
                    while(insert == target[i - 1])
                            insert = gen.nextInt(4);

                    target[i] = insert;
                }
            }
            currTarget = 0;
            sTimer = 500;
            repaint();
        }
    }

    // when the user clicks the mouse, this method registers the click and
    // draws a circle there for a short time
    private class MouseHandler implements MouseListener, MouseMotionListener 
    {
        // the method that is called when the mouse is clicked - note that Java is very picky that a click does
        // not include any movement of the mouse; if you move the mouse while you are clicking that is a
        // "drag" and this method is not called
        public void mouseClicked(MouseEvent event) 
        {
            // get the X and Y coordinates of where the  mouse was clicked 
            xClick = event.getX();
            yClick = event.getY();
            System.out.println("(" + xClick + "," + yClick + ")");

            // the repaint( ) method calls the paintComponent method, forcing the application window to be redrawn
            timer = 50;
            repaint();
        }
        // Java requires that the following six methods be included in a MouseListener class, but they need
        // not have any functionality provided for them
        public void mouseExited(MouseEvent event) {}
        public void mouseEntered(MouseEvent event) {}
        public void mouseReleased(MouseEvent event) {}
        public void mousePressed(MouseEvent event) {}
        public void mouseMoved(MouseEvent event) {}
        public void mouseDragged(MouseEvent event) {}
    }

}

【问题讨论】:

  • 罗伯特·马丁的“清洁代码”。无法回答您的问题,但本书最终会有所帮助。
  • 对于不懂游戏的我们来说,你至少可以写一个link to some description
  • @David:你为什么不回答?为什么这本书会有所帮助?
  • 我认为wiki中的解释会为你清楚一些事情:en.wikipedia.org/wiki/Queue_(data_structure)
  • 如果你已经解决了,请标记对你帮助最大的答案。

标签: java stack queue


【解决方案1】:

描述这似乎很复杂,所以我做了一个控制台示例:

public static class SimonSays {
    private final Random r;
    private final int simonNR = 4;
    private final ArrayList<Integer> nrs = new ArrayList<Integer>();
    private final Queue<Integer> hits = new LinkedList<Integer>();

    public SimonSays(Random r, int nr) {
        this.r = r;

        for (int i = 0; i < nr - 1; i++)
            this.nrs.add(r.nextInt(this.simonNR));
    }
    private void next() {
        this.nrs.add(r.nextInt(this.simonNR));
        this.hits.clear();
        this.hits.addAll(this.nrs);
    }
    public boolean hasTargets() {
        return hits.size() > 0;
    }
    public Integer[] targets() {
        return nrs.toArray(new Integer[nrs.size()]);
    }
    public boolean hit(Integer i) {
        Integer val = hits.poll();
        if (val.compareTo(i) == 0) return true;
        return false;
    }
    public void play() {
        Scanner sc = new Scanner(System.in);
        boolean b = true;

        do {
            this.next();
            System.out.println("Simon says: " + 
                Arrays.toString(this.targets()));
            while (hasTargets()) {
                System.out.print("You say: ");
                if ((b = hit(sc.nextInt())) == false) break;
            }
        } while (b);

        System.out.println("\nYou made to size: " + this.nrs.size());
    }
}

播放:new SimonSays(new Random(/*for fixed results enter seed*/), 3).play();

你只需要重新连接播放方法。

【讨论】:

  • 我喜欢!很好地使用扫描仪。
【解决方案2】:

检查 Javadoc 中的队列

Queue (Java 2 Platform SE 5.0)

  1. 首先将生成的序列保存在您的ButtonListener 中。在您的ButtonListener 中创建一个队列:

    Queue<Integer> generatedSequence = new Queue<Integer>();
    
  2. 在生成随机数时用随机数填充队列。

  3. 然后,在您的 mouseClicked 事件处理程序中,项从队列中取出并检查它们是否与鼠标单击的区域相同。

    李>

【讨论】:

    【解决方案3】:

    我会将生成的序列保存在List 中。然后获取此列表的Iterator。 (*) 现在您只需将用户输入与迭代器指向的列表项进行比较。如果不同:中止,如果相同:在迭代器上调用 next() 并在 * 处重复该过程,直到您浏览完整列表。 好吧,正如 gmoore 所写,您也可以使用Queue。原理保持不变,而是使用迭代器从队列中调用remove()poll()

    【讨论】:

      【解决方案4】:

      除了ArrayList,您应该不需要任何其他东西。将您的 int[] target 替换为:

      private List<Integer> targets = new ArrayList<Integer>();
      

      当需要向序列中添加新的随机目标数时:

      targets.add(new Random().nextInt(4));
      

      为了显示,您可以通过以下方式获取当前目标编号:

      int targetNum = targets.get(currTarget);
      

      使用另一个变量来跟踪用户输入回放时的位置,例如int currUser。通过检查targets.get(currUser),每轮将其设置为 0 并在他们获得正确的序列时将其递增。您也可以将numTargets 替换为targets.size()

      何时重新开始:

      targets.clear();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-14
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 2011-06-30
        • 2016-02-16
        • 2012-08-15
        • 1970-01-01
        相关资源
        最近更新 更多