java斗地主洗牌发牌
时间: 2023-11-10 20:04:24 浏览: 78
Java斗地主洗牌发牌的实现可以分为以下几个步骤:
1. 初始化一副扑克牌,包括52张牌,每张牌由花色和点数组成。
2. 对扑克牌进行洗牌操作,打乱牌的顺序。
3. 将洗好的牌发给三个玩家,每个玩家17张牌,底牌留3张。
以下是Java代码实现:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DouDiZhu {
public static void main(String[] args) {
// 初始化扑克牌
List<String> poker = new ArrayList<>();
String[] colors = {"♠", "♥", "♣", "♦"};
String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
for (String color : colors) {
for (String number : numbers) {
poker.add(color + number);
}
}
poker.add("小王");
poker.add("大王");
// 洗牌
Collections.shuffle(poker);
// 发牌
List<String> player1 = new ArrayList<>();
List<String> player2 = new ArrayList<>();
List<String> player3 = new ArrayList<>();
List<String> dipai = new ArrayList<>();
for (int i = 0; i < poker.size(); i++) {
String card = poker.get(i);
if (i >= 51) {
dipai.add(card);
} else if (i % 3 == 0) {
player1.add(card);
} else if (i % 3 == 1) {
player2.add(card);
} else if (i % 3 == 2) {
player3.add(card);
}
}
// 输出结果
System.out.println("玩家1:" + player1);
System.out.println("玩家2:" + player2);
System.out.println("玩家3:" + player3);
System.out.println("底牌:" + dipai);
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)