This small project is: to distribute five three players take turns playing cards, playing cards and can be re-wash.
First, we want a new pair of existing cards, then we shuffle operations on the deck. Down to three players take turns distribution.
First playing cards have attributes:
- Color
- size
Let's create a Card class:
class Card {
public String suit;//花色
public String rank;//大小
@Override
public String toString() {
return "[" + suit + " " + rank + "]";
}
}
Due to the size of playing cards have "A, J, Q, K ", can not all be represented with int, so we use the String to represent.
Here we need to buy a new pair of playing cards, we use an ArrayList to create a Vice new cards:
public static String[] SUITS = {"♥️", "♦️", "♣️", "♠️"};
public static String[] RANKS = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K"};
public static List<Card> buyCard() {
List<Card> list = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
String suit = SUITS[i];
String rank = RANKS[j];
Card card = new Card();
card.suit = suit;
card.rank = rank;
list.add(card);
}
}
return list
}
We shuffle Collections class shuffle shuffling method.
Collections.shuffle(list);
The next step is to distribute the cards:
// List<List<Card>> 首先 List<Card> 这个是一个扑克牌的数组,
//然后 List<List<Card>> 这个相当于是一个 '二维数组',
//这个数组里面的每个元素又是一个 List<Card>,
//这样就模拟出了 3 个 List<Card> 扑克牌数组,相当于三个玩家
List<List<Card>> player = new ArrayList<>();
player.add(new ArrayList<>());
player.add(new ArrayList<>());
player.add(new ArrayList<>());
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
player.get(j).add(list.remove(0));
}
}
This project complete code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Card {
public String suit;
public String rank;
@Override
public String toString() {
return "[" + suit + " " + rank + "]";
}
}
public class CardDemo {
public static String[] SUITS = {"♥️", "♦️", "♣️", "♠️"};
public static String[] RANKS = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K"};
public static List<Card> buyCard() {
List<Card> list = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
String suit = SUITS[i];
String rank = RANKS[j];
Card card = new Card();
card.suit = suit;
card.rank = rank;
list.add(card);
}
}
return list;
}
public static void main(String[] args) {
List<Card> list = buyCard();
System.out.println("刚买回来的牌:");
System.out.println(list);
Collections.shuffle(list);
System.out.println("洗过之后的牌:");
System.out.println(list);
//三人轮流抽牌,每人一共抽5张
List<List<Card>> player = new ArrayList<>();
player.add(new ArrayList<>());
player.add(new ArrayList<>());
player.add(new ArrayList<>());
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
player.get(j).add(list.remove(0));
}
}
System.out.println("玩家1的手牌:");
System.out.println(player.get(0));
System.out.println("玩家2的手牌:");
System.out.println(player.get(1));
System.out.println("玩家3的手牌:");
System.out.println(player.get(2));
System.out.println("剩余的牌:");
System.out.println(list);
}
}
Show results: