//实现斗地主洗牌发牌功能 //1.准备一副牌 //2.洗牌 //3.发牌 //4.看牌 Java实现
时间: 2023-11-12 10:07:36 浏览: 99
以下是Java实现斗地主洗牌发牌功能的代码:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class DouDiZhu {
public static void main(String[] args) {
// 1.准备一副牌
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("大王");
// 2.洗牌
Collections.shuffle(poker);
// 3.发牌
Map<String, List<String>> players = new HashMap<>();
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);
}
}
players.put("玩家1", player1);
players.put("玩家2", player2);
players.put("玩家3", player3);
// 4.看牌
System.out.println("玩家1的牌:" + player1);
System.out.println("玩家2的牌:" + player2);
System.out.println("玩家3的牌:" + player3);
System.out.println("底牌:" + dipai);
}
}
```
阅读全文