public class Game { private static final String[] suits = {"♥","♣","♦","♠"}; public List buyPoker() { List<Poker> pokers = new ArrayList<>(); for (int i = 0; i < 4; i++) { for (int j = 0; j <= 13; j++) { Poker poker = new Poker(suits[i],j); pokers.add(poker); } } return pokers; } public void shuffle(List<Poker> pokers) { for (int i = pokers.size()-1; i > 0; i--) { Random random = new Random(); int index = random.nextInt(i); swap(pokers,i,index); } } private void swap(List<Poker> pokers,int i,int j) { Poker tmp = pokers.get(i); pokers.set(i,pokers.get(j)); pokers.set(j,tmp); } public List<List<Poker>> game(List<Poker> pokers) { List<List<Poker>> hand = new ArrayList<>(); List<Poker> hand1 = new ArrayList<>(); List<Poker> hand2 = new ArrayList<>(); List<Poker> hand3 = new ArrayList<>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Poker removePoker = pokers.remove(0); hand.get(j).add(removePoker); } } return hand; } }有错吗
时间: 2024-03-04 21:49:38 浏览: 78
在game方法中,你创建了一个空的List<List<Poker>> hand,但没有将任何元素添加到它中。因此,在尝试将元素添加到hand.get(j)时,会抛出NullPointerException异常。为了解决这个问题,你需要在game方法中为hand添加3个空列表,然后才能向它们中添加元素。
你需要将game方法中的代码修改如下:
```
public List<List<Poker>> game(List<Poker> pokers) {
List<List<Poker>> hand = new ArrayList<>();
List<Poker> hand1 = new ArrayList<>();
List<Poker> hand2 = new ArrayList<>();
List<Poker> hand3 = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Poker removePoker = pokers.remove(0);
hand.get(j).add(removePoker);
}
}
return hand;
}
```
这样就能成功地将元素添加到hand中了。
相关问题
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; public class PokerGame { public static void main(String[] args) { List<String> pokerCards = new ArrayList<>(); List<String> players = new ArrayList<>(); HashMap<String, List<String>> playerHands = new HashMap<>(); // Step 1: Combine the cards into a deck 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("大王"); // Step 2: Shuffle the deck Collections.shuffle(pokerCards); // Step 3: Distribute cards to players 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); } // Step 4: Display each player's hand for (String player : players) { System.out.println(player + "的手牌:" + playerHands.get(player)); } } }解释这串代码
这段代码是一个简单的扑克牌游戏的实现。以下是对代码的解释:
1. 创建了一个名为PokerGame的类,其中包含了一个main方法作为程序的入口点。
2. 创建了两个ArrayList对象,pokerCards和players,分别用于存储扑克牌和玩家信息。
3. 创建了一个HashMap对象playerHands,用于存储每个玩家的手牌。
4. 使用两个String数组suits和ranks来表示扑克牌的花色和点数。
5. 使用嵌套的for循环将每个花色和点数组合在一起,构成一副完整的扑克牌,并将其添加到pokerCards中。
6. 添加了两张大小王到pokerCards中。
7. 使用Collections.shuffle方法对pokerCards进行洗牌,打乱顺序。
8. 添加了三个玩家到players中。
9. 计算每个玩家应该分到的牌数和剩余的牌数。
10. 使用另一个for循环将牌发给每个玩家,先将牌添加到hand中,再将hand添加到playerHands中。
11. 如果还有剩余的牌,则将剩余的牌发给最后一个玩家。
12. 最后,使用for循环遍历每个玩家,打印出他们的手牌。
这段代码实现了一个简单的扑克牌游戏,包括洗牌和发牌的过程,并将每个玩家的手牌打印出来。
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方法
要用随机数组洗牌的话,可以使用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`方法进行洗牌。最后输出每个玩家的手牌。
希望对你有所帮助!如有任何疑问,请随时提问。
阅读全文