用java实现三个经典的洗牌算法

三个经典的洗牌算法分别为Fisher-Yates算法,knuth-Durstenfeld算法和Inside-Out算法.

这里是详细介绍点击打开链接

这里用java实现了这三个算法


这个是纸牌类


public class Card {
	//private int i,j;
	//private Card cardList[];
	private String num,chr;
	
	public Card(String chr,String num){
		this.num=num;
		this.chr=chr;
	}
	
	public Card getCard(){
		return this;
	}
	
	public void setCard(Card a){
		this.num=a.num;
		this.chr=a.chr;
	}
	
	public Card(Card a){
		this(a.chr,a.num);
	}
	
	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getChr() {
		return chr;
	}

	public void setChr(String chr) {
		this.chr = chr;
	}

	public String toString(){
		return chr+" "+num;
	}
}




这里实现了三个算法

import java.util.*;


public class CardGame {
	
	private String numList[]={"A","2","3","4","5","6","7","8","9","10",
			"J","Q","K"};
	private String chrList[]={"*","&","^","%"};
	//cardList表示所有牌,curList表示当前的牌
	private Card cardList[],curList[];
	private int curSize;
	private Random rdm=new Random();
	
	public static final int CARDSIZE=13*4;
	
	public Card[] getCardList() {
		return cardList;
	}

	public Card[] getCurList() {
		return curList;
	}

	public void setCurList(Card[] curList) {
		for(int i=0;i0;i--){
			tempInd=rdm.nextInt(i);
			this.swap(curList[i], curList[tempInd]);
		}
	}
	
	//在前面的i-1张牌中随机选出一张和第i张牌交换。
	public void IOShuffle(int size){
		int tempInd;
		this.setCurList(this.getCardList());
		for(int i=1;i



你可能感兴趣的:(用java实现三个经典的洗牌算法)