用java写一串代码。现在一副扑克牌有52+2张四个花色"" "" """◆"十三个牌面12 3 45 67 8 910 JQK1.第一步组合成为一张扑克牌放到一个集合中2.将扑克牌打乱3.分给三个玩家小王小李小明4.查看每个人的手牌有哪些。用Hashmap写
时间: 2023-07-22 21:13:03 浏览: 112
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);
}
}
// Step 2: Shuffle the deck
Collections.shuffle(pokerCards);
// Step 3: Distribute cards to players
players.add("小王");
players.add("小李");
players.add("小明");
int cardIndex = 0;
for (String player : players) {
List<String> hand = new ArrayList<>();
hand.add(pokerCards.get(cardIndex));
cardIndex++;
playerHands.put(player, hand);
}
// Step 4: Display each player's hand
for (String player : players) {
System.out.println(player + "的手牌:" + playerHands.get(player));
}
}
}
注意:本代码仅实现了基本功能,并未处理多余的牌或错误输入。
阅读全文