1、 定义card类
属性 rank代表牌面值0-13
suit 代表花色
class Card{
public int rank;//牌面值
public String suit;
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
@Override
public String toString() {
return String.format("[%s %d]", suit, rank);
}
}
定义四种花色 “♥”, “♠”, “♦”, “♣”
public static final String[] SUITS = {"♥", "♠", "♦", "♣"};
2、生成52张牌
//生成牌 52
public static List<Card> buyDeck() {
List<Card> deck = new ArrayList<>();
for (int i = 0; i < 4; i++){
for (int j = 1; j <= 13; j++){
//int rank = i;
//String suit = SUITS[j];
Card card = new Card(j, SUITS[i]);
deck.add(card);
}
}
return deck;
}
3、打乱牌面顺序
这里采用从后向前的顺序进行交换,每次把最后一张牌与前面随机的一张牌进行交换使用,使用rand函数生成随机值
private static void swap(List<Card> deck,int index, int i){
//tmp = [index]
//[index] = [i]
//[i] = tmp;
Card tmp = deck.get(index);
deck.set(index, deck.get(i));
deck.set(i, tmp);
}
//洗牌
public static void shuffle(List<Card> deck){
for (int i = deck.size() - 1; i >0; i--){
Random random = new Random();
int index = random.nextInt(i);//[0,i)
swap(deck, index, i);
}
}
3、三个人每人轮流起五张牌
这里采用嵌套列表的方式实现,定义一个总的列表用来存储下面的每个人存牌的列表。每次从最上面抽取一张牌,由总列表控制每人每轮取到一张牌,直到每人都取到五张牌为止。
public static void main(String[] args) {
List<Card> deck = buyDeck();
System.out.println(deck);
System.out.println("==========洗牌==========");
shuffle(deck);
System.out.println(deck);
//三个人每人揭牌五张
List<List<Card>> hand = new ArrayList<>();
List<Card> hands1 = new ArrayList<>();
List<Card> hands2 = new ArrayList<>();
List<Card> hands3 = new ArrayList<>();
hand.add(hands1);
hand.add(hands2);
hand.add(hands3);
for (int i = 0; i < 5; i++){
for (int j = 0; j < 3; j++){
Card card = deck.remove(0);
hand.get(j).add(card);
// List<Card> lsit = hand.get(j);
// list.add(card);
}
}
System.out.println("第一个人的牌:");
System.out.println(hands1);
System.out.println("第二个人的牌:");
System.out.println(hands2);
System.out.println("第三个人的牌:");
System.out.println(hands3);
System.out.println("剩下的牌:");
System.out.println(deck);
}
完整代码
class Card{
public int rank;//牌面值
public String suit;
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
@Override
public String toString() {
return String.format("[%s %d]", suit, rank);
}
}
public class CardDemo {
public static final String[] SUITS = {"♥", "♠", "♦", "♣"};
//买牌 52
public static List<Card> buyDeck() {
List<Card> deck = new ArrayList<>();
for (int i = 0; i < 4; i++){
for (int j = 1; j <= 13; j++){
//int rank = i;
//String suit = SUITS[j];
Card card = new Card(j, SUITS[i]);
deck.add(card);
}
}
return deck;
}
private static void swap(List<Card> deck,int index, int i){
//tmp = [index]
//[index] = [i]
//[i] = tmp;
Card tmp = deck.get(index);
deck.set(index, deck.get(i));
deck.set(i, tmp);
}
//洗牌
public static void shuffle(List<Card> deck){
for (int i = deck.size() - 1; i >0; i--){
Random random = new Random();
int index = random.nextInt(i);//[0,i)
swap(deck, index, i);
}
}
public static void main(String[] args) {
List<Card> deck = buyDeck();
System.out.println(deck);
System.out.println("洗牌:");
shuffle(deck);
System.out.println(deck);
//三个人每人揭牌五张
List<List<Card>> hand = new ArrayList<>();
List<Card> hands1 = new ArrayList<>();
List<Card> hands2 = new ArrayList<>();
List<Card> hands3 = new ArrayList<>();
hand.add(hands1);
hand.add(hands2);
hand.add(hands3);
for (int i = 0; i < 5; i++){
for (int j = 0; j < 3; j++){
Card card = deck.remove(0);
hand.get(j).add(card);
// List<Card> lsit = hand.get(j);
// list.add(card);
}
}
System.out.println("第一个人的牌:");
System.out.println(hands1);
System.out.println("第二个人的牌:");
System.out.println(hands2);
System.out.println("第三个人的牌:");
System.out.println(hands3);
System.out.println("剩下的牌:");
System.out.println(deck);
}
}
运行结果