數據加載中……
          今天終于完成了洗牌程序.不過可能有點亂!
          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("發牌");
          ?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="請先洗牌";
          ?????JOptionPane.showMessageDialog(f,message,title,type);
          ??????}
          ????
          ???? }
          ???}
          //---------------------------------------------------------------------------------------???
          }





          自己感覺有點亂,大家有好的方法可以告訴我,精誠合作,金石為開

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

          評論

          # re: 今天終于完成了洗牌程序.不過可能有點亂![未登錄] 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]);
          }
          }

          }
            回復  更多評論    
          主站蜘蛛池模板: 青神县| 柳江县| 射阳县| 江川县| 上高县| 微博| 岳阳县| 剑河县| 交城县| 锦州市| 邹城市| 永善县| 泾川县| 昌黎县| 库伦旗| 库尔勒市| 宜兰县| 琼结县| 泰安市| 晋中市| 两当县| 开平市| 灯塔市| 靖州| 玛曲县| 大悟县| 牡丹江市| 清水河县| 陆丰市| 富川| 淮北市| 伊宁县| 微山县| 兴文县| 尖扎县| 宁城县| 西丰县| 瓦房店市| 铁力市| 宁远县| 海南省|