?? game24.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 + -