不是一次性把一个人的牌全发掉,而是一轮一轮的发
时间: 2024-04-16 20:26:07 浏览: 53
非常抱歉,我误解了您的要求。以下是按照一轮一轮发牌的Java代码:
```java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PokerGame {
public static void main(String[] args) {
String[] pokerDeck = createPokerDeck();
List<String>[] playerHands = dealCards(pokerDeck, 3, 5); // 每位玩家发5张牌
// 打印每位用户的手牌
for (int i = 0; i < playerHands.length; i++) {
System.out.println("Player " + (i+1) + " hand: " + Arrays.toString(playerHands[i].toArray()));
}
}
// 创建扑克牌数组
public static String[] createPokerDeck() {
String[] suits = {"红桃", "方片", "梅花", "黑桃"};
String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
String[] pokerDeck = new String[suits.length * ranks.length];
int index = 0;
for (String suit : suits) {
for (String rank : ranks) {
pokerDeck[index] = suit + rank;
index++;
}
}
return pokerDeck;
}
// 发牌给指定人数的用户
public static List<String>[] dealCards(String[] pokerDeck, int numPlayers, int cardsPerPlayer) {
List<String>[] playerHands = new ArrayList[numPlayers];
// 初始化每位玩家的手牌列表
for (int i = 0; i < numPlayers; i++) {
playerHands[i] = new ArrayList<>();
}
int deckIndex = 0;
int currentPlayer = 0;
// 一轮一轮发牌,直到每位玩家都有足够的手牌
while (playerHands[0].size() < cardsPerPlayer) {
playerHands[currentPlayer].add(pokerDeck[deckIndex]);
deckIndex++;
currentPlayer++;
// 如果当前玩家是最后一位玩家,则回到第一位玩家继续发牌
if (currentPlayer == numPlayers) {
currentPlayer = 0;
}
}
return playerHands;
}
}
```
这段代码将创建一个字符串数组,其中包含了扑克牌的所有牌。然后,它使用`dealCards`方法按照一轮一轮发牌的方式将牌发给指定数量的用户,并将每位用户的手牌打印出来。每位玩家一轮发一张牌,直到每位玩家都有足够的手牌。您可以根据需要修改代码以适应您的需求。
阅读全文