0

我正在尝试制作一个像 simon 这样的游戏,其中一个模式会亮起,你必须重复它。我遇到了一个路障,所有逻辑都是正确的,但是当应该时,块不会变白并返回。该循环在第一次运行,但在第二级不起作用。

这是循环:

private void runLevel() {
    // TODO Auto-generated method stub
    System.out.println("Level"+ level);
    for(int i=0;i<level;i++){
        Random rand = new Random();
        int num = rand.nextInt(4);
        System.out.println(num);
        cpu.add(cpuChoices.get(num));
    }
    for(int i=0;i<cpu.size();i++){
        System.out.println("Level "+level+" i "+i+" "+cpu.get(i));
        cpu.get(i).setBackground(Color.white);
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(cpu.get(i)==button1)
            cpu.get(i).setBackground(Color.green);
        else if(cpu.get(i)==button2)
        cpu.get(i).setBackground(Color.red);
        else if (cpu.get(i)==button3)
        cpu.get(i).setBackground(Color.blue);
        else if (cpu.get(i)==button4)
        cpu.get(i).setBackground(Color.yellow);

        ;
    }
        transferData();
        cpu.clear();
}

同样第一级很好,但第二级程序仍然有效,但方块没有按预期亮起。

如果需要这里是整个班级:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

import javax.swing.*;

public class MyFrame extends JFrame implements ActionListener{

    int frameRate = 200;
    int guess = 1;
    int level = 1;
    int endSplash = 1;
    int g = 1;

    boolean canClick = false;

    ArrayList<JButton> guesses = new ArrayList<JButton>();
    ArrayList<JButton> cpu = new ArrayList<JButton>(); //the next button is NOT being added so that the cpu.guess will equal theres
    ArrayList<JButton> cpuChoices = new ArrayList<JButton>();

    Queue<JButton> cpuMoves = new LinkedList<JButton>();
    public JButton button1;
    public JButton button2;
    public JButton button3;
    public JButton button4;
    public JButton start;



    boolean isTrue = true;
    boolean switchColor = true;
    boolean endThread = true;
    MyFrame(){
        super("Simon");
        initialize();

        this.setSize(400, 400);
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        try {
            run();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private void initialize() {
        // TODO Auto-generated method stub


        JPanel squarePane = new JPanel(new GridLayout(2, 2));
        JPanel controlPane = new JPanel(new BorderLayout());
        start = new JButton("Start");
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        button4 = new JButton();
        squarePane.add(button1);
        squarePane.add(button2);
        squarePane.add(button3);
        squarePane.add(button4);

        cpuChoices.add(button1);
        cpuChoices.add(button2);
        cpuChoices.add(button3);
        cpuChoices.add(button4);

        button1.addActionListener(this);
        button2.addActionListener(this);
        button3.addActionListener(this);
        button4.addActionListener(this);

        controlPane.add(squarePane);


        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.add(controlPane);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);



    }
    public void splash(){
        while(isTrue){
            switch(g){
            case 1:

                button1.setBackground(Color.green);
                button2.setBackground(Color.red);
                button4.setBackground(Color.yellow);
                button3.setBackground(Color.blue);

                if(!isTrue){
                    return;
                }

                try {
                    Thread.sleep(frameRate);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if(endSplash==4){
                    return;
                }

                break;
            case 2:
                button2.setBackground(Color.green);
                button4.setBackground(Color.red);
                button3.setBackground(Color.yellow);
                button1.setBackground(Color.blue);


                try {
                    Thread.sleep(frameRate);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            case 3:
                button4.setBackground(Color.green);
                button3.setBackground(Color.red);
                button1.setBackground(Color.yellow);
                button2.setBackground(Color.blue);


                try {
                    Thread.sleep(frameRate);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            case 4:
                button3.setBackground(Color.green);
                button1.setBackground(Color.red);
                button2.setBackground(Color.yellow);
                button4.setBackground(Color.blue);


                try {
                    Thread.sleep(frameRate);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e
                    .printStackTrace();
                }
                g=0;

                endSplash++;

                break;

            }
            g++;
        }
        refresh();

        switchColor = true;
    }



    private void run() throws InterruptedException {
        // TODO Auto-generated method stub
        splash();
        flashButtons();
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        flashButtons();
        Thread.sleep(500);
        runLevel();
    }




    private void runLevel() {
        // TODO Auto-generated method stub
        System.out.println("Level"+ level);
        for(int i=0;i<level;i++){
            Random rand = new Random();
            int num = rand.nextInt(4);
            System.out.println(num);
            cpu.add(cpuChoices.get(num));
        }
        for(int i=0;i<cpu.size();i++){
            System.out.println("Level "+level+" i "+i+" "+cpu.get(i));
            cpu.get(i).setBackground(Color.white);
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(cpu.get(i)==button1)
                cpu.get(i).setBackground(Color.green);
            else if(cpu.get(i)==button2)
            cpu.get(i).setBackground(Color.red);
            else if (cpu.get(i)==button3)
            cpu.get(i).setBackground(Color.blue);
            else if (cpu.get(i)==button4)
            cpu.get(i).setBackground(Color.yellow);

            ;
        }
            transferData();
            cpu.clear();
    }




    private void transferData() {
        // TODO Auto-generated method stub
        for(int i=0;i<cpu.size();i++){
            cpuMoves.offer(cpu.get(i));
        }
    }




    Runnable r2 = new Runnable() {
        public void run() {
            while(isTrue){
                button1.doClick(250);
                button2.doClick(250);
                button3.doClick(250);
                button4.doClick(250);
            }
        }
    };

    public void refresh(){
        this.invalidate();
        this.validate();
        this.repaint();
    }

    public void flashButtons(){
        button1.setBackground(Color.white);
        button2.setBackground(Color.white);
        button3.setBackground(Color.white);
        button4.setBackground(Color.white);
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        button1.setBackground(Color.green);
        button2.setBackground(Color.red);
        button3.setBackground(Color.blue);
        button4.setBackground(Color.yellow);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == button1){
            this.checkGuess(button1);
        } else if(e.getSource()==button2){
            this.checkGuess(button2);
        } else if(e.getSource()==button3){
            this.checkGuess(button3);
        } else if(e.getSource()==button4){
            this.checkGuess(button4);
        }

    }
    public void checkGuess(JButton b){
        //cpuMoves.add(button1);
                if(cpuMoves.peek()!=b){
                    button1.setBackground(Color.black);
                    button2.setBackground(Color.black);
                    button3.setBackground(Color.black);
                    button4.setBackground(Color.black);
                    int reply = JOptionPane.showConfirmDialog(null, "You lost, run again?", "You are a Loser", JOptionPane.YES_NO_OPTION);
                    if(reply==0){
                        this.splash();
                    } else {
                        this.setVisible(false);
                        this.dispose();
                    }
                }

                cpuMoves.poll();
                if(cpuMoves.isEmpty()){
                    level++;
                    try {
                        Thread.sleep(250);
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    runLevel();
                }
                guess++;
                System.out.println(cpu.size());


    }
}
4

0 回答 0