數(shù)據(jù)加載中……
          今天終于完成了洗牌程序.不過可能有點(diǎn)亂!
          import java.applet.*;
          import java.awt.*;
          import java.awt.event.*;
          import java.util.*;
          import javax.swing.*;
          public class puke extends JApplet
          {
          ?JFrame f=new JFrame();
          ?Container y=getContentPane();
          ?JButton wash=new JButton("洗牌");
          ??JButton post=new JButton("發(fā)牌");
          ?JLabel first=new JLabel("The first is:");
          ?JLabel second=new JLabel("The second is:");
          ?JLabel third=new JLabel("The third is:");
          ?JLabel fourth=new JLabel("The fourth is:");
          ?public void init()
          ?{??
          ??y.setLayout(new GridLayout(3,2));
          ??y.add(wash);
          ??y.add(post);
          ??y.add(first);
          ??y.add(second);
          ??y.add(third);
          ??y.add(fourth);
          ??wash.addActionListener(new WashActionAdapter());
          ??post.addActionListener(new PostActionAdapter());??????
          ?}
          //---------------------------------------------------------------
          ?? class MyThread extends Thread
          ?? {
          ?? ?public void run()
          ?? ?{
          ?? ??Stack save=new Stack();
          ?? ??Vector MyVector=new Vector(1,1);
          ????String[] wpkp={"紅桃","黑桃","方片","草花"};
          ????? Random i=new Random();
          ?? ???? int a,j;
          ?? ???? a=4;
          ????try
          ????{
          ???????? while(a!=0)
          ???{
          ? ???j=i.nextInt(4);
          ???? if(wpkp[j]!="NULL")
          ????? {
          ????? ?save.push(wpkp[j]);
          ??? ???? wpkp[j]="NULL";
          ???? ?? a-=1;
          ???? }
          ???? else continue;
          ????}
          ???? while(!save.empty())??????
          ???????? ?MyVector.addElement(save.pop());
          ???????? ?for(int ii=0;ii<MyVector.capacity();ii++)
          ???????? ?{
          ???????? ??switch(ii%5)
          ?????{
          ??????case 0:first.setText("The first is:"+MyVector.get(ii).toString());break;
          ??????case 1:second.setText("The second is:"+MyVector.get(ii).toString());break;
          ??????case 2:third.setText("The third is:"+MyVector.get(ii).toString());break;
          ??????case 3:fourth.setText("The fourth is:"+MyVector.get(ii).toString());break;
          ?????}
          ?????}
          ????}
          ????catch(Exception ee)
          ????{
          ????}
          ?? ??}
          ?? ?}
          //----------------------------------------------------------------
          ??class WashActionAdapter implements ActionListener
          ??{
          //?? Stack save=new Stack();
          ??????
          ?????
          ??
          ???public? void actionPerformed(ActionEvent e)
          ???{
          ??????????? first.setText("The first is:");
          ??????second.setText("The second is:");
          ??????third.setText("The third is:");
          ??????fourth.setText("The fourth is:");????
          ????}
          ???}
          //-------------------------------------------------------------------------------------
          ???class PostActionAdapter implements ActionListener
          ???{
          ????public? void actionPerformed(ActionEvent e)
          ????{
          ?????String cmd=e.getActionCommand();
          ?????String title="Message Dialog";
          ?????String message="";
          ?????int type;
          ?????if(first.getText().equals("The first is:"))
          ?????{
          ??????Thread t=new MyThread();
          ??????? t.start();
          ??????}
          ?????else
          ?????{
          ??????type=JOptionPane.PLAIN_MESSAGE;
          ??????message="請(qǐng)先洗牌";
          ?????JOptionPane.showMessageDialog(f,message,title,type);
          ??????}
          ????
          ???? }
          ???}
          //---------------------------------------------------------------------------------------???
          }





          自己感覺有點(diǎn)亂,大家有好的方法可以告訴我,精誠(chéng)合作,金石為開

          posted on 2006-04-01 16:50 牛浪de流 閱讀(537) 評(píng)論(1)  編輯  收藏 所屬分類: 爪哇學(xué)習(xí)

          評(píng)論

          # re: 今天終于完成了洗牌程序.不過可能有點(diǎn)亂![未登錄] 2007-10-23 21:13 zc

          package Poker.Game;

          class Card {

          private String face;
          private String suit;
          public Card(String suit, String face)
          {
          this.face = face;
          this.suit = suit;
          }
          protected String getFace()
          {
          return face;
          }
          protected String getSuit()
          {
          return suit;
          }
          public String toString()
          {
          return suit+" "+face;
          }

          public static void shuffle(Card[] deck,int startIndex,int size, int splitIndex)
          {
          if (splitIndex * 2 > size)
          {
          Card.swap(deck,startIndex,splitIndex,size-splitIndex);
          shuffle(deck,size-splitIndex,splitIndex,size-splitIndex);
          }
          else if (splitIndex * 2 < size)
          {
          Card.swap(deck,startIndex,size-splitIndex,splitIndex);
          shuffle(deck,startIndex,size-splitIndex,splitIndex);
          }
          else
          {
          Card.swap(deck,startIndex,splitIndex,splitIndex);
          }

          }
          public static void swap(Card[] deck,int srcIndex,int dstIndex, int size)
          {
          String face = "";
          String suit = "";
          for (int i=0; i<size;i++)
          {
          face = deck[srcIndex+i].face;
          suit = deck[srcIndex+i].suit;
          deck[srcIndex+i].face = deck[dstIndex+i].face;
          deck[srcIndex+i].suit = deck[dstIndex+i].suit;
          deck[dstIndex+i].face = face;
          deck[dstIndex+i].suit = suit;
          }
          }
          /**
          * @param args
          */
          public static void main(String[] args) {
          Card[] deck = new Card[52];
          String f[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
          String s[] ={ "黑桃", "紅桃", "梅花", "方塊" };
          for(int i=0; i<s.length; i++)
          {
          for(int j=0; j<f.length; j++)
          {
          deck[i*13+j] = new Card(s[i],f[j]);
          }
          }
          int rnd = 0;
          int numOfShuffle = 10;
          for (int i=0; i<numOfShuffle; i++)
          {
          rnd = (int) Math.abs(Math.random()*52);
          Card.shuffle(deck,0,deck.length,rnd);
          }
          // Test
          for (int i=0; i<deck.length; i++)
          {
          System.out.println(deck[i]);
          }
          }

          }
          主站蜘蛛池模板: 海丰县| 开化县| 定南县| 阿城市| 昌江| 澄江县| 安顺市| 双柏县| 贞丰县| 临邑县| 德化县| 和平区| 玉溪市| 铅山县| 札达县| 桃源县| 芮城县| 江油市| 英吉沙县| 福安市| 儋州市| 阿拉善盟| 襄樊市| 大理市| 察雅县| 斗六市| 石楼县| 安徽省| 连山| 凤山市| 新竹市| 石嘴山市| 宁晋县| 平凉市| 镇坪县| 潍坊市| 汾阳市| 吴忠市| 汝阳县| 南汇区| 香港 |