猜拳游戏

项目简介

通过Java实现一个和计算机交互的猜拳游戏。人和计算机各选一项,然后判断胜负。程序猜拳为随机选取,在用户选择后才会和用户的选择同时呈现并判断胜负。游戏一直进行直到用户退出为止,并且输出双方输赢次数和总局数。项目需做到能够直观显示用户和电脑的出拳并且游戏具有随机性和准确性,保证了游戏的公平性。

架构图

java人机猜拳面向对象 java猜拳小游戏_java人机猜拳面向对象

项目采用技术

  • GUI

项目亮点

  • 可以重新开始 即可以清空计数,图片,回到开始状态
  • 打赏小彩蛋(不是真付款码)

项目展示

代码:

Main.java

import javax.swing.*;

public class Main extends JFrame {
    public static void main(String[] args) {
        Fight frame = new Fight();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            //设置窗体关闭按钮;
        frame.setVisible(true);
    }
}

TestFight.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestFight extends JFrame {
    public static void main(String[] args) {
        Fight fight = new Fight();
        fight.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                                    //设置窗体关闭按钮;
        fight.setVisible(true);
    }
}
class Fight extends JFrame implements ActionListener  {
    JButton jiandaoB, shitouB, buB, restartB,supportB;                                     //把JButton组件定义放到类里面,方便其他函数调用;
    JPanel panel1, panel2;                                                                //把JPanel组件定义放到类里面,方便其他函数调用;
    JLabel computer,person,label;                                                        //把JLabel组件定义放到类里面,方便其他函数调用;
    ImageIcon zhuP,shitouP,jiandaoP,buP,monP;                                           //把ImageIcon组件定义放到类里面,方便其他函数调用
    int peopleWin=0;                                                                   //玩家胜利局数
    int computerWin=0;                                                                //电脑胜利局数
    int  sum=0;                                                                      //总局数
    int m=1,n=2,o=3,p=0,r=9;                                                        //对话框
    public Fight() {                                                               //Fight类
        setTitle("石头剪刀布");                                                     //设置窗体标题;
        setLocation(600, 250);                                             //设置窗体左上顶点坐标;
        setSize(1000, 600);                                        //设置窗体大小;
        panel1 = new JPanel();                                                 //添加一个JPanel对象,名为panel1,方便布局;
        panel2 = new JPanel();                                                //添加一个新的JPanel对象,名为panel2;
        panel1.setBackground(Color.pink);                                    //设置panel1颜色为白色;
        panel2.setBackground(Color.cyan);                                   //设置panel2颜色为白色;
        shitouB = new JButton("石头");                                 //添加一个JButton对象,名为shitou;
        jiandaoB = new JButton("剪刀");                               //添加一个JButton对象,名为jiandao;
        buB = new JButton("布");                                     //添加一个JButton对象,名为bu;
        restartB = new JButton("重新开始");                          //添加一个JButton对象,名为重新开始;
        supportB = new JButton("打赏");                            //添加一个JButton对象,名为打赏;
        label = new JLabel("选择剪刀、石头、布开始游戏");
        computer=new JLabel("");
        person=new JLabel("");
        shitouB.addActionListener(this);                                         //给shitouB添加事件约束;
        buB.addActionListener(this);                                            //给buB添加事件约束;
        jiandaoB.addActionListener(this);                                      //给jiandaoB添加事件约束;
        restartB.addActionListener(this);                                     //给restartB添加事件约束;
        supportB.addActionListener(this);                                      //给supportB添加事件约束;
        panel1.add(shitouB);                                                    //把shitouB放进panel1中;
        panel1.add(jiandaoB);                                                //把jiandaoB放进panel1中;
        panel1.add(buB);                                                        //把buB放进panel1中;
        panel1.add(restartB);                                                   //把restartB放进panel1中;
        panel1.add(supportB);                                                  //把supportB放进panel1中;
        panel2.add(label);                                                    //把label放进panel2中;
        panel2.add(computer);                                                //把computer放进panel2中;
        panel2.add(person);                                                 //把person放进panel2中;
        add(panel1, BorderLayout.NORTH);                                        //把panel1添加到窗体的北面;
        add(panel2, BorderLayout.CENTER);                                       //把panel2添加到窗体的中间;
        zhuP=new ImageIcon("D:/mkn/test/img/"+p+".png");
        shitouP= new ImageIcon("D:/klw/TestFight/img/"+m+".png");
        jiandaoP= new ImageIcon("D:/klw/TestFight/img/"+n+".png");
        buP= new ImageIcon("D:/klw/TestFight/img/"+o+".png");
        monP= new ImageIcon("D:/klw/TestFight/img/"+r+".png");
        label.setIcon(zhuP);
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == shitouB) {                                        //如果你按下的是shitou按键时,就会执行以下操作;
            int i = ((int) (Math.random() * 10)) % 3;                                //定义一个int型变量i,利用Math.random()函数获取随机数,因为*10所以随机数区间为[0,10),最后强制转换为int型,再除3取余,最后的赋值给i;
            switch (i)                                                        //i为0时就执行case 0操作,以此类推;
            {
                case 0:
                    sum++;
                    computer.setText("电脑");
                    person.setText("玩家");
                    person.setIcon(shitouP);
                    computer.setIcon(shitouP);
                    label.setIcon(null);
                    label.setText("你出石头,电脑出石头,平局!");
                    n=JOptionPane.showConfirmDialog(this, "比赛进行了:"+sum+"次--您赢了:"+peopleWin+"次--电脑赢了:"+computerWin+"次 还要继续玩吗?(是-继续,否-关闭程序)","确认对话框",JOptionPane.YES_NO_OPTION) ;                    //编辑确认对话框
                    if(n==JOptionPane.NO_OPTION) {                           //如果选择否的话,则重置总局数,玩家胜利局数,电脑胜利局数
                        dispose();
                    }
                    break;            //当i=0时,执行后续操作,到break结束;(必须要添加break,不然会一直执行下去);
                case 1:
                    sum++;
                    peopleWin++;
                    computer.setText("电脑");
                    person.setText("玩家");
                    person.setIcon(shitouP);
                    computer.setIcon(jiandaoP);
                    label.setIcon(null);
                    label.setText("你出石头,电脑出剪子,恭喜你赢了!");
                    n=JOptionPane.showConfirmDialog(this, "比赛进行了:"+sum+"次--您赢了:"+peopleWin+"次--电脑赢了:"+computerWin+"次 还要继续玩吗?(是-继续,否-关闭程序)","确认对话框",JOptionPane.YES_NO_OPTION) ;
                    if(n==JOptionPane.NO_OPTION) {
                        dispose();
                    }
                    break;        //当i=1时,与上同理;
                case 2:
                    sum++;
                    computerWin++;
                    computer.setText("电脑");
                    person.setText("玩家");
                    person.setIcon(shitouP);
                    computer.setIcon(buP);
                    label.setIcon(null);
                    label.setText("你出石头,电脑出布,很遗憾你输了!");
                    n=JOptionPane.showConfirmDialog(this, "比赛进行了:"+sum+"次--您赢了:"+peopleWin+"次--电脑赢了:"+computerWin+"次 还要继续玩吗?(是-继续,否-关闭程序)","确认对话框",JOptionPane.YES_NO_OPTION) ;
                    if(n==JOptionPane.NO_OPTION) {
                        dispose();
                    }
                    break;        //当i=2时,与上同理;
            }
        } else if (e.getSource() == buB) {                                    //如果你按下的是bu按键时,与上同理;
            int i = ((int) (Math.random() * 10)) % 3;
            switch (i) {
                case 0:
                    sum++;
                    computer.setText("电脑");
                    person.setText("玩家");
                    person.setIcon(buP);
                    computer.setIcon(buP);
                    label.setIcon(null);
                    label.setText("你出布,电脑出布,平局!");
                    n=JOptionPane.showConfirmDialog(this, "比赛进行了:"+sum+"次--您赢了:"+peopleWin+"次--电脑赢了:"+computerWin+"次 还要继续玩吗?(是-继续,否-关闭程序)","确认对话框",JOptionPane.YES_NO_OPTION) ;
                    if(n==JOptionPane.NO_OPTION) {
                        dispose();
                    }
                    break;
                case 1:
                    sum++;
                    computer.setText("电脑");
                    person.setText("玩家");
                    person.setIcon(buP);
                    computer.setIcon(shitouP);
                    label.setIcon(null);
                    peopleWin++;
                    label.setText("你出布,电脑出石头,恭喜你赢了!");
                    n=JOptionPane.showConfirmDialog(this, "比赛进行了:"+sum+"次--您赢了:"+peopleWin+"次--电脑赢了:"+computerWin+"次 还要继续玩吗?(是-继续,否-关闭程序)","确认对话框",JOptionPane.YES_NO_OPTION) ;
                    if(n==JOptionPane.NO_OPTION) {
                        dispose();
                    }
                    break;
                case 2:
                    sum++;
                    computer.setText("电脑");
                    person.setText("玩家");
                    person.setIcon(buP);
                    computer.setIcon(jiandaoP);
                    label.setIcon(null);
                    computerWin++;
                    label.setText("你出布,电脑出剪刀,很遗憾你输了!");
                    n=JOptionPane.showConfirmDialog(this, "比赛进行了:"+sum+"次--您赢了:"+peopleWin+"次--电脑赢了:"+computerWin+"次 还要继续玩吗?(是-继续,否-关闭程序)","确认对话框",JOptionPane.YES_NO_OPTION) ;
                    if(n==JOptionPane.NO_OPTION) {
                        dispose();
                    }
                    break;
            }
        } else if (e.getSource() == jiandaoB) {                                //当你按下jiandao按键时,与上同理;
            int i = ((int) (Math.random() * 10)) % 3;
            switch (i) {
                case 0:
                    sum++;
                    computer.setText("电脑");
                    person.setText("玩家");
                    person.setIcon(jiandaoP);
                    computer.setIcon(jiandaoP);
                    label.setIcon(null);
                    label.setText("你出剪刀,电脑出剪刀,平局!");
                    n=JOptionPane.showConfirmDialog(this, "比赛进行了:"+sum+"次--您赢了:"+peopleWin+"次--电脑赢了:"+computerWin+"次 还要继续玩吗?(是-继续,否-关闭程序)","确认对话框",JOptionPane.YES_NO_OPTION) ;
                    if(n==JOptionPane.NO_OPTION) {
                        dispose();
                    }
                    break;
                case 1:
                    sum++;
                    peopleWin++;
                    computer.setText("电脑");
                    person.setText("玩家");
                    person.setIcon(jiandaoP);
                    computer.setIcon(buP);
                    label.setIcon(null);
                    label.setText("你出剪刀,电脑出布,恭喜你赢了!");
                    n=JOptionPane.showConfirmDialog(this, "比赛进行了:"+sum+"次--您赢了:"+peopleWin+"次--电脑赢了:"+computerWin+"次 还要继续玩吗?(是-继续,否-关闭程序)","确认对话框",JOptionPane.YES_NO_OPTION) ;
                    if(n==JOptionPane.NO_OPTION) {
                        dispose();
                    }
                    break;
                case 2:
                    sum++;
                    computerWin++;
                    computer.setText("电脑");
                    person.setText("玩家");
                    person.setIcon(jiandaoP);
                    computer.setIcon(shitouP);
                    label.setIcon(null);
                    label.setText("你出剪刀,电脑出石头,很遗憾你输了!");
                    n=JOptionPane.showConfirmDialog(this, "比赛进行了:"+sum+"次--您赢了:"+peopleWin+"次--电脑赢了:"+computerWin+"次 还要继续玩吗?(是-继续,否-关闭程序)","确认对话框",JOptionPane.YES_NO_OPTION) ;
                    if(n==JOptionPane.NO_OPTION) {
                        dispose();
                    }
                    break;
            }
        } else if (e.getSource() == restartB)                                    //当你按下clear按键时,直接输出label.setText();
        {   label.setIcon(zhuP);
            person.setIcon(null);
            computer.setIcon(null);
            computer.setText(null);
            person.setText(null);
            sum=0;
            peopleWin=0;
            computerWin=0;
            label.setText("选择剪刀、石头、布开始游戏");
        }else if (e.getSource()== supportB){
            person.setIcon(null);
            computer.setIcon(null);
            label.setIcon(monP);
            label.setText("制作不易,多谢打赏");
            computer.setText(null);
            person.setText(null);
        }
    }
}
效果演示:

java人机猜拳面向对象 java猜拳小游戏_关闭程序_02