JavaIdot 2022-01-26 16:28 采纳率: 50%
浏览 29
已结题

石头剪刀布的游戏有问题

我本来想做一个石头剪刀布的游戏哩,有两个类:Main.java和Game.java。其中Main.java是启动类。
Game.java的代码如下:


package surface;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class Game extends JFrame implements ActionListener {
    JMenuBar mb;
    JComboBox<String> box;
    static String[] way = new String[] {"石头","剪刀","布"};
    Label yourLabel;
    int randomNum;
    int count = 1;
    Object[][] table = new Object[3][3]; 
    
    /**
     * 游戏
     */
    public Game() {
        this.launch();
        this.addCompoments();
    }
    
    
    /**
     * 
     */
    private void getHelp() {
        String str = "这个游戏的灵感来自于我们耳熟能详的游戏——石头剪刀布。\n"
                + "这个游戏采用了三局两胜制,下面有本次打开日志,可以自行查看。\n"
                + "注:如果平局,算电脑获胜。";
        JOptionPane.showMessageDialog(this, str);
    }
    
    
    /**
     * 添加组件
     */
    private void addCompoments() {
        //获取容器
        Container con = getContentPane();
        con.setLayout(null);
        
        //菜单栏
        mb = new JMenuBar();
        
        JMenu m1 = new JMenu("文件");
        JMenuItem mi1 = new JMenuItem("确定(O)");
        mi1.addActionListener(this);
        m1.add(mi1);
        mb.add(m1);
        
        JMenu m2 = new JMenu("帮助");
        JMenuItem mi2 = new JMenuItem("游戏操控(K)");
        mi2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                getHelp();
            }
            
        });
        m2.add(mi2);
        mb.add(m2);
        
        this.setJMenuBar(mb);

        
        //题目
        JLabel title = new JLabel("<html><center><b>石头剪刀布<b></center></html>");
        title.setFont(new Font("微软雅黑",Font.BOLD,25));
        title.setBounds(70,10,160,30);
        con.add(title);
        
        //标签1
        Label myLabel = new Label("请选择你出的拳:");
        myLabel.setBounds(0, 50, 100, 30);
        con.add(myLabel);
        
        //准备选择框
        box = new JComboBox<>();
        box.addItem("石头");
        box.addItem("剪刀");
        box.addItem("布");
        box.setBounds(0, 90, getWidth() - 18, 25);
        con.add(box);
        
        //标签2
        yourLabel = new Label("电脑出了:");
        yourLabel.setBounds(0, 125, getWidth(), 30);
        con.add(yourLabel);
        
        //按钮“确定”
        Button ok = new Button("确定");
        ok.setBounds(110, 155, 60, 30);
        
        ok.addActionListener(this);
        
        con.add(ok);
        

    }

    /**
     * 初始化本身
     */
    private void launch() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300,300);
        setLocationRelativeTo(null);
        setResizable(false);
        setTitle("石头剪刀布");
        
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
    /**
     * 检查
     */
    private void update(String whoWin) {
        table[count-1][count-1] = new Object[] {count-1,whoWin};
        count++;
    }
    
    /**
     * 检查是否结束
     */
    private void checkCanOver() {
        if (!(count<6)) {
            dispose();
            JOptionPane.showMessageDialog(null, "本次游戏结束。");
            
            //投票
            int myIndex = 0,computerIndex = 0;
            String winner;
            int style;
            
            for (int i = 0;i < 3;i++) {
                String who = table[i][i].toString();
                if (who=="computer") {
                    computerIndex++;
                } else if (who=="me") {
                    myIndex++;
                }
            }
            
            if (myIndex > computerIndex) {
                winner = "电脑";
                style = JOptionPane.ERROR_MESSAGE;
            } else {
                winner = "你";
                style = JOptionPane.INFORMATION_MESSAGE;
            }
            
            JOptionPane.showMessageDialog(this,
                    "最终的赢家是:"+winner+"!",
                    winner,
                    style
                );
        } else {
            return;
        }
    }
    
    /**
     * 点击事件
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (count < 6) {
            Random rand = new Random();
            randomNum = rand.nextInt(3);
            yourLabel.setText("电脑出了:" + way[randomNum]);
            
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                System.err.println("InterruptedException.");
            }
            
            StringBuilder sb = new StringBuilder();
            sb.append("电脑出的是");
            sb.append(way[randomNum]);
            sb.append(",你出的是");
            sb.append(box.getSelectedItem());
            sb.append("。");
            
            JOptionPane.showMessageDialog(getContentPane(), sb.toString());
            
            sb = null;
            System.gc();
        
            check();
        }
    }
    
    /**
     * 检查
     */
    private void check() {
        //检查
        final int myChoice = box.getSelectedIndex() + 1;
        final int yourChoice = randomNum + 1;
        
        //判断
        if (myChoice==yourChoice) {
            JOptionPane.showMessageDialog(this, "平局!(如果平局算作电脑赢)");
            update("computer");
            
            count++;
            System.out.println(count);
            checkCanOver();
            return;
        }
        
        /*
         * 1=石头
         * 2=剪刀
         * 3=布
         */
        
        switch (myChoice) {
        case 1:
            System.out.println(1);
            if (yourChoice==2) {
                youWin();
                update("me");

            } else {
                youLose();
                update("computer");

            }
            
            count ++;
            break;
        case 2:
            System.out.println(2);
            if (yourChoice==3) {
                youWin();
                update("me");

            } else {
                youLose();
                update("computer");

            }
            
            count++;
            break;
        case 3:
            System.out.println(3);
            if (yourChoice==1) {
                youWin();
                update("me");

            } else {
                youLose();
                update("computer");

            }
            
            count++;
            break;
        }

        System.out.println(count);
        checkCanOver();
    }
    
    /**
     * 你赢了
     */
    private void youWin() {
        JOptionPane.showMessageDialog(null, 
                "在这一轮,你胜利了!",
                "胜利",
                JOptionPane.INFORMATION_MESSAGE);
    }
    
    /**
     * 你输了
     */
    private void youLose() {
        JOptionPane.showMessageDialog(null, 
                "在这一轮,你失败了!",
                "失败",
                JOptionPane.ERROR_MESSAGE);
    }
}

Main.java的代码如下:

package main;

import surface.Game;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Game().setVisible(true);
    }

}


其中Game.count有一些Bug,请修复一下。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 2月3日
    • 创建了问题 1月26日

    悬赏问题

    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。
    • ¥15 livecharts wpf piechart 属性
    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了