Java模拟发牌、洗牌


1).写一个卡片类Card, 含两个属性一个花色(Suit),一个是点数(Rank).

2).写个扑克牌Poker类,含52Card,要用集合来存放

2张副牌(大王和小王)不计算

3).写一个发牌者 Dealer ,有属性 扑克牌Poker类,并提供方法:

 发牌 deal(), 洗牌 shuffle()

4).写一个 玩家 Player

5).在单元测试中,可以初始化3个玩家,并由发牌者轮流发牌。

  最后打印3个玩家手中的牌、


Card类:

public class Card {
	public String suit;
	public int rank;
	public String getSuit() {
		return suit;
	}
	public void setSuit(String suit) {
		this.suit = suit;
	}
	public int getRank() {
		return rank;
	}
	public void setRank(int rank) {
		this.rank = rank;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + rank;
		result = prime * result + ((suit == null) ? 0 : suit.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Card other = (Card) obj;
		if (rank != other.rank)
			return false;
		if (suit == null) {
			if (other.suit != null)
				return false;
		} else if (!suit.equals(other.suit))
			return false;
		return true;
	}
	public Card(String suit, int rank) {
		super();
		this.suit = suit;
		this.rank = rank;
	}
	public Card() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
}

Player类

import java.util.ArrayList;

public class Player {
	private int id;
	private String name;
	private ArrayList list;
    public void getCard(Card card){  
        if(list == null) 
        	list = new ArrayList();  
        list.add(card);  
    }
	public ArrayList getList() {
		return list;
	}
	public void setList(ArrayList list) {
		this.list = list;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Player(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Player(int id, String name, ArrayList list) {
		super();
		this.id = id;
		this.name = name;
		this.list = list;
	}
	public Player() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((list == null) ? 0 : list.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Player other = (Player) obj;
		if (id != other.id)
			return false;
		if (list == null) {
			if (other.list != null)
				return false;
		} else if (!list.equals(other.list))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}

Dealer类(发牌者)

public class Dealer {
	public static ArrayList arrayList = new ArrayList<>();
	/**
	 * 创建扑克牌
	 */
	public ArrayList deal(String[] suit,int[] rank){
	      for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 13; j++) {
				arrayList.add(new Card(suit[i], rank[j]));
			}
		}
	      
	      return arrayList;
	}
	/**
	 * 洗牌
	 */
	public void shuffle(ArrayList arrayList) {
		Collections.shuffle(arrayList);
	}
}

测试类:

public class Test {
	


	public static void main(String[] args) {
		ArrayList nowCardList = new ArrayList<>();
		//创建玩家
		 Player[] p = new Player[3];
	
		 p[0] = new Player(1, "jim");
		 p[1] = new Player(2, "tom");
		 p[2] = new Player(3, "lucy");
		 Dealer dealer = new Dealer();
		 
		//创建扑克

		 String[] suit = {"黑桃","红心","方片","梅花"};
		 int[] rank = {1,2,3,4,5,6,7,8,9,10,11,12,13};
		 ArrayList arrayList = dealer.deal(suit,rank);
		 dealer.shuffle(arrayList);
		 //发牌
		    for(int i=0; i<52; i++){  
	            if(i%3 == 0){  
	                p[0].getCard(arrayList.get(i));  
	            }else if(i%3 == 1){  
	                p[1].getCard(arrayList.get(i));  
	            }else if(i%3 == 2){  
	                p[2].getCard(arrayList.get(i));  
	            }  
	        }  
 
    
		  
		  
		  for(int i = 0;i < 3;i++) {
			  System.out.println("第"+(i+1)+"个人拿到的牌是"+p[i].getList());
			
		  }
	}
	

}
没有创建poker类,我省点事把定义52张牌直接放在dealer类中。

你可能感兴趣的:(Java模拟发牌、洗牌)