洗牌:數(shù)組重新排序 Collections.shuffle(cards)负懦;
Card:牌的屬性
TestCard:測(cè)試
Player:玩家,發(fā)牌
import java.util.Objects;
public class Card {
public static final String[] SUITE_NAME = {"紅桃","方塊","梅花",
"黑桃",""};
public static final String[] RANK_NAME = {"3","4","5","6","7",
"8","9","10","J","Q","K","A","2","小王","大王"};
public static final int HEART = 0;
public static final int DIAMOND = 1;
public static final int CLUB = 2;
public static final int SPADE = 3;
public static final int JOKER = 4;
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 QUEUE = 9;
public static final int KING = 10;
public static final int ACE = 11;
public static final int TWO = 12;
public static final int BLACK = 13;
public static final int COLOR = 14;
private int suit; //花色
private int rank; //點(diǎn)數(shù)
public Card() {}
public Card(int suit, int rank) {//構(gòu)造方法
this.suit = suit;
this.rank = rank;
}
@Override
public String toString() {
return SUITE_NAME[suit]+RANK_NAME[rank];
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Card card = (Card) o;
return suit == card.suit &&
rank == card.rank;
}
@Override
public int hashCode() {
return Objects.hash(suit, rank);
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}
//Player 玩家類
import java.util.Arrays;
public class Player {
private int id;//玩家id屬性
private String name;//玩家姓名
private Card[] cards;//玩家手牌數(shù)組//構(gòu)造方法,花色點(diǎn)數(shù)
public Player(int id, String name) {
this.id = id;
this.name = name;
this.cards = new Card[]{};
}
public void addCard(Card c) {
cards = Arrays.copyOf(cards, cards.length + 1);//數(shù)組擴(kuò)容黄娘,手牌長(zhǎng)度=手牌長(zhǎng)度+1
cards[cards.length - 1] = c;//把牌放進(jìn)擴(kuò)容后的數(shù)組中
}
@Override
public String toString() {
return id+","+name+":"+Arrays.toString(cards);
}
}
package cn.besttest.day4_homework;
import java.util.*;
public class TestCard {
public static void main(String[] args) {
List<Card> cards = new ArrayList<>();//創(chuàng)建一個(gè)cards數(shù)組禁炒,數(shù)組類型Crad绕辖,(花色脓诡,點(diǎn)數(shù))
for (int rank = Card.THREE; rank <= Card.TWO; rank++) { //創(chuàng)建一副撲克牌,THREE=0媒役,TWO=12 小3-2的位數(shù)
cards.add(new Card(Card.HEART, rank));//添加顏色和對(duì)應(yīng)點(diǎn)數(shù) 紅桃
cards.add(new Card(Card.DIAMOND, rank));//添加顏色和對(duì)應(yīng)點(diǎn)數(shù) 方塊
cards.add(new Card(Card.CLUB, rank));//添加顏色和對(duì)應(yīng)點(diǎn)數(shù) 梅花
cards.add(new Card(Card.SPADE, rank));//添加顏色和對(duì)應(yīng)點(diǎn)數(shù) 黑桃
}
cards.add(new Card(Card.JOKER,Card.BLACK));//添加小王
cards.add(new Card(Card.JOKER,Card.COLOR));//添加大王
System.out.println(cards);
//洗牌
// Random ran=new Random();
// for (int i=cards.size()-1;i>0;i--){//到0的話就沒(méi)有換的了祝谚,所以要留一個(gè)
// int index=ran.nextInt(i);
// Card c=cards.get(index);
// Card temp=cards.get(i);
// cards.set(i,c);
// cards.set(index,temp);
// }
Collections.shuffle(cards);// 把一個(gè)數(shù)組中數(shù)據(jù)重寫(xiě)排列
System.out.println(cards);
Player[] players={new Player(1,"碧瑤"),new Player(2,"陸雪琪"),new Player(3,"張小凡")};
for (int i=0;i<cards.size()-3;i++){
Card c=cards.get(i);
players[i%players.length].addCard(c);
}
System.out.println(players[0]);
System.out.println(players[1]);
System.out.println(players[2]);
}
}