編寫模組
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# 簡單遊戲模組

class Player(object):
    """A player for a game."""

    def __init__(self, name, score = 0):
        self.name = name
        self.score = score

    def __str__(self):    # 為該物件建立其字串表示方式:當物件被列印時會顯示這個字串
        rep = self.name + ":\t" + str(self.score)
        return rep

    def ask_yes_no(question):   
        """Ask a yes or no question."""
        response = None
        while response not in ("y", "n"):
            response = input(question).lower()
        return response

    def ask_number(question, low, high):
        """Ask for a number within a range."""
        response = None
        while response not in range(low, high):
            response = int(input(question))
        return response

    if __name__ == "__main__":   # 如果改程式是直接執行的,if語句條件為真。如果該檔案是作為模組使用的,則為假。
        print("You ran this module directly (and did not 'import' it).")   # 告訴使用者該檔案應該被引用而不是直接執行
        input("\n\nPress the Enter key to exit.")


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# 卡牌遊戲需要用到的基礎類


class Card(object):  # 該類基於object類  ,卡牌類

    """A playing card."""
    RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
    SUITS = ["c", "d", "h", "s"]     # 花色

    def __init__(self, rank, suit, face_up = True):
        self.rank = rank
        self.suit = suit
        self.is_face_up = face_up   # bool變數 卡牌是否面朝上

    def __str__(self):    # 為該物件建立其字串表示方式:當物件被列印時會顯示這個字串
        if self.is_face_up:
            rep = self.rank + self.suit
        else:
            rep = "XX"
        return rep

    def flip(self):   # 翻牌
        self.is_face_up = not self.is_face_up


class Hand(object):     # 手牌類
    """A hand of playing cards."""
    def __init__(self):
        self.cards = []

    def __str__(self):     # 為該物件建立其字串表示方式:當物件被列印時會顯示這個字串
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + "\t"
        else:
            rep = "<empty>"
        return rep

    def clear(self):    # 清牌
        self.cards = []

    def add(self, card):   # 添牌
        self.cards.append(card)

    def give(self, card, other_hand):  # 交換牌
        self.cards.remove(card)
        other_hand.add(card)


class Deck(Hand):    # 副牌類, 繼承Hand類
    """A deck of palying cards"""
    def populate(self):   # 建立一副牌
        for suit in Card.SUITS:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))

    def shuffle(self):   # 洗牌
        import random
        random.shuffle(self.cards)

    def deal(self, hands, per_hand = 1):   # 發牌  per_hand(玩家應得牌數),hands(玩家列表)
        for rounds in range(per_hand):
            for hand in hands:
                if self.cards:
                    top_card = self.cards[0]
                    self.give(top_card, hand)
                else:
                    print("Can't continue deal. Out of cards!")

    if __name__ == "__main__":
        print("This is a module with classes for playing cards!")
        input("\n\nPress the enter key to exit.")

引入模組

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
卡牌遊戲: 二十一點(blackjack)
玩法:
玩家根據點數來玩牌
每個玩家都希望總點數儘量接近21點而不爆掉
帶數字的牌以其面值計算點數
A牌算作1點或11點(由玩家決定),J、Q、K則算作10點
計算機是莊家,同時跟1-7名玩家對壘。每輪牌局開始時,計算機給每位參與者(包括自己)發兩張牌。玩家可以檢視自己的全部牌,
程式會把玩家的總點數顯示出來。但此時莊家的一張牌暫時是看不到的。
接下來,每位玩家都會得到一個繼續拿牌的機會。每位玩家每次都只能要一張牌,想要多少次都可以。但是如果玩家的總點數超過了
21(即“爆掉”),玩家就輸了。如果所有玩家都爆掉了,計算機就翻開它自己的第一張牌。本局結束。否則,賭局繼續。只要計算機
的總點數小於17,那他就必須繼續拿牌。如果計算機爆掉了,則所有沒有爆掉的玩家就贏了;否則,每位沒爆掉的玩家的總點數就會
拿來跟計算機的比。如果玩家的總點數大,則玩家贏。如果玩家的總點數小,則玩家輸。如果兩者的總點數相等,則玩家跟計算機握
手言和(平手)。
"""
import Games, Cards_Module    # 引入模組


class BJ_Card(Cards_Module.Card):    # 卡牌類, 繼承Card類
    """A Blackjack Card."""
    ACE_VALUE = 1          # "A"的值,預設為1

    @property
    def value(self):     # 獲取牌所對應的值
        if self.is_face_up:
            v = BJ_Card.RANKS.index(self.rank) + 1
            if v > 10:
                v = 10
        else:
            v = None
        return v


class BJ_Deck(Cards_Module.Deck):   # 副牌類, 繼承Deck類
    """A Blackjack Deck."""
    def populate(self):     # 重寫populate方法,允許BJ_Deck物件填入BJ_Card物件
        for suit in BJ_Card.SUITS:
            for rank in BJ_Card.RANKS:
                self.cards.append(BJ_Card(rank, suit))


class BJ_Hand(Cards_Module.Hand):  # 手牌類  , 繼承Hand類
    """ A Blackjack Hand."""
    def __init__(self, name):   # 重寫構造器,新增一個表示擁有者的name特性
        super(BJ_Hand, self).__init__()   # super(BJ_Hand,self)用於找到BJ_Hand的父類,也可以直接呼叫父類名
        self.name = name

    def __str__(self):   # 重寫該方法,使其可以顯示這手牌的總點數
        rep = self.name + ":\t" + super(BJ_Hand,self).__str__()
        if self.total:
            rep += "(" + str(self.total) + ")"
        return rep

    @property
    def total(self):
        # 如果當前這手牌中有一張牌的value為None,則total為None
        for card in self.cards:
            if not card.value:
                return None

        # 把牌的點數加起來,A的點數記為1
            t = 0
            for card in self.cards:
                t += card.value

        # 判斷當前這手牌中有沒有A
        contains_ace = False
        for card in self.cards:
            if card.value == BJ_Card.ACE_VALUE:
                contains_ace = True

        # 如果有A且total夠小,則將A記為11
        if contains_ace and t <= 11:
            # 因為已經為這張A加了1,所有這裡只加10
            t += 10

        return t

    def is_busted(self):  # 判斷是否爆掉
        return self.total > 21


class BJ_Player(BJ_Hand):   # 玩家類  繼承BJ_Hand類
    """A Blackjack Player."""
    def is_hitting(self):   # 是否再次叫牌
        response = Games.Player.ask_yes_no("\n" + self.name +", do you want a hit?(Y/N):")
        return response == "y"

    def bust(self):   # 宣告該玩家爆掉
        print(self.name, "busts.")
        self.lose()

    def lose(self):   # 宣告玩家輸了
        print(self.name, "loses.")

    def win(self):    # 宣告玩家贏了
        print(self.name, "wins.")

    def push(self):   # 宣告玩家平手
        print(self.name, "pushes.")


class BJ_Dealer(BJ_Hand):   # 莊家類   繼承BJ_Hand類
    """A Blackjack Dealer."""
    def is_hitting(self):   # 總點數不足17必須叫牌
        return self.total < 17

    def bust(self):    # 宣告莊家爆掉
        print(self.name, "busts.")

    def flip_first_card(self):   # 翻開莊家的第一張牌
        first_card = self.cards[0]
        first_card.flip()


class BJ_Game(object):   # 21點遊戲 類,用於建立一局遊戲
    """A Blackjack Game."""
    def __init__(self, names):
        self.players = []
        for name in names:   # 玩家加入遊戲
            player = BJ_Player(name)
            self.players.append(player)

        self.dealer = BJ_Dealer("Dealer")   # 建立莊家

        self.deck = BJ_Deck()   # 定義一副牌
        self.deck.populate()    # 構造一副牌
        self.deck.shuffle()     # 洗牌

    @property
    def still_playing(self):   # 存活玩家列表
        sp = []
        for player in self.players:
            if not player.is_busted():
                sp.append(player)
        return sp

    def __additional_cards(self, player):   # 向玩家或莊家加發一張牌
        while not player.is_busted() and player.is_hitting():
            self.deck.deal([player])
            print(player)
            if player.is_busted():
                player.bust()

    def play(self):
        # 給每個人發兩張牌
        self.deck.deal(self.players + [self.dealer], per_hand=2)
        self.dealer.flip_first_card()  # 隱藏莊家的第一張牌
        for player in self.players:
            print(player)
        print(self.dealer)

        # 給所有玩家加牌
        for player in self.players:
            self.__additional_cards(player)

        self.dealer.flip_first_card()  # 翻開莊家的第一張牌
        if not self.still_playing:
            # 由於所有玩家都爆掉了,因此直接亮出莊家手中的牌即可
            print(self.dealer)
        else:
            # 給莊家加牌
            print(self.dealer)
            self.__additional_cards(self.dealer)

            if self.dealer.is_busted():
                # 所有還在玩的玩家都獲勝
                for player in self.still_playing:
                    player.win()
            else:
                # 每位還在玩的玩家分別跟莊家比點數
                for player in self.still_playing:
                    if player.total > self.dealer.total:
                        player.win()
                    elif player.total < self.dealer.total:
                        player.lose()
                    else:
                        player.push()

        # 清空所有人手中的牌
        for player in self.players:
            player.clear()
        self.players.clear()


def main():
    print("\t\tWelcome to Blackjack!\n")
    names = []     # 玩家列表
    number = Games.Player.ask_number("How many players? (1-7):", low = 1, high = 8)
    for i in range(number):     # 新增玩家
        name = input("Enter player name:")
        names.append(name)
    print()

    game = BJ_Game(names)    # 建立遊戲

    again = None
    while again != "n":
        game.play()      # 開始遊戲
        again = Games.Player.ask_yes_no("\nDO you want to play again?:(Y/N)")   # 再來一局?

# 程式主體
main()
input("\n\nPress the enter key to exit.")


.