扑克牌创建与洗牌

package poker;

public class Card {
	protected int color;
	protected int number;

	public static final int HEART = 0;
	public static final int SPADE = 1;
	public static final int CLUB = 2;
	public static final int DIAMOND = 3;
	public static final int BLACK = 4;
	public static final int RED = 5;
	
	public static final int THREE = 0;
	public static final int FOUR = 1;
	public static final int FIVE = 2;
	public static final int SIX = 3;
	public static final int SEVEN = 4;
	public static final int EIGHT = 5;
	public static final int NINE = 6;
	public static final int TEN = 7;
	public static final int JACK = 8;
	public static final int QUEEN = 9;
	public static final int KING = 10;
	public static final int ACE = 11;
	public static final int TWO = 12;
	public static final int JOKER = 13;
    private static final String[] Color={"红桃","黑桃","梅花","方块","小","大"};
    private static final String[] Number="3,4,5,6,7,8,9,10,J,Q,K,A,2,王".split(",");
    
	public Card(){
		super();
	}
	
	public Card(int color, int number) {
		super();
		if (color >= 0 && color <= 3 && number >= 0 && number < 13) {
			this.color = color;
			this.number = number;
		} else if (number == 13 && (color == 4 || color == 5)) {
			this.color = color;
			this.number = number;
		} else {
			System.out.println("error");
		}
	}
	
	public String GetCard(){
		return Color[color]+Number[number];
	}
	
}
package poker;

import java.util.Random;

public class Poker {
	private Card[] poker=new Card[54];
	public Poker(){
		int j=0;
		for(int i=Card.THREE;i<=Card.TWO;i++){
			this.poker[j++]=new Card(Card.HEART, i);
			this.poker[j++]=new Card(Card.SPADE, i);
			this.poker[j++]=new Card(Card.CLUB, i);
			this.poker[j++]=new Card(Card.DIAMOND, i);
		}
		this.poker[j++]=new Card(Card.BLACK, Card.JOKER);
		this.poker[j]=new Card(Card.RED, Card.JOKER);
	}
	public Card[] GetPoker(){
		return poker;
	}
	/*
	 * 洗牌
	public void Shuffle(){
		boolean[] CardFlag=new boolean[54];
		Random CardRandom=new Random();
		Card[] tmpPoker=new Card[54];
		int CardNum;

		for(int i=0;i<54;i++){
			tmpPoker[i]=new Card();
			do {
				CardNum=CardRandom.nextInt(54);
			}while(CardFlag[CardNum]);
				tmpPoker[i]=poker[CardNum];
				CardFlag[CardNum]=true;
		}
		poker=tmpPoker;
		
	}
	*/
	/*
	 * 由最后一张牌开始洗牌
	 */
	public void Shuffle(){
		Card tmpCord=new Card();
		Random tmpCardRandom=new Random();
		int tmpCardNum;
		for(int i=poker.length-1;i>=1;i--){
			tmpCardNum=tmpCardRandom.nextInt(i);
			tmpCord=poker[i];
			poker[i]=poker[tmpCardNum];
			poker[tmpCardNum]=tmpCord;
		}
	}
}
package poker;

public class PokerDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Poker myPoker=new Poker();
		Card[] myCard=new Card[54];
		myCard=myPoker.GetPoker();
		for(int i=0;i<54;i++){
			System.out.println(myCard[i].GetCard());
		}
		System.out.println("over");
		myPoker.Shuffle();
		myCard=myPoker.GetPoker();
		for(int i=0;i<54;i++){
			System.out.println(myCard[i].GetCard());
		}
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值