import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; public class PokeGame { public static void main(String[] args) { List<String> pokerCards = new ArrayList<>(); List<String> players = new ArrayList<>(); HashMap<String, List<String>> playerHands = new HashMap<>(); //创建了两个ArrayList对象,pokerCards和players,分别用于存储扑克牌和玩家信息。 //创建了一个HashMap对象playerHands,用于存储每个玩家的手牌。 String[] suits = {"♠", "♥", "♣", "♦"}; String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; //使用两个String数组suits和ranks来表示扑克牌的花色和点数。 for (String suit : suits) { for (String rank : ranks) { pokerCards.add(suit + rank); } } //使用嵌套的for循环将每个花色和点数组合在一起,构成一副完整的扑克牌,并将其添加到pokerCards中。 pokerCards.add("小王"); pokerCards.add("大王"); //添加了两张大小王到pokerCards中。 Collections.shuffle(pokerCards); //使用Collections.shuffle方法对pokerCards进行洗牌,打乱顺序。 players.add("小王"); players.add("小李"); players.add("小明"); //添加了三个玩家到players中。 int numPlayers = players.size(); int cardsPerPlayer = pokerCards.size() / numPlayers; int remainingCards = pokerCards.size() % numPlayers; //计算每个玩家应该分到的牌数和剩余的牌数。 int cardIndex = 0; for (String player : players) { List<String> hand = new ArrayList<>(); for (int i = 0; i < cardsPerPlayer; i++) { hand.add(pokerCards.get(cardIndex)); cardIndex++; } if (remainingCards > 0) { hand.add(pokerCards.get(cardIndex)); cardIndex++; remainingCards--; } playerHands.put(player, hand); } // Step 4: Display each player's hand for (String player : players) { System.out.println(player + "的手牌:" + playerHands.get(player)); } } } 改成用随机数组洗牌而不是用shuffle方法
时间: 2024-04-19 12:26:06 浏览: 129
java.util.Collection源码分析与深度理解
要用随机数组洗牌的话,可以使用Fisher-Yates算法。以下是将代码改为使用随机数组洗牌的示例:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class PokeGame {
public static void main(String[] args) {
List<String> pokerCards = new ArrayList<>();
List<String> players = new ArrayList<>();
HashMap<String, List<String>> playerHands = new HashMap<>();
String[] suits = {"♠", "♥", "♣", "♦"};
String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String suit : suits) {
for (String rank : ranks) {
pokerCards.add(suit + rank);
}
}
pokerCards.add("小王");
pokerCards.add("大王");
shuffleCards(pokerCards);
players.add("小王");
players.add("小李");
players.add("小明");
int numPlayers = players.size();
int cardsPerPlayer = pokerCards.size() / numPlayers;
int remainingCards = pokerCards.size() % numPlayers;
int cardIndex = 0;
for (String player : players) {
List<String> hand = new ArrayList<>();
for (int i = 0; i < cardsPerPlayer; i++) {
hand.add(pokerCards.get(cardIndex));
cardIndex++;
}
if (remainingCards > 0) {
hand.add(pokerCards.get(cardIndex));
cardIndex++;
remainingCards--;
}
playerHands.put(player, hand);
}
for (String player : players) {
System.out.println(player + "的手牌:" + playerHands.get(player));
}
}
public static void shuffleCards(List<String> cards) {
Random random = new Random();
int size = cards.size();
for (int i = size - 1; i > 0; i--) {
int j = random.nextInt(i + 1);
String temp = cards.get(i);
cards.set(i, cards.get(j));
cards.set(j, temp);
}
}
}
```
这里新增了一个`shuffleCards`方法,使用Fisher-Yates算法实现洗牌功能。在洗牌前将扑克牌存储在列表中,然后调用`shuffleCards`方法进行洗牌。最后输出每个玩家的手牌。
希望对你有所帮助!如有任何疑问,请随时提问。
阅读全文