⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 twentyfour.java

📁 java 项目 扑克小游戏 源码 24点
💻 JAVA
字号:
package game;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.Stack;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TwentyFour extends JFrame {
	private class CardLoader extends JPanel {

		public void paint(Graphics g) {
			super.paint(g);
			g.drawImage(img, 0, 0, 71, 96, this);
		}

		private static final long serialVersionUID = 1L;
		private Image img;
		private int point;
		final TwentyFour this$0;

		public CardLoader(int i) {
			this$0 = TwentyFour.this;
			point = i % 100;
			String imgFile = "./images/";
			imgFile = (new StringBuilder(String.valueOf(imgFile))).append(i)
					.append(".gif").toString();
			img = (new ImageIcon(imgFile)).getImage();
			setSize(71, 96);
		}
	}

	private class MyCal {

		int co(char a, char b) {
			if (a == '+' || a == '-')
				return b != '+' && b != '-' && b != ')' && b != '#' ? -1 : 1;
			if (a == '*' || a == '/')
				return b != '(' ? 1 : -1;
			if (a == '(' || a == '#') {
				if (a == '(' && b == ')')
					return 0;
				if (a == '(' && b == '#')
					return 2;
				if (a == '#' && b == ')')
					return 2;
				return a != '#' || b != '#' ? -1 : 0;
			}
			if (a == ')')
				return b != '(' ? 1 : 2;
			else
				return 0;
		}

		boolean isnum(char ch) {
			return ch >= '0' && ch <= '9' || ch == '.';
		}

		boolean issign(char ch) {
			return ch == '+' || ch == '-' || ch == '*' || ch == '/'
					|| ch == '(' || ch == ')' || ch == '#';
		}

		double cal(double a, char s, double b) {
			if (s == '+')
				return a + b;
			if (s == '-')
				return b - a;
			if (s == '*')
				return a * b;
			if (s == '/')
				return b / a;
			else
				return 0.0D;
		}

		double calculate(String s) {
			s = (new StringBuilder(String.valueOf(s))).append("#").toString();
			int i = 0;
			Stack num = new Stack();
			Stack op = new Stack();
			op.push(Character.valueOf('#'));
			for (char c = s.substring(i, i + 1).charAt(0); c != '#'
					|| ((Character) op.peek()).toString().charAt(0) != '#';)
				if (!issign(c)) {
					int start = i;
					int end;
					for (end = 0; !issign(s.substring(i, i + 1).charAt(0)); end = i++)
						;
					double m = Double.parseDouble(s.substring(start, end + 1));
					if (points.indexOf(Integer.valueOf((int) m)) == -1)
						return -999D;
					points.remove(points.indexOf(Integer.valueOf((int) m)));
					num.push(Double.valueOf(m));
					c = s.substring(i, i + 1).charAt(0);
				} else {
					switch (co(((Character) op.peek()).charValue(), c)) {
					case -1:
						op.push(Character.valueOf(c));
						i++;
						c = s.substring(i, i + 1).charAt(0);
						break;

					case 0: // '\0'
						op.pop();
						i++;
						c = s.substring(i, i + 1).charAt(0);
						break;

					case 1: // '\001'
						double a = ((Double) num.pop()).doubleValue();
						double b = ((Double) num.pop()).doubleValue();
						char ss = ((Character) op.pop()).charValue();
						num.push(Double.valueOf(cal(a, ss, b)));
						break;
					}
				}

			return ((Double) num.peek()).doubleValue();
		}

		final TwentyFour this$0;

		private MyCal() {
			this$0 = TwentyFour.this;
		}

		MyCal(MyCal mycal) {
			this();
		}
	}

	public TwentyFour() {
		jContentPanel = null;
		jCardPanel = null;
		jAnswerPanel = null;
		jFootPanel = null;
		jButtonPanel = null;
		answer = null;
		menuBar = null;
		menu1 = null;
		menu2 = null;
		initialize();
		addEventHandler();
	}

	private void initialize() {
		setSize(300, 325);
		setContentPane(getJContentPane());
		setTitle("24\u70B9\u667A\u529B\u5C0F\u6E38\u620F");
		setJMenuBar(getMebuBar());
		pack();
		Dimension dim = getToolkit().getScreenSize();
		Rectangle abounds = getBounds();
		setLocation((dim.width - abounds.width) / 2,
				(dim.height - abounds.height) / 2);
		setDefaultCloseOperation(3);
	}

	private JMenuBar getMebuBar() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(menu1 = new JMenu("\u6E38\u620F"));
			menuBar.add(menu2 = new JMenu("\u5E2E\u52A9"));
			menu1.add(menuItem11 = new JMenuItem("\u5F00\u59CB"));
			menu1.addSeparator();
			menu1.add(menuItem12 = new JMenuItem("\u9000\u51FA"));
			menu2.add(menuItem21 = new JMenuItem("\u5E2E\u52A9"));
			menu2.add(menuItem22 = new JMenuItem("\u5173\u4E8E"));
		}
		return menuBar;
	}

	private Container getJContentPane() {
		if (jContentPanel == null) {
			jContentPanel = new JPanel();
			jContentPanel.setLayout(new GridLayout(2, 1));
			jContentPanel.add(getJCardPanel(), "Center");
			jContentPanel.add(getJFootPanel(), "South");
		}
		return jContentPanel;
	}

	private Component getJAnswerPanel() {
		if (jAnswerPanel == null) {
			jAnswerPanel = new JPanel();
			jAnswerPanel.setLayout(new FlowLayout());
			jAnswerPanel.add(new JLabel(
					"\u8BF7\u8F93\u5165\u4F60\u7684\u7B54\u6848\uFF1A"));
			jAnswerPanel.add(answer = new JTextField(15));
		}
		return jAnswerPanel;
	}

	private Component getJFootPanel() {
		if (jFootPanel == null) {
			jFootPanel = new JPanel();
			jFootPanel.setLayout(new GridLayout(2, 1, 10, 10));
			jFootPanel.add(getJAnswerPanel());
			jFootPanel.add(getJButtonPanel());
		}
		return jFootPanel;
	}

	private Component getJButtonPanel() {
		if (jButtonPanel == null) {
			jButtonPanel = new JPanel();
			jButtonPanel.setLayout(new FlowLayout(1, 20, 10));
			start = new JButton("\u5F00\u59CB\u6E38\u620F");
			submit = new JButton("\u63D0\u4EA4\u7B54\u6848");
			view = new JButton("\u67E5\u770B\u7B54\u6848");
			jButtonPanel.add(start);
			jButtonPanel.add(submit);
			jButtonPanel.add(view);
		}
		return jButtonPanel;
	}

	private Component getJCardPanel() {
		if (jCardPanel == null) {
			jCardPanel = new JPanel();
			jCardPanel.setLayout(new GridLayout(1, 4, 10, 10));
			jCardPanel.add(new CardLoader(0));
			jCardPanel.add(new CardLoader(0));
			jCardPanel.add(new CardLoader(0));
			jCardPanel.add(new CardLoader(0));
		}
		return jCardPanel;
	}

	public static void main(String args[]) {
		(new TwentyFour()).setVisible(true);
	}

	private void addEventHandler() {
		ActionListener lis = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String cmd = e.getActionCommand();
				if (cmd.equals("\u5F00\u59CB\u6E38\u620F")
						|| cmd.equals("\u5F00\u59CB")
						|| cmd.equals("\u91CD\u65B0\u53D1\u724C")) {
					start.setText("\u91CD\u65B0\u53D1\u724C");
					shufer();
					points = new ArrayList();
					points.add(Integer.valueOf(card1.point));
					points.add(Integer.valueOf(card2.point));
					points.add(Integer.valueOf(card3.point));
					points.add(Integer.valueOf(card4.point));
				}
				if (cmd.equals("\u9000\u51FA"))
					System.exit(0);
				if (cmd.equals("\u5E2E\u52A9")) {
					String help = "";
					try {
						BufferedReader in = new BufferedReader(
								new InputStreamReader(new FileInputStream(
										"./help.txt")));
						for (String temp = null; (temp = in.readLine()) != null;)
							help = (new StringBuilder(String.valueOf(help)))
									.append(temp).append("\n").toString();

					} catch (IOException ioE) {
						ioE.printStackTrace();
					}
					JOptionPane.showMessageDialog(null, help,
							"\u5E2E\u52A9\uFF0D24\u70B9\u5C0F\u6E38\u620F", 1);
				}
				if (cmd.equals("\u5173\u4E8E"))
					JOptionPane.showMessageDialog(null,
							"SD0802\u5C0F\u5802\u5236\u4F5C.\n",
							"\u5173\u4E8E", 1);
				if (cmd.equals("\u63D0\u4EA4\u7B54\u6848"))
					if (!answer.getText().equals("") && isstart) {
						double caculateResult = (new MyCal(null))
								.calculate(answer.getText());
						if ((int) caculateResult == -999)
							JOptionPane
									.showMessageDialog(
											null,
											"\u4F60\u4F7F\u7528\u6251\u514B\u724C\u4E0A\u6CA1\u6709\u7684\u6570\u5B57\uFF01\n\n      \u60F3\u5FFD\u60A0\u6211\uFF1F\uFF01\u6CA1\u95E8\uFF01",
											"\u63D0\u793A", 0);
						else if ((int) caculateResult == 24) {
							JOptionPane
									.showMessageDialog(
											null,
											"\u4F60\u597D\u806A\u660E\u963F\uFF0C\u7B54\u5BF9\u4E86\uFF01",
											"\u606D\u559C", 1);
							shufer();
						} else {
							JOptionPane
									.showMessageDialog(
											null,
											(new StringBuilder(
													"\u9519\u8BEF\uFF01\n\u4F60\u7684\u8BA1\u7B97\u7ED3\u679C\u662F"))
													.append(caculateResult)
													.append(
															",\u4E0D\u7B49\u4E8E24\uFF01")
													.toString(),
											"\u9519\u8BEF", 0);
							answer.setText("");
						}
					} else {
						JOptionPane
								.showMessageDialog(
										null,
										"\u6E38\u620F\u672A\u5F00\u59CB\u6216\u4F60\u8FD8\u6CA1\u6709\u8F93\u5165\u4F60\u7684\u8BA1\u7B97\u8868\u8FBE\u5F0F\u3002",
										"\u9519\u8BEF", 0);
						answer.setText("");
					}
				if (cmd.equals("\u67E5\u770B\u7B54\u6848"))
					if (isstart)
						JOptionPane
								.showMessageDialog(
										null,
										"\u8FD9\u4E48\u7B80\u5355\u7684\u6E38\u620F\u8FD8\u8981\u67E5\u770B\u7B54\u6848\uFF1F\uFF01\n\n     \u4E0D\u7ED9\uFF01\u81EA\u5DF1\u6162\u6162\u60F3\uFF01",
										"\u63D0\u793A", 0);
					else
						JOptionPane.showMessageDialog(null,
								"\u6E38\u620F\u8FD8\u6CA1\u5F00\u59CB\u3002",
								"\u63D0\u793A", 0);
			}

			final TwentyFour this$0;

			{
				this$0 = TwentyFour.this;
			}
		};
		start.addActionListener(lis);
		submit.addActionListener(lis);
		view.addActionListener(lis);
		menuItem11.addActionListener(lis);
		menuItem12.addActionListener(lis);
		menuItem21.addActionListener(lis);
		menuItem22.addActionListener(lis);
	}

	private void shufer() {
		Random rd = new Random((new GregorianCalendar()).getTimeInMillis());
		jCardPanel.removeAll();
		int i1 = Math.abs(rd.nextInt()) % 13 + 1
				+ (Math.abs(rd.nextInt()) % 4 + 1) * 100;
		int i2 = Math.abs(rd.nextInt()) % 13 + 1
				+ (Math.abs(rd.nextInt()) % 4 + 1) * 100;
		int i3 = Math.abs(rd.nextInt()) % 13 + 1
				+ (Math.abs(rd.nextInt()) % 4 + 1) * 100;
		int i4 = Math.abs(rd.nextInt()) % 13 + 1
				+ (Math.abs(rd.nextInt()) % 4 + 1) * 100;
		card1 = new CardLoader(i1);
		card2 = new CardLoader(i2);
		card3 = new CardLoader(i3);
		card4 = new CardLoader(i4);
		jCardPanel.add(card1);
		jCardPanel.add(card2);
		jCardPanel.add(card3);
		jCardPanel.add(card4);
		jCardPanel.updateUI();
		isstart = true;
	}


	private class TwentyFour$CardLoader extends JPanel {

		public void paint(Graphics g) {
			super.paint(g);
			g.drawImage(img, 0, 0, 71, 96, this);
		}

		private static final long serialVersionUID = 1L;
		private Image img;
		private int point;
		final TwentyFour this$0;

		public TwentyFour$CardLoader(int i) {
			this$0 = TwentyFour.this;
			point = i % 100;
			String imgFile = "./images/";
			imgFile = (new StringBuilder(String.valueOf(imgFile))).append(i)
					.append(".gif").toString();
			img = (new ImageIcon(imgFile)).getImage();
			setSize(71, 96);
		}
	}

	private static final long serialVersionUID = 1L;
	private JPanel jContentPanel;
	private JPanel jCardPanel;
	private JPanel jAnswerPanel;
	private JPanel jFootPanel;
	private JPanel jButtonPanel;
	private JTextField answer;
	private JMenuBar menuBar;
	private JMenu menu1;
	private JMenu menu2;
	private JMenuItem menuItem11;
	private JMenuItem menuItem12;
	private JMenuItem menuItem21;
	private JMenuItem menuItem22;
	private JButton start;
	private JButton submit;
	private JButton view;
	private CardLoader card1;
	private CardLoader card2;
	private CardLoader card3;
	private CardLoader card4;
	private boolean isstart;
	private ArrayList points;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -