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

📄 game24.java

📁 24点
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
import javax.swing.*;
import java.util.regex.*;
import java.net.URL;

public class game24 implements ActionListener,KeyListener
{
	JFrame f=null;
	private DrawPanel dp;
	JPanel jpl;
	JTextField txtField;
	int[] mystatus=new int[4];

	public game24()
	{
		f=new JFrame("“速算24”扑克游戏");
		dp = new DrawPanel(this);
		for(int i=0;i<4;i++)
	    {
			mystatus[i]=0;
	    }
		Container c = f.getContentPane();
		c.setLayout(new BorderLayout());

		JMenuBar mb=new JMenuBar();//菜单
		JMenu youxi=new JMenu("游戏");
		JMenu bangzu=new JMenu("帮助");
		JMenuItem replay=new JMenuItem("重新发牌---F2");
		JMenuItem exit=new JMenuItem("退出");
		JMenuItem aboutItem=new JMenuItem("关于");
		youxi.add(replay);
		youxi.addSeparator();
		youxi.add(exit);
		replay.addActionListener(this);
		exit.addActionListener(this);
		aboutItem.addActionListener(this);
		bangzu.add(aboutItem);
		mb.add(youxi);
		mb.add(bangzu);
		f.setJMenuBar(mb);
		c.add(dp,BorderLayout.CENTER);
		jpl=new JPanel();
		c.add(jpl,BorderLayout.SOUTH);

		JButton btnok=new JButton("计算");
		txtField=new JTextField(15);
		JLabel label=new JLabel("请输入表达式");
		JButton btnstart=new JButton("开始游戏");

		jpl.add(label);
		jpl.add(txtField);
		jpl.add(btnok);
		jpl.add(btnstart);
		btnstart.addActionListener(this);
		btnok.addActionListener(this);
		txtField.addKeyListener(this);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(600,300);
		f.setVisible(true);
	}

	public void keyPressed(KeyEvent e){
	int code=e.getKeyCode();
		if(code==113)
			for(int i=0;i<4;i++)
			{
				mystatus[i]=(int)(Math.random()*13)+1;
			}
	//JOptionPane.showMessageDialog(f,"f2","test",JOptionPane.WARNING_MESSAGE);
	dp.repaint();
	}
	public void keyReleased(KeyEvent e){}
	public void keyTyped(KeyEvent e){}

	public void actionPerformed(ActionEvent e)
	{
		String Cmd=e.getActionCommand();
		if(Cmd.equals("开始游戏"))
		{
			for(int i=0;i<4;i++)
			{
				mystatus[i]=(int)(Math.random()*13)+1;
			}
			dp.repaint();
		}
		else if(Cmd.equals("重新发牌---F2"))
		{
			for(int i=0;i<4;i++)
			{
				mystatus[i]=(int)(Math.random()*13)+1;
			}
			dp.repaint();
		}
		 else if(Cmd.equals("退出")){
               int s=JOptionPane.showConfirmDialog(f,"你真的要结束吗","结束程序",JOptionPane.YES_NO_CANCEL_OPTION);
               if(s==JOptionPane.YES_OPTION)
                System.exit(0);
				}
		else if(Cmd.equals("关于"))
		{
             JOptionPane.showMessageDialog(f,"作者-黄海泉:“速算24”扑克游戏1.1版本","关于",JOptionPane.PLAIN_MESSAGE);//显示对话框
		}
		else if(Cmd.equals("计算"))
		{
			String title="";
   			String message="";
   			int type=JOptionPane.PLAIN_MESSAGE;
			try{
				int result=Compute(txtField.getText().trim());
				if(result==-1)
				{
					title="提示";
					message="表达式不能为空,请输入!";
					type=JOptionPane.WARNING_MESSAGE;
					JOptionPane.showMessageDialog(f,message,title,type);//显示对话框
					txtField.setText("");
					txtField.requestFocus();

				}else if(result==-2)
				{
					title="错误";
					message="你输入的表达式不合法,请重新输入!";
					type=JOptionPane.ERROR_MESSAGE ;
					JOptionPane.showMessageDialog(f,message,title,type);
					txtField.setText("");
					txtField.requestFocus();
				}
				else if(result==-3)
				{
					title="警告";
					message="你输入的数字至少有一个不是扑克牌上的数字,请重新输入!";
					type=JOptionPane.WARNING_MESSAGE ;
					JOptionPane.showMessageDialog(f,message,title,type);
					txtField.setText("");
					txtField.requestFocus();
				}
				else if(result!=24)
				{
					title="错误";
					message="你输入的表达式的值为"+result+",请重新输入!";
					type=JOptionPane.ERROR_MESSAGE ;
					JOptionPane.showMessageDialog(f,message,title,type);
					txtField.setText("");
					txtField.requestFocus();
				}
				else if(result==24)
				{
					title="提示";
					message="祝贺你,你的输入正确!";
					type=JOptionPane.INFORMATION_MESSAGE;
					txtField.requestFocus();
					JOptionPane.showMessageDialog(f,message,title,type);
					for(int i=0;i<4;i++)
					{
						mystatus[i]=(int)(Math.random()*13)+1;
					}
					dp.repaint();
					txtField.setText("");
					txtField.requestFocus();
				}

			}catch(Exception ex)
			{
				ex.printStackTrace();
			}
		}
	}

	public  int Compute(String s)//函数1--5-8+6---------
	{
		int First,Last=0,temp=0,result;
		String SubStr="",LeftStr="",Middle="",Right="";
		String stemp=new String(s);
		result=Check(stemp);
		if(result!=0)
			return result;
		First=s.lastIndexOf('('); //定位最后一个(号位置//=
		try{
		while(First!=-1)//用于去除()操作
		{
			SubStr=String.valueOf(s.toCharArray(),First+1,s.length()-First-1);//复制最后一个(号后面的字符串//=
			Last=SubStr.indexOf(')');//查找)号在substr中的位置//=
			Last=Last+First;  //定位最后一个(号以后的最开始的)号位置//
			LeftStr=String.valueOf(s.toCharArray(),0,First); //(号左边的字符串//=
			Middle=String.valueOf(s.toCharArray(),First+1,Last-First); //()号中间的字符串//=
			Right=String.valueOf(s.toCharArray(),Last+2,s.length()-Last-2); //)号右边的字符串//=-
			temp=SubCompute(Middle);  //进入下面的计算//=
			Middle=String.valueOf(temp);

			s=LeftStr+Middle+Right;
			First=s.lastIndexOf('(');
		}
		}catch(Exception e){
			return -2;
		}
		return SubCompute(s);
	 }//函数1结束

	 //检查表达式的合法性
	 public int Check(String s)
	{
		String regEx1="[^0-9+*/()-]"; //减号不能放在中间,因为是特殊字符
        Pattern p=Pattern.compile(regEx1);
		if(s.equals(""))
			return -1;//设为空
		Matcher m = p.matcher(s);
		boolean rs=m.find();//没有返回false,有返回true
		if(rs)
			return -2;//设为不合法
		//else
			//return true;

		String regEx2="[()]";
		p=Pattern.compile(regEx2);
		m=p.matcher(s); 
		
		s=m.replaceAll("");
		s=m.replaceAll("");
		regEx2="[+*/-]";
		p=Pattern.compile(regEx2);
		String[] r=p.split(s);
		if(r.length!=4)//如果输入的表达式没有四个数
			return -2;
		int[] numbers=new int[r.length];
		for(int i=0;i<r.length;i++)
		{
			numbers[i]=Integer.parseInt(r[i]);
		}

		//判断表达式的数字是否都是牌上的数字
		int tempstatus[]=new int[4];
		for(int i=0;i<4;i++)
		{
			tempstatus[i]=mystatus[i];
		}
		for(int i=0;i<4;i++)
		{
			int j=0;
			boolean existed=false;//先设为全是
			while(j<4&&!existed)
			{
				if(tempstatus[j]==numbers[i])
				{
					tempstatus[j]=-1;
					existed=true;//设为不是扑克上的数
				}
				j++;
			}
			if(!existed)
				return -3;//设为不是扑克上的数
		 }
		 return 0;
	   }//检查表达式的合法性函数结束

	//此计算用于计算不带()号的加、减、乘、除运算
	public static int SubCompute(String s)//函数2--------5-8+6
	{
		String Middle,Mul2,Right,tempStr,Left,Mul1,temMiddle,tems;
		int First,temp,MulPos,DivPos;
		char Fuhao;
		Middle="";
		Mul2="";
		Right="";
		tems="";
		temMiddle="";

		//再计算乘除,定位第一个*号或/号的位置=====================
		 MulPos=s.indexOf('*');//=
		 DivPos=s.indexOf('/');//=
		 First=MulPos;//
		 if (MulPos>DivPos)
		 First=DivPos;//=
		 if ((DivPos==-1)&&(MulPos!=-1))
		 {
			First=MulPos;//=
			DivPos=2000;//将除号所在位置设置成一个大于MulPos但又不可能的值
		 }
		 if ((DivPos!=-1)&&(MulPos==-1))
		 {
			First=DivPos;//将乘号所在位置设置成一个大于DivPos但不可能的值//=
			MulPos=2000;
		 }

		while(First!=-1)//循环计算乘、除
		{
			tempStr=String.valueOf(s.toCharArray(),0,First);//=
			temp=AnyLastPos(tempStr);//
			Left=String.valueOf(s.toCharArray(),0,temp);//=
			Mul1=String.valueOf(s.toCharArray(),temp,First-temp);//=
			tempStr=String.valueOf(s.toCharArray(),First+1,s.length()-First-1);//=
			temp=AnyFirstPos(tempStr);//
			if(temp==200)
			{
				Mul2=tempStr;//
				Right="";//
			}
			else
			{
				Mul2 =String.valueOf(tempStr.toCharArray(),0,temp);//
				Right=String.valueOf(tempStr.toCharArray(),temp,tempStr.length()-temp);//
			}
			if(MulPos>DivPos)
				Middle=String.valueOf(Integer.parseInt(Mul1)/Integer.parseInt(Mul2));//
			else
				Middle=String.valueOf(Integer.parseInt(Mul1)*Integer.parseInt(Mul2));//
				s=Left+Middle+Right;//

				MulPos=s.indexOf('*');//
				DivPos=s.indexOf('/');//
				First=MulPos;//
			if(MulPos>DivPos)
				First=DivPos;

			if((DivPos==-1)&&(MulPos!=-1))
			{
				First=MulPos;
				DivPos=2000;//将除号所在位置设置成一个大于MulPos但又不可能的值
			}
			if((DivPos!=-1)&&(MulPos==-1))
			{
				First=DivPos;//将乘号所在位置设置成一个大于DivPos但不可能的值
				MulPos=2000;
			}
		  }//while结束

		//定位+、-号首先出现的位置------5-8+6
		First=AnyFirstPos(s);//第一个运算符的位置=1
		if(First==200)//如果没有+、-号,则可以直接返回结果
		{
			return Integer.parseInt(s);//函数返回
		}
		Fuhao=AnyFirstF(s); //确定首先出现的符号是+号还是-号//=-
		while(First!=200)//First=2/s=-3+6
		{
			//如果找到+号或-号
			tempStr=String.valueOf(s.toCharArray(),0,First);//得到第一个符号前的字符串//=-3
			temp=AnyLastPos(tempStr);//最后一个运算符的位置//=0
			Left=String.valueOf(s.toCharArray(),0,temp);//最后一个运算符前的字符串//=空
			Mul1=String.valueOf(s.toCharArray(),temp,First-temp);//第一个数//=-3
			tempStr=String.valueOf(s.toCharArray(),First+1,s.length()-First-1);//=6
			temp=AnyFirstPos(tempStr);//读取第一个运算符的位置//=200
			if(temp==200) //没有运算符,则
			{
				Mul2=tempStr;//=6
				Right="";
			}
			else
			{
				Mul2=String.valueOf(tempStr.toCharArray(),0,temp);//=8
				//Right为第一个运算符后的字符串
				Right=String.valueOf(tempStr.toCharArray(),temp,tempStr.length()-temp);//=+6
			}
			 if (Fuhao=='+')
			{
				int tem1;
				tem1=Integer.parseInt(Mul1)+Integer.parseInt(Mul2);//=-3-6
				if(tem1<0)
				{
					tem1=-tem1;//=3
					temMiddle=String.valueOf(tem1);
					tems=Left+temMiddle+Right;//3+6
				}
				Middle=String.valueOf(Integer.parseInt(Mul1)+Integer.parseInt(Mul2));//=-3+6
			}
			 else
			  {
				int tem1;
				tem1=Integer.parseInt(Mul1)-Integer.parseInt(Mul2);//=5-8
				if(tem1<0)
				{
					tem1=-tem1;//=3
					temMiddle=String.valueOf(tem1);
					tems=Left+temMiddle+Right;//3+6
				}
				
				Middle=String.valueOf(Integer.parseInt(Mul1)-Integer.parseInt(Mul2));//=-3
			  }

				s=Left+Middle+Right;//=-3+6
				First=AnyFirstPos(s);//读取第一个运算符的位置以得到表达式的第一个数字//=0
			Fuhao=AnyFirstF(s);
			if(First==0) //说明第一个为-号
			{
				First=AnyFirstPos(tems);//=2
				if(First==200)
					return Integer.parseInt(Middle);
				First=First+1;
				Fuhao=AnyFirstF(tems);//=+
			}
		}//while结束

		return Integer.parseInt(Middle);
	}

	//读取第一个运算符的位置以得到表达式的第一个数字,找到返回位置,否则返回200
	public static int AnyFirstPos(String s)//函数3
	{
		int SubPos,PluPos,MulPos,DivPos,FirstPos;
		   //定位字符串中最先一个运算符的位置
		   SubPos=s.indexOf('-');
		   PluPos=s.indexOf('+');
		   MulPos=s.indexOf('*');
		   DivPos=s.indexOf('/');
		   FirstPos=200;
		   if (SubPos==-1)  //如果没有-号
				SubPos=200; //将SubPos设置成一个不可能的值
		   if (PluPos==-1)  //如果没有+号
				PluPos=200; //将PluPos设置成一个不可能的值
		   if (MulPos==-1) //如果没有*号
				MulPos=200; //将MulPos设置成一个不可能的值
		   if (DivPos==-1) //如果没有/号
				DivPos=200; //将DivPos设置成一个不可能的值

			if(FirstPos > SubPos)
			  FirstPos=SubPos;
			if (FirstPos > PluPos)
			  FirstPos = PluPos;
			if (FirstPos>MulPos)
			  FirstPos = MulPos;
			if (FirstPos > DivPos)
			  FirstPos=DivPos;

			return FirstPos; //结束函数,返回位置
	}//函数3结束

//读取最后一个运算符的位置以得到表达式的最后一个数字,有返回位置,无返回0
	public static int AnyLastPos(String s)//函数4
	{
		int SubPos,PluPos,MulPos,DivPos,Pos;
		
		//定位字符串中最后一个运算符的位置
		   SubPos=s.lastIndexOf('-');
		   PluPos=s.lastIndexOf('+');
		   MulPos=s.lastIndexOf('*');
		   DivPos=s.lastIndexOf('/');
		   Pos=SubPos;
		   if (Pos<PluPos)
				Pos=PluPos;
		   if (Pos<MulPos)
				Pos=MulPos;
		   if (Pos<DivPos)
				Pos=DivPos;
		   if(Pos==0)
			   return Pos;
		   return (Pos+1); //结束函数,返回位置
	}//函数4结束

	//判断最先出现的符号是+号、-号、*号还是/号
	public static char AnyFirstF(String s)//函数5
	{
		int SubPos,PluPos,MulPos,DivPos,tempPos;
		char Operator;
		   SubPos=s.indexOf('-');
		   PluPos=s.indexOf('+');
		   MulPos=s.indexOf('*');
		   DivPos=s.indexOf('/');

		   if (SubPos==-1) //如果没有-号
				SubPos=200; //将SubPos设置成一个不可能的值
		   if (PluPos==-1) //如果没有+号
				PluPos=200; //将PluPos设置成一个不可能的值
		   if (MulPos==-1)  //如果没有*号
				MulPos=200; //将MulPos设置成一个不可能的值
		   if (DivPos==-1)  //如果没有/号
				DivPos=200; //将DivPos设置成一个不可能的值

		   Operator='-';
		   tempPos=SubPos;//临时位置等于减号位置

		if(tempPos>PluPos)//如果临时位置大于加号位置,则最先出现的符号为加号
		{
			tempPos=PluPos;
			Operator='+';
		}
		if(tempPos>MulPos)//如果临时位置大于乘号位置,则最先出现的符号为乘号
		{
			tempPos=MulPos;
			Operator='*';
		}
		if(tempPos>DivPos)
		{
			  //tempPos:=DivPos;
			  Operator='/';
		}

		   return Operator; //结束函数,返回符号
	}//函数5


	public static void main(String args[])
	{
		game24 g=new game24();

	}
}


class DrawPanel extends JPanel
{
	final int IMGSIZE=130;
	Image[] myImage=new Image[14];
	game24 mygame;

	public DrawPanel(game24 mygame)
	{
		this.mygame=mygame;
	}

	public void paintComponent(Graphics g)//
	{
		super.paintComponent(g);
		URL url=null;
		try{
			url=Class.forName("game24").getResource("img/back.jpg");
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		myImage[0]=getToolkit().getImage(url);//背景图片
		for(int i=1;i<=13;i++)
		{
			try{
				url=Class.forName("game24").getResource("img/"+i+".jpg");
			}catch(Exception e)
			{}
			myImage[i]=getToolkit().getImage(url);
		}

		for(int i=0;i<4;i++)
		{
			g.drawImage(myImage[mygame.mystatus[i]],i*IMGSIZE+20,20,this);
		}
	}
}

⌨️ 快捷键说明

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