24点游戏

24点游戏是经典的纸牌益智游戏。
常见游戏规则:
从扑克中每次取出4张牌。使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要求编程解决24点游戏。
功能:
1.随机生成4个代表扑克牌牌面的数字字母。
2.程序自动列出所有可能算出24的表达式。
3.具有游戏帮助功能。
算法思路:
1.用java的swing来创建窗口UI。
2.把随机产生的四个数字保存在hashSet对象中,然后在从此对象取出这 四个数字当做四张卡牌的号码。
3.利用穷举法列出所有能组成24点的表达式。首先随机确定好了卡牌号 码,然后通过3重循环,组合排列出所有运算符的组合,在第一层循环,对 号码数字进行两两运算,在下层循环,再把这个运算后的结果和后面一个数 进行运算,以此类推。
实现截屏:
界面:
24点游戏
自动生成卡牌:
24点游戏
游戏帮助:
24点游戏

mainFrame

package xust.edn.cn.view;
import xust.edn.cn.listen.action;
import xust.edn.cn.listen.exit;
import xust.edn.cn.listen.helpAction;
import xust.edn.cn.listen.randAction;


import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.dnd.DropTarget;
import java.io.File;

public class mainFrame extends JFrame {
    private JPanel jpb ;
    private JPanel []jpImage;

    private JButton jb1,jb2,jb3,jb4;


    public void initView() {
        jpb = new JPanel();
        jpImage = new JPanel[6];
        jpImage[4] = new JPanel();
        jpImage[5] = new JPanel();
        for (int time = 3; time >= 0; time--) {
            jpImage[time] = new JPanel() {
                public void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    ImageIcon icon = new ImageIcon("src//back.jpg");
                    Image image = icon.getImage();
                    g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
                }
            };
        }


        jb1 = new JButton("自动生成卡牌");
        jb2 = new JButton("玩家生成卡牌");
        jb3 = new JButton("游戏帮助");
        jb4 = new JButton("退出");


    }

    public  void setLayouts(){
        this.setLayout(new GridLayout(3,1));
        jpb.setLayout(new GridLayout(2,2,5,5));
        jpImage[4].setLayout(new GridLayout(1,2,10,30));
        jpImage[5].setLayout(new GridLayout(1,2,10,30));
    }

    public void addAction(){
        jb1.addActionListener(new randAction(this));
        jb2.addActionListener(new action());
        jb3.addActionListener(new helpAction(this));
        jb4.addActionListener(new exit(this));
    }

    public void addSurface(){
        jpb.add(jb1);  jpb.add(jb2);  jpb.add(jb3); jpb.add(jb4);

        jpImage[4].add(jpImage[0]);
        jpImage[4].add(jpImage[1]);
        jpImage[5].add(jpImage[2]);
        jpImage[5].add(jpImage[3]);

        this.add(jpImage[4]);
        this.add(jpImage[5]);
        this.add(jpb);
    }

    public void setView(){
        this.setTitle("24点游戏");
        this.setSize(300,500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
    }

    public  mainFrame(){
        this.initView();
        this.setLayouts();
        this.addSurface();
        this.addAction();
        this.setView();
    }

    public static void main(String[] args) {

        mainFrame myFrame = new mainFrame();
    }
}

randUI

package xust.edn.cn.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.HashSet;
import java.util.Iterator;

public class randUI {
    private Dialog dlg;
    private JPanel []jp;
    private JLabel[] jlb;
    private JTextArea jt;
    private JScrollPane jsp;
    private mainFrame mainFrame;
    private ImageIcon[] icon;
    private Integer max = 12, min = 0;
    private int q, w, e, r;
    private int num = 0;

    public randUI(mainFrame mainFrame) {
        this.mainFrame = mainFrame;

        this.initUI();
        this.randCard();
        this.createCard();
        this.setLayouts();
        this.calculator();
        this.addUI();
        this.setUI();
    }

    public void initUI() {
        dlg = new Dialog(mainFrame, "自动生成卡牌");
        dlg.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                dlg.dispose();
            }
        });

        jp = new JPanel[2];
        jp[0] = new JPanel();
        jp[1] = new JPanel();

        jlb = new JLabel[4];

        jt = new JTextArea();
        jsp = new JScrollPane(jt);
    }


    public void addUI() {
        jp[0].add(jlb[0]);
        jp[0].add(jlb[1]);
        jp[0].add(jlb[2]);
        jp[0].add(jlb[3]);
        jp[1].add(jsp);

        dlg.add(jp[0]);
        dlg.add(jp[1]);
    }

    public void setLayouts(){
        dlg.setLayout(new GridLayout(2,1));
        jp[0].setLayout(new GridLayout(2,2));
        jp[1].setLayout(new GridLayout(1,1));
    }

    public void setUI() {
        dlg.setSize(350, 680);
        dlg.setLocation(450, 200);
        dlg.setVisible(true);

    }

    //产生4个图片的号码
    public void randCard() {
        HashSet<Integer> hashSet = new HashSet<Integer>();
        int sign = 0;

        for (int i = 0; i < 4; i++) {
            int temp = (int) (Math.random() * (max - min)) + min;
            hashSet.add(temp);
        }

        Iterator<Integer> it = hashSet.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            if (sign == 0) {
                q = ((Integer) obj).intValue();
            } else if (sign == 1) {
                w = ((Integer) obj).intValue();
            } else if (sign == 2) {
                e = ((Integer) obj).intValue();
            } else {
                r = ((Integer) obj).intValue();
            }
            sign++;
        }
    }

    public void createCard() {
        icon = new ImageIcon[13];
        String[] str ={"src//1.jpg","src//2.jpg",
                        "src//3.jpg","src//4.jpg",
                        "src//5.jpg","src//6.jpg",
                        "src//7.jpg","src//8.jpg",
                        "src//9.jpg","src//10.jpg",
                        "src//11.jpg","src//12.jpg",
                        "src//13.jpg"};

            for(int i = 0;i<13;i++){
                icon[i] = new ImageIcon(str[i]);
                 }

            jlb[0] = new JLabel(icon[q]);
            jlb[1] = new JLabel(icon[w]);
            jlb[2] = new JLabel(icon[e]);
            jlb[3] = new JLabel(icon[r]);


        }

    public void calculator(){
        int []pokerNum = new int[]{q+1,w+1,e+1,r+1};
        operator(pokerNum);
    }

    public void operator(int[]pokerNum){
        double sum=0;
        for(int i=0;i<4;i++){
            double sum1=code(pokerNum[0],pokerNum[1],i);
            for(int j=0;j<4;j++){
                double sum2=code(sum1,pokerNum[2],j);
                for(int k=0;k<4;k++){
                    sum=code(sum2,pokerNum[3],k);
                    int[] symbolNum={i,j,k};
                    String[] symbol=new String[4];
                    symbol=symbol(symbolNum);
                    if(sum==24){
                        num++;
                        if(num == 1){
                            jt.append("恭喜你获胜\n");
                        }
                        jt.append("["+"("+pokerNum[0]+" "+symbol[0]+" "+pokerNum[1]+")"+" "+symbol[1]+" "+pokerNum[2]+"]"+" "+symbol[2]+" "+pokerNum[3]+"=24"+"\n");
                    }
                }
            }
        }
        if(num == 0){
            jt.append("很遗憾,这四张牌无法组成24点");
        }
        jt.setEditable(false);
    }

    public double code(double num1,double num2,int num){
        double sum=0.0;
        if(num==0){
            sum=num1+num2;
        }else if(num==1){
            sum=num1-num2;
        }else if(num==2){
            sum=num1*num2;
        }else{
            sum=num1/num2;
        }
        return sum;

    }

    public String[] symbol(int[] symbolNum){
        String[] symbol=new String[4];
        for(int i=0;i<3;i++){
            int sym=symbolNum[i];
            switch (sym) {
                case 0:
                    symbol[i]="+";  break;
                case 1:
                    symbol[i]="-";	break;
                case 2:
                    symbol[i]="x";	break;
                case 3:
                    symbol[i]="÷";	break;
                default:			break;
            }
        }
        return symbol;
    }

    }


helpUI

package xust.edn.cn.view;

import xust.edn.cn.listen.helpAction;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class helpUI  {
    private mainFrame helpFrame;
    private Dialog dlg;
    private JLabel jlb;
    private JButton jb;

    public Dialog getDlg() {
        return dlg;
    }

    public JLabel getJlb() {
        return jlb;
    }

    public JButton getJb() {
        return jb;
    }

    public helpUI(mainFrame helpF){
        this.helpFrame = helpF;
        this.produceUI();
    }


    public void produceUI(){
        dlg = new JDialog(helpFrame,"游戏帮助");
        jlb = new JLabel();
        jb = new JButton("确定");

        jlb.setSize(300,200);
        jlb.setFont(new Font("宋体",1,15));
        jlb.setText("<html><body>游戏名称:24点游戏<br>" +
                "游戏类型:益智类<br>"+
                "游戏介绍:<br>"+
                "牌库*有1-13张不同的卡牌,其中J<br>" +
                "代表11,Q代表12,K代表13,玩家通<br>" +
                "过手动输入或自动生成两种方式获得牌<br>" +
                "库里的四张卡牌,如果这四张卡牌通过<br>" +
                "某一种四则运算(加减乘除)得出的结<br>" +
                "果为24,则玩家胜利,否则失败。</body></html>");

        dlg.add(jlb);
        dlg.add(jb);

        dlg.setSize(400,300);
        dlg.setLocation(450,250);
        dlg.setLayout(new FlowLayout());
        dlg.setVisible(true);


        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource()==jb){
                    dlg.dispose();
                }
            }
        });
    }
}

helpAction

package xust.edn.cn.listen;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import xust.edn.cn.view.helpUI;
import xust.edn.cn.view.mainFrame;


public class helpAction implements ActionListener {
    private mainFrame help;

    public helpAction(mainFrame help) {
        this.help = help;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        helpUI hUI = new helpUI(help);
    }
}

randAction

package xust.edn.cn.listen;

import xust.edn.cn.view.mainFrame;
import xust.edn.cn.view.randUI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class randAction implements ActionListener {
    private mainFrame mainFrame;

    public randAction(mainFrame mainFrame){
        this.mainFrame = mainFrame;
    }

    public void actionPerformed(ActionEvent event){
        randUI randUI = new randUI(mainFrame);
    }

}

exit

package xust.edn.cn.listen;
import xust.edn.cn.view.mainFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class exit implements ActionListener {
    private mainFrame mainFrame;
    public exit(mainFrame mainFrame){
        this.mainFrame = mainFrame;
    }
    public void actionPerformed(ActionEvent event){
        mainFrame.dispose();
    }
}