java扑克牌发三张
时间: 2023-11-29 20:46:29 浏览: 86
java实现扑克牌分发功能
以下是Java实现扑克牌发三张的示例代码:
```java
import java.util.ArrayList;
import java.util.Collections;
public class Poker {
public static void main(String[] args) {
// 初始化扑克牌
ArrayList<String> poker = new ArrayList<>();
String[] colors = {"♠️", "♥️", "♣️", "♦️"};
String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
for (String color : colors) {
for (String number : numbers) {
poker.add(color + number);
}
}
poker.add("小王");
poker.add("大王");
// 洗牌
Collections.shuffle(poker);
// 发牌
ArrayList<String> player1 = new ArrayList<>();
ArrayList<String> player2 = new ArrayList<>();
ArrayList<String> player3 = new ArrayList<>();
ArrayList<String> lastThreeCards = new ArrayList<>();
for (int i = 0; i < poker.size(); i++) {
String card = poker.get(i);
if (i >= poker.size() - 3) {
lastThreeCards.add(card);
} else if (i % 3 == 0) {
player1.add(card);
} else if (i % 3 == 1) {
player2.add(card);
} else {
player3.add(card);
}
}
// 输出结果
System.out.println("玩家1的牌:" + player1);
System.out.println("玩家2的牌:" + player2);
System.out.println("玩家3的牌:" + player3);
System.out.println("底牌:" + lastThreeCards);
}
}
```
阅读全文