数据加载中……
今天终于完成了洗牌程序.不过可能有点乱!
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流 阅读(530) 评论(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]);
}
}

}
  回复  更多评论    

只有注册用户登录后才能发表评论。


网站导航: