複製內容到剪貼板
代碼:
/*****************************
圖形檔名規則是:
1-13 梅花 1-13
14-26 方塊 1-13
27-39 紅心 1-13
40-52 黑桃 1-13
********************************/
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.event.*;
public class b3s extends JTest3b implements ActionListener
{
JButton button1,button2;
JFrame frame1;
Container c;
JPanel pan0,pan1;
ImageIcon icon1;
JLabel[] label1 = new JLabel[6];//設定放牌與顯示牌型用的label;
String result;
String[] cardname=new String[52];
int[] pcard = new int[5]; //發到的牌存放的地方;
static int point[] = new int[5];//存點數
static int color[] = new int[5];//存花色
static int tempdiff[] = new int[5];
static int temp,tempi,k,l,m;
boolean samekind = true; //同花
boolean straight = true; //順
int zerocount = 0; //算幾個0
int continuezero = 0;//算幾個連續0
public b3s() //建構元,送入bridge目錄
{
int i,j;
//把牌初始化
for (i=0;i<=51;i=i+1)
{
cardname[i]=new String(Integer.toString(i));
}
frame1 = new JFrame("梭哈");
c = frame1.getContentPane();
c.setLayout(new GridLayout(2,1));
//panel 0 放按鈕,panel 1放玩家的牌與牌型
pan0 = new JPanel();
pan1 = new JPanel();
for(j=0;j<6;j++)
{
label1[j] = new JLabel();//先設定沒有牌與牌型
pan1.add(label1[j]);
}
button1 = new JButton("發牌");
button2 = new JButton("牌型");
button1.addActionListener(this);
button2.addActionListener(this);
pan0.add(button1);
pan0.add(button2);
c.add(pan0);
//設定關閉視窗,因為不是繼承JFrame,所以必須另外處理
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(200,200);
frame1.show(); //show 出來
}
public void actionPerformed(ActionEvent e)//處理按鈕事件
{
String poker;
if(e.getSource()==button1) //發牌
{
shuffle(cardname); //繼承來的發牌方法
for(int j=0;j<5;j++)
{
//取得牌的數值
pcard[j] = Integer.parseInt(cardname[j]);//每個人發五張牌
//取得牌的圖形,要牌的點數加一
if (pcard[j]<9)
poker = new String("bridge/0"+(pcard[j]+1)+".GIF");
else poker =new String("bridge/"+(pcard[j]+1)+".GIF");
icon1 = new ImageIcon(poker);
//把牌當icon處理
label1[j].setIcon(icon1);
}
label1[5].setText("");//清除牌型資料
c.add(pan1);//show 出牌
frame1.pack(); //自動設定大小
frame1.show();
}
if(e.getSource()==button2) //計算牌型
{
zerocount = 0;
continuezero = 0;
straight = true; //順
samekind = true; //同花
//分別記錄五張牌的花色與點數
for(int j=0;j<5;j++)
{
color[j] = pcard[j]/13; //花色
point[j] = pcard[j]%13; //點數
}
//照點數排序
for( k=0;k<5;k++)
{
for( l=4;l>k;l--)
{
if(point[l]<point[l-1])
{
tempi = point[l-1];
point[l-1] = point[l];
point[l] = tempi;
tempi = color[l-1];
color[l-1] = color[l];
color[l] = tempi;
}
}
}
for(int j=1;j<5;j++)
{
if(color[j]!=color[j-1]) //花色不同
samekind = false;
tempdiff[j] = point[j] - point[j-1];
if(tempdiff[j] != 1) //不連續
straight = false;
if(tempdiff[j] == 0) //同點數
zerocount++;
}
for(int j=2; j<5;j++)
if((tempdiff[j]==0) && (tempdiff[j-1]==0) )
continuezero++; //連續同點數
result = "無";
if(straight)
result = "順";
if(samekind)
result = "同花";
if(straight && samekind)
result = "同花順";
if(zerocount == 1)
result = "一對";
if((zerocount == 2) && (continuezero == 0))
result = "兩對";
if((zerocount == 2) && (continuezero == 1))
result = "三條";
if((zerocount == 3) && (continuezero == 1))
result = "fullhouse";
if((zerocount == 3) && (continuezero == 2))
result = "鐵枝";
label1[5].setText(result);
frame1.pack();
//frame1.show();
}// for player
}
public static void main(String args[]) //程式起點
{
b3s app=new b3s(); //畫圖
}
}