Một bộ bài tiêu chuẩn 52 lá bao gồm bốn loại bài. chúng là bích, cơ tim, kim cương và câu lạc bộ. Mỗi bộ đồ bao gồm ba thẻ mặt, King, Queen và Jack. Và mỗi thứ hạng có nghĩa là các thẻ riêng lẻ từ cao đến thấp Show Vậy hãy cùng tìm hiểu các bước chơi đánh bài trong python Đề cươngChúng ta có thể trực tiếp chạy chương trình và lấy đầu ra. Chúng tôi không cần lấy bất kỳ đầu vào nào từ người dùng Điều kiện tiên quyết của dự ánChúng tôi không cần cài đặt bất kỳ gói nào để chạy mã này. Chúng ta cần nhập các mô-đun cần thiết. Chúng là các mô-đun ngẫu nhiên và sys Một mô-đun ngẫu nhiên được sử dụng để tạo số ngẫu nhiên Mô-đun sys cho phép chúng tôi truy cập các tham số và chức năng dành riêng cho hệ thống Triển khai mãĐầu tiên chúng ta phải nhập các mô-đun từ gói Tkinter Gói Tkinter được cài đặt sẵn trong python Vì vậy, hãy nhập 2 mô-đun này import random import sys Ở đây trong đoạn mã dưới đây, chúng ta sẽ tạo một số lớp và chức năng Đầu tiên chúng ta phải tạo một tên lớp gọi là thẻ và xác định các thuộc tính của nó class Card: suits = ['\u2666', '\u2665', '\u2663', '\u2660'] # ["Clubs", "Diamonds", "Hearts", "Spades"] ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] def __init__(self, suit=0, rank=0): """Default constructor """ self.suit = suit self.rank = rank def __str__(self): """Returns a human-readable string representation """ return '%s %s' % (Card.suits[self.suit], Card.ranks[self.rank]) # return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit]) def __lt__(self, other): """Overriding < operator """ t1 = self.rank, self.suit t2 = other.rank, other.suit return t1 < t2 Bây giờ, chúng ta phải tạo một lớp khác gọi là bộ bài để khởi tạo bộ bài 52 lá class Deck: def __init__(self): """Initializes the Deck with 52 cards.""" self.cards = [] for suit in range(4): for rank in range(13): card = Card(suit, rank) self.cards.append(card) self.shuffle() def __str__(self): """Returns a string representation of the deck.""" res = [] for card in self.cards: res.append(str(card)) return ', '.join(res) def __len__(self): """Overriding len operator""" return len(self.cards) def add_card(self, card): """Adds a card to the deck.""" self.cards.append(card) def pop_card(self, i=-1): """Removes and returns a card from the deck. i: index of the card to pop; by default, pops the last card. """ return self.cards.pop(i) def shuffle(self): """Shuffles the cards in this deck.""" random.shuffle(self.cards) def sort(self): """Sorts the cards in ascending order.""" self.cards.sort() def wincard(self, cards): """Get the highest winner card from list""" winner = cards[0] for card in cards: if winner < card: winner = card return winner Ở đây lớp này đại diện cho chơi bài class Hand(Deck): """Represents a hand of playing cards.""" def __init__(self, label=''): self.cards = [] self.label = label self.wincount = 0 def getlabel(self): """ Store players name """ return self.label def roundwinner(self): """ increasing the win count for player """ self.wincount += 1 def getwincount(self): """ get the winner count finally """ return self.wincount def __str__(self): return "Card for " + self.label + " is " + Deck.__str__(self) Phần chính của mã. trong phần này, chúng tôi khởi tạo bộ bài và số lượng người chơi, và cuối cùng là mã cuối cùng của chúng tôi để kiểm tra thẻ người chiến thắng def play(argv): deck = Deck() # initialize deck hands = [] for i in range(1, 5): player = 'Player %d' % i # default player name if len(argv) > i: player = argv[i] # get player name from command line parameter hands.append(Hand(player)) # add player while len(deck) > 0: for hand in hands: hand.add_card(deck.pop_card()) # remove card from deck and add to hand print(hands[0]) # print first player card input("Lets start playing. Press any key to continue : ") # wait for keypress for i in range(1, 14): cards = [] # collect card in a round floors = [] # get string representation for display in each round for hand in hands: card = hand.pop_card() cards.append(card) # collect individual card floors.append(hand.getlabel() + " : " + str(card)) # add string format for individual card winner_card = deck.wincard(cards) # check for winner card winner_hand = hands[cards.index(winner_card)] # find the winner hand from winner card winner_hand.roundwinner() # add score for winner hand print("Round", i, ":-", ", ".join(floors), ", Winner :- ", winner_hand.getlabel(), ":", winner_card) input() # wait for keypress to go for next round for hand in hands: # display the individual hand score after the 13 rounds of play print("Score for", hand.getlabel(), "is", hand.getwincount()) def main(argv=[]): answer = "Y" while answer.upper() == "Y": play(argv) answer = input("Play Again (Y/N)? : ") print("Bye Bye") if __name__ == '__main__': main(sys.argv) Mã nguồnĐây là mã nguồn hoàn chỉnh cho dự án import random import sys class Card: suits = ['\u2666', '\u2665', '\u2663', '\u2660'] # ["Clubs", "Diamonds", "Hearts", "Spades"] ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] def __init__(self, suit=0, rank=0): """Default constructor """ self.suit = suit self.rank = rank def __str__(self): """Returns a human-readable string representation """ return '%s %s' % (Card.suits[self.suit], Card.ranks[self.rank]) # return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit]) def __lt__(self, other): """Overriding < operator """ t1 = self.rank, self.suit t2 = other.rank, other.suit return t1 < t2 class Deck: def __init__(self): """Initializes the Deck with 52 cards.""" self.cards = [] for suit in range(4): for rank in range(13): card = Card(suit, rank) self.cards.append(card) self.shuffle() def __str__(self): """Returns a string representation of the deck.""" res = [] for card in self.cards: res.append(str(card)) return ', '.join(res) def __len__(self): """Overriding len operator""" return len(self.cards) def add_card(self, card): """Adds a card to the deck.""" self.cards.append(card) def pop_card(self, i=-1): """Removes and returns a card from the deck. i: index of the card to pop; by default, pops the last card. """ return self.cards.pop(i) def shuffle(self): """Shuffles the cards in this deck.""" random.shuffle(self.cards) def sort(self): """Sorts the cards in ascending order.""" self.cards.sort() def wincard(self, cards): """Get the highest winner card from list""" winner = cards[0] for card in cards: if winner < card: winner = card return winner class Hand(Deck): """Represents a hand of playing cards.""" def __init__(self, label=''): self.cards = [] self.label = label self.wincount = 0 def getlabel(self): """ Store players name """ return self.label def roundwinner(self): """ increasing the win count for player """ self.wincount += 1 def getwincount(self): """ get the winner count finally """ return self.wincount def __str__(self): return "Card for " + self.label + " is " + Deck.__str__(self) def play(argv): deck = Deck() # initialize deck hands = [] for i in range(1, 5): player = 'Player %d' % i # default player name if len(argv) > i: player = argv[i] # get player name from command line parameter hands.append(Hand(player)) # add player while len(deck) > 0: for hand in hands: hand.add_card(deck.pop_card()) # remove card from deck and add to hand print(hands[0]) # print first player card input("Lets start playing. Press any key to continue : ") # wait for keypress for i in range(1, 14): cards = [] # collect card in a round floors = [] # get string representation for display in each round for hand in hands: card = hand.pop_card() cards.append(card) # collect individual card floors.append(hand.getlabel() + " : " + str(card)) # add string format for individual card winner_card = deck.wincard(cards) # check for winner card winner_hand = hands[cards.index(winner_card)] # find the winner hand from winner card winner_hand.roundwinner() # add score for winner hand print("Round", i, ":-", ", ".join(floors), ", Winner :- ", winner_hand.getlabel(), ":", winner_card) input() # wait for keypress to go for next round for hand in hands: # display the individual hand score after the 13 rounds of play print("Score for", hand.getlabel(), "is", hand.getwincount()) def main(argv=[]): answer = "Y" while answer.upper() == "Y": play(argv) answer = input("Play Again (Y/N)? : ") print("Bye Bye") if __name__ == '__main__': main(sys.argv) đầu raChạy tập lệnh python trong dấu nhắc lệnh của bạn Xem thêm Theo dõi số điện thoại bằng Python Sau khi chạy lệnh đã cho ở trên, bạn sẽ nhận được lời nhắc chọn thẻ Đây là ảnh chụp màn hình của đầu ra Lưu ý quan trọngLưu ý rằng bạn vẫn có thể cải thiện mã này bằng một số gói GUI. Tùy thuộc vào bạn để chơi với mã và thực hiện các thay đổi. Chúng tôi sẽ thực hiện dự án này trong GUI trong các dự án sắp tới Bạn có thể tạo một trò chơi bài bằng Python không?Tạo Trò chơi bài của riêng bạn bằng lập trình Hướng đối tượng trong Python . Cách tốt nhất để học Lập trình hướng đối tượng là tạo thứ gì đó hướng đối tượng trong Python. Trong hướng dẫn này, chúng ta sẽ tạo một trò chơi bài đơn giản.
Làm cách nào để chơi trò chơi bài bằng Python?Trò chơi bài với Python. Bộ bài lớp
. Vòng lặp đầu tiên đi từ 2 đến 15 vì giá trị đầu tiên của quân bài là 2 và giá trị cuối cùng của quân bài là 14 (quân át). When you initialize the Deck object, the two for loops of __init__ create Card objects representing all the cards in a 52-card deck and add them to the card list. The first loop goes from 2 to 15 because the first value of a card is 2 and the last value of a card is 14 (the ace).
Điều gì mang lại cho bạn 13 điểm trong Hearts?Mỗi trái tim được tính là một điểm và nữ hoàng được tính 13 điểm. Mỗi trái tim - 1 điểm Q - 13 điểm Tổng cộng tất cả các điểm cho mỗi bàn tay phải là bội số của 26. Trò chơi thường được chơi tới 100 điểm (một số chơi tới 50).
Bí mật để chiến thắng tại Hearts là gì?Không bao giờ dẫn đầu với quân Át . Bạn không bao giờ biết khi nào ai đó đã vô hiệu hóa bộ đồ đó và có thể tặng bạn ♥️ hoặc Q♠. Đi đầu với quân Át đảm bảo bạn sẽ giành được mánh khóe. Mặt khác, điều này có thể tuyệt vời nếu bạn đang cố gắng Chụp mặt trăng. |