编程模拟斗地主的发牌过程。
一副扑克有54张牌,每张扑克包括花色(Suit)和牌面(Face)两个部分,花色包括:黑桃(spades)、红桃(hearts)、草花(clubs)、方块(diamonds) 、red、black。黑桃,红桃,草花和方块又有13张牌面:2、3……10、Jack(J)、Queen(Q)、King(K)、A。red 和 black 仅有1种牌面Joker(JK)。每位玩家17张牌,剩余3张底牌
要求随机发5次牌,输出每次发牌后,每位玩家的牌和剩余的底牌:
示例输出:
玩家1: 黑桃_A 红桃_A 草花_2 ….
玩家2: 方块_K 草花_J 黑桃_Q ….
玩家3: 黑桃_4 方块_10 草花_8 ….
底牌: 方块_2 小王 大王
java代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;public class DouDiZhu {private static final String[] SUITS = {"黑桃", "红桃", "草花", "方块"};private static final String[] FACES = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};private static final String[] JOKERS = {"大王", "小王"};public static void main(String[] args) {for (int i = 0; i < 5; i++) {dealCards();System.out.println();}}private static void dealCards() {List<String> deck = createDeck();Collections.shuffle(deck, new Random());List<String> player1 = new ArrayList<>();List<String> player2 = new ArrayList<>();List<String> player3 = new ArrayList<>();List<String> bottomCards = new ArrayList<>();for (int i = 0; i < 51; i++) {if (i % 3 == 0) {player1.add(deck.get(i));} else if (i % 3 == 1) {player2.add(deck.get(i));} else {player3.add(deck.get(i));}}bottomCards.add(deck.get(51));bottomCards.add(deck.get(52));bottomCards.add(deck.get(53));System.out.println("玩家1: " + player1);System.out.println("玩家2: " + player2);System.out.println("玩家3: " + player3);System.out.println("底牌: " + bottomCards);}private static List<String> createDeck() {List<String> deck = new ArrayList<>();for (String suit : SUITS) {for (String face : FACES) {deck.add(suit + "_" + face);}}deck.add("红_" + JOKERS[0]);deck.add("黑_" + JOKERS[1]);return deck;}
}
-
牌组创建:
createDeck
方法创建一个字符串列表表示扑克牌。包括 52 张普通牌和 2 张王。 -
洗牌:
使用Collections.shuffle
随机打乱牌组顺序。 -
发牌:
前 51 张牌分给三位玩家。最后三张保留为底牌。 -
输出:
程序输出每位玩家的手牌和底牌。
c++代码:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>void createDeck(std::vector<std::string>& deck) {const std::vector<std::string> suits = {"黑桃", "红桃", "草花", "方块"};const std::vector<std::string> faces = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};const std::vector<std::string> jokers = {"大王", "小王"};for (const auto& suit : suits) {for (const auto& face : faces) {deck.push_back(suit + "_" + face);}}deck.push_back("红_" + jokers[0]);deck.push_back("黑_" + jokers[1]);
}void dealCards() {std::vector<std::string> deck;createDeck(deck);std::random_shuffle(deck.begin(), deck.end());std::vector<std::string> player1, player2, player3, bottomCards;for (int i = 0; i < 51; ++i) {if (i % 3 == 0) {player1.push_back(deck[i]);} else if (i % 3 == 1) {player2.push_back(deck[i]);} else {player3.push_back(deck[i]);}}bottomCards.push_back(deck[51]);bottomCards.push_back(deck[52]);bottomCards.push_back(deck[53]);std::cout << "玩家1: ";for (const auto& card : player1) std::cout << card << " ";std::cout << "\n玩家2: ";for (const auto& card : player2) std::cout << card << " ";std::cout << "\n玩家3: ";for (const auto& card : player3) std::cout << card << " ";std::cout << "\n底牌: ";for (const auto& card : bottomCards) std::cout << card << " ";std::cout << "\n";
}int main() {std::srand(unsigned(std::time(nullptr)));for (int i = 0; i < 5; ++i) {dealCards();std::cout << "\n";}return 0;
}