一个极简单的扑克类

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        public enum Suit
        {
            Club,
            Diamond,
            Heart,
            Spade,
        }

        public enum Rank
        {
            Ace = 1,
            Deuce,
            Three,
            Four,
            Five,
            Six,
            Seven,
            Eight,
            Nine,
            Ten,
            Jack,
            Queen,
            King,
        }

        public class Card
        {
            public readonly Suit suit;
            public readonly Rank rank;

            public Card(Suit newSuit, Rank newRank)
            {
                suit = newSuit;
                rank = newRank;
            }
            private Card()
            {

            }

            public override string ToString()
            {
                return "The " + rank + " of " + suit + "s";
            }
        }

        public class Deck
        {
            private Card[] cards;
            public Deck()
            {
                cards = new Card[52];
                for (int suitVal = 0; suitVal < 4; ++suitVal)
                    for (int rankVal = 1; rankVal < 14; ++rankVal)
                        cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal,
                        (Rank)rankVal);
            }

            public Card GetCard(int cardNum)
            {
                if (cardNum < 0 || cardNum > 51)
                    throw (new System.ArgumentOutOfRangeException("cardNum", cardNum,
                            "Value must between the 0 and 51"));
                return cards[cardNum];
            }

            public void shuffle()
            {
                Card[] newDeck = new Card[52];
                bool[] assigned = new bool[52];
                Random sourceGen = new Random();
                for (int i = 0; i < 52; ++i)
                {
                    int destCard = 0;
                    while (true)
                    {
                        destCard = sourceGen.Next(52);
                        //if the card have exist, find again
                        if (assigned[destCard] == false) break;
                    }
                        assigned[destCard] = true;
                        //random put the origin cards to the new cards sequentially
                        newDeck[destCard] = cards[i];
                }
                newDeck.CopyTo(cards, 0);
            }
        }
        static void Main(string[] args)
        {
            Deck deck = new Deck();
            deck.shuffle();
            for (int i = 0; i < 52; ++i)
            {
                Card tempCard = deck.GetCard(i);
                Console.WriteLine(tempCard.ToString());
            }
            Console.ReadKey();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单扑克牌比大小游戏的Python代码: ```python import random class Card: def __init__(self, suit, rank): self.suit = suit self.rank = rank def __str__(self): return f"{self.rank} of {self.suit}" class Deck: def __init__(self): self.cards = [] for suit in ["Hearts", "Diamonds", "Clubs", "Spades"]: for rank in range(2, 11): self.cards.append(Card(suit, str(rank))) for rank in ["Jack", "Queen", "King", "Ace"]: self.cards.append(Card(suit, rank)) def __str__(self): deck_comp = "" for card in self.cards: deck_comp += f"\n {card.__str__()}" return f"The deck has:{deck_comp}" def shuffle(self): random.shuffle(self.cards) def deal(self): single_card = self.cards.pop() return single_card class Hand: def __init__(self): self.cards = [] self.value = 0 self.aces = 0 def add_card(self, card): self.cards.append(card) if card.rank.isdigit(): self.value += int(card.rank) elif card.rank in ["Jack", "Queen", "King"]: self.value += 10 else: self.aces += 1 self.value += 11 def adjust_for_ace(self): while self.value > 21 and self.aces: self.value -= 10 self.aces -= 1 class Chips: def __init__(self): self.total = 100 self.bet = 0 def win_bet(self): self.total += self.bet def lose_bet(self): self.total -= self.bet def take_bet(chips): while True: try: chips.bet = int(input("How many chips would you like to bet? ")) except: print("Sorry, please provide an integer.") else: if chips.bet > chips.total: print(f"Sorry, your bet can't exceed {chips.total}") else: break def hit(deck, hand): hand.add_card(deck.deal()) hand.adjust_for_ace() def hit_or_stand(deck, hand): global playing while True: x = input("Would you like to Hit or Stand? Enter 'h' or 's': ") if x[0].lower() == "h": hit(deck, hand) elif x[0].lower() == "s": print("Player stands. Dealer is playing.") playing = False else: print("Sorry, please try again.") continue break def show_some(player, dealer): print("\nDealer's Hand:") print("<card hidden>") print(dealer.cards[1]) print("\nPlayer's Hand:", *player.cards, sep="\n ") def show_all(player, dealer): print("\nDealer's Hand:", *dealer.cards, sep="\n ") print("Dealer's Hand =",dealer.value) print("\nPlayer's Hand:", *player.cards, sep="\n ") print("Player's Hand =",player.value) def player_busts(player, dealer, chips): print("BUST PLAYER!") chips.lose_bet() def player_wins(player, dealer, chips): print("PLAYER WINS!") chips.win_bet() def dealer_busts(player, dealer, chips): print("PLAYER WINS! DEALER BUSTED!") chips.win_bet() def dealer_wins(player, dealer, chips): print("DEALER WINS!") chips.lose_bet() def push(player, dealer): print("Dealer and player tie! PUSH") # 游戏逻辑 while True: print("Welcome to BlackJack! Get as close to 21 as you can without going over!\n\ Dealer hits until she reaches 17. Aces count as 1 or 11.") deck = Deck() deck.shuffle() player_hand = Hand() player_hand.add_card(deck.deal()) player_hand.add_card(deck.deal()) dealer_hand = Hand() dealer_hand.add_card(deck.deal()) dealer_hand.add_card(deck.deal()) player_chips = Chips() take_bet(player_chips) show_some(player_hand, dealer_hand) playing = True while playing: hit_or_stand(deck, player_hand) show_some(player_hand, dealer_hand) if player_hand.value > 21: player_busts(player_hand, dealer_hand, player_chips) break if player_hand.value <= 21: while dealer_hand.value < 17: hit(deck, dealer_hand) show_all(player_hand, dealer_hand) if dealer_hand.value > 21: dealer_busts(player_hand, dealer_hand, player_chips) elif dealer_hand.value > player_hand.value: dealer_wins(player_hand, dealer_hand, player_chips) elif dealer_hand.value < player_hand.value: player_wins(player_hand, dealer_hand, player_chips) else: push(player_hand, dealer_hand) print("\nPlayer's winnings stand at",player_chips.total) new_game = input("Would you like to play another hand? Enter 'y' or 'n': ") if new_game[0].lower() == "y": playing = True continue else: print("Thanks for playing!") break ``` 通过运行以上代码,你可以开始一个简单扑克牌比大小游戏。在游戏中,你可以和电脑进行对战,看看谁能更接近21点但不超过21点。祝你好运!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值