简单的Python卡牌游戏

发布于 2024-10-31 06:29:57 字数 10527 浏览 0 评论 0原文

我对 python 完全陌生,我不知道如何处理这个程序。

我正在尝试构建一个迷你纸牌游戏,它可以执行以下操作

  1. 有六名玩家。
  2. 每手牌开始时,每位玩家都会收到四张牌。
  3. 对于每个玩家,他们手上的所有对子都会被放下。对于由两张点数小于 10 的牌组成的对子(其中 A 的点数大于 10),玩家可获得 1 分。对于由两张 10 级或以上的牌组成的付款者,玩家可获得 2 分。如果手牌为两对,则均放下。玩家根据该对的类型获得相应的分数(1 或 2)。
  4. 牌局在对子掉落后结束。 (玩家实际上并没有输入信息或做出决定。我告诉过你这是一个无聊的游戏。)所有牌都返回到牌堆中,重新洗牌,并开始新的一手牌。
  5. 每轮结束后,选手按照总积分降序排列。如果两名玩家得分相同,则首先打印得分最低的玩家。
  6. 六轮过后,比赛结束。得分列表中的第一个玩家(如上所定义)赢得了游戏

输出应该如下所示:

Hand 1 deal:
Player 1's hand: 5D 5H KD AS
Player 2's hand: 7D 8D 9C JS
Player 3's hand: 3D 3H 6S 7C
Player 4's hand: 4C 6D 8S TH
Player 5's hand: 4H 5C 7S QC
Player 6's hand: 5S 6H 9H KH

Dropping pairs:
Player 1 dropped 1 pair.
Player 2 dropped no pairs.
Player 3 dropped 1 pair.
Player 4 dropped no pairs.
Player 5 dropped no pairs.
Player 6 dropped no pairs.

Score:
Player 1: 1
Player 3: 1
Player 2: 0
Player 4: 0
Player 5: 0
Player 6: 0

我为该游戏提供的模块包括。

CARDS.py

import string
import random

suits = ['S', 'C', 'D', 'H']
longsuits = ['spades', 'clubs', 'diamonds', 'hearts']

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
longranks = ['two', 'three', 'four', 'five', 'six', 'seven', 'eight',
        'nine', 'ten', 'jack', 'queen', 'king', 'ace']

ranklist = string.join(ranks, "")

ranklookup = {}
for i in range(len(ranks)):
    ranklookup[ranks[i]] = i

suitlookup = {}
for i in range(len(suits)):
    suitlookup[suits[i]] = i

class Card:
    """
    Class to hold information about a single playing card.  The card's rank
    and suit are stored.

    The constructor takes two arguments, the rank and suit of the card.  The
    rank and suit must be values from the ranks and suits list.

    >>> c1 = Card('8', 'C')
    >>> c2 = Card('K', 'H')
    >>> print c1
    8C
    >>> print c2
    KH
    """

    def __init__(self, rank, suit):
        self.__rank = ranklookup[rank]
        self.__suit = suitlookup[suit]

    def __cmp__(self, other):
        """
        Compare two card objects.

        >>> c1 = Card('8', 'C')
        >>> c2 = Card('K', 'H')
        >>> c1<c2
        True
        >>> c1>c2
        False
        >>> c1==c2
        False
        """
        if self.__rank == other.__rank:
            return cmp(self.__suit, other.__suit)
        else:
            return cmp(self.__rank, other.__rank)

    def __str__(self):
        """
        Return a two-character string representing the card.

        >>> c1 = Card('8', 'C')
        >>> str(c1)
        '8C'
        """
        return self.shortname()
    def __repr__(self):
        """
        Return a the Python code required to construt the card.

        >>> c1 = Card('8', 'C')
        >>> print repr(c1) .split(".",1)[1]
        Card('8', 'C')
        """
        return "%s.Card('%s', '%s')" % (self.__module__, ranks[self.__rank], suits[self.__suit])

    def suit(self):
        """
        Return a character representing the card's suit.  This will be one of the
        characters from suits.

        >>> c1 = Card('8', 'C')
        >>> c1.suit()
        'C'
        """
        return suits[self.__suit]

    def rank(self):
        """
        Return a character with the card's rank.  This will be one of the
        characters from ranks.

        >>> c1 = Card('8', 'C')
        >>> c1.rank()
        '8'
        """
        return ranks[self.__rank]

    def shortname(self):
        """
        Output a short two-character description of the card.

        >>> c1 = Card('8', 'C')
        >>> c1.shortname()
        '8C'
        """
        return ranks[self.__rank] + suits[self.__suit]

    def longname(self):
        """
        Return a long English description of the card.

        >>> c1 = Card('8', 'C')
        >>> c1.longname()
        'eight of clubs'
        """
        return longranks[self.__rank] + " of " + longsuits[self.__suit]



testhand = [ Card('9', 'H'), Card('6', 'C'), Card('7', 'S'), Card('6', 'D'), Card('A', 'H') ]

def deck():
    """
    Return an *unshuffled* deck of cards (list of card objects).

    >>> d = deck()
    >>> print hand_string(d)
    2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AS 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AC 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AD 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AH
    >>> print len(d)
    52
    """
    d = []
    for suit in range(len(suits)):
        for rank in range(len(ranks)):
            c = Card(ranks[rank], suits[suit])
            d.append(c)

    return d


def small_deck():
    """
    Return a small *unshuffled* deck of cards (list of card objects).  This is
    smaller than a regular deck and can be used for testing.

    >>> d = small_deck()
    >>> print hand_string(d)
    9S TS JS QS KS AS 9C TC JC QC KC AC 9D TD JD QD KD AD 9H TH JH QH KH AH
    >>> print len(d)
    24
    """
    d = []
    for suit in range(len(suits)):
        for rank in [7,8,9,10,11,12]:
            c = Card(ranks[rank], suits[suit])
            d.append(c)

    return d



def start_pair(hand):

    """
        Return index of first card in first pair of the hand.
        The index is for the order the hand has after sorting.
        If there are no pairs, return -1.

        Side effect:  The hand is sorted.
    """
    hand.sort()
    start = -1
    for i in range(len(hand)-1, 0, -1):
        if hand[i].rank() == hand[i-1].rank():
            start = i -1
    return start





def drop_pair(hand):
    """
    Remove a pair from the hand (list of card objects) if possible.  Return
    the new hand and the number of pairs dropped (0 or 1).  A "pair" is two
    cards with the same rank.

    If there is more than one pair, only the first is removed.

    The hand MUST be sorted by rank before this function is called.  This
    can be done with:
        hand.sort()

    >>> testhand.sort()
    >>> print hand_string(testhand)
    6C 6D 7S 9H AH
    >>> newhand, pts = drop_pair(testhand)
    >>> print hand_string(newhand)
    7S 9H AH
    >>> print pts
    1
    """
    newhand = hand[:]
    for i in range(len(newhand)-1):
        if newhand[i].rank() == newhand[i+1].rank():
            del(newhand[i+1])
            del(newhand[i])
            return newhand, 1
    return newhand, 0



def hand_string(hand):
    """
    Create a string that represents the cards in the player's hand.

    >>> hand_string(testhand)
    '6C 6D 7S 9H AH'
    >>> hand_string([])
    ''
    """

    return " ".join( [c.shortname() for c in hand] )


def _test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    _test()

模块结束。

pointsort.py

'''
    Module to sort players by points and player numbers.  See definition for
    function sortPlayers() for details of sort order.
'''

def playerToSort(p):
    ''' Represent player number such that lower-numbered players sort before higher-numbered '''
    return -p

def playerFromSort(p):
    ''' Extract original player number from playerToSort() representation '''
    return -p

def sortPlayers(playerNum, points):
    ''' Sort players by total points, then by player number within total points.
        Player numbers are sorted such that low-numbered players are highest.

        Returns list of tuples (playerNumber, points), sorted in *increasing* order
        by the two criteria.
    '''
    keys = []
    for n in playerNum:
        keys.append(playerToSort(n))

    order = []
    for i in range(len(points)):
        order.append((points[i], keys[i]))

    order.sort()
    result = []
    for i in range(len(order)):
        result.append((playerFromSort(order[i][1]), order[i][0]))

    return result

if __name__ == "__main__":
    points = [3, 4, 1, 2, 0, 3]
    number = [2, 1, 3, 4, 0, 5]
    order = sortPlayers(number, points)
    # Note that the following prints results in the WRONG order for A4 
    for i in range(len(order)):
        print "Player " + str(order[i][0]) + " had " + str(order[i][1]) + " points."



i was really hoping if someone could help me with the loops that this program needs, especially the point loop system. 
this what i have so far, 

import cards
import random

new = cards.small_deck()

print cards.hand_string(new)
print len(new)


player1 = []
player2 = []
player3 = []
player4 = []
player5 = []
player6 = []
#shuffle the cards
random.shuffle(new)
num = input('How many cards to deal to each player? ')
while num > 0:
        player1.append(new.pop(0))
        player2.append(new.pop(0))
        player3.append(new.pop(0))
        player4.append(new.pop(0))
        player5.append(new.pop(0))
        player6.append(new.pop(0))
        num = num - 1
#prints out 8 cards for each person

print 'the cards remaining in the deck are: '
print len(new)

#sorts player1 cards and removes the pairs
player1.sort()
print "sorted hand for player 1:"
print cards.hand_string(player1)
newplayer1 = []
player1points = 0

newplayer1, player1points = cards.drop_pair(player1)
print cards.hand_string(newplayer1)

#sorts player2 cards
player2.sort()
print "sorted hand for player 2:"
print cards.hand_string(player2)
newplayer2 = []
player2points = 0

newplayer2, player1points = cards.drop_pair(player2)
print cards.hand_string(newplayer2)



#sorts player3 cards
player3.sort()
print "sorted hand for player 3:"
print cards.hand_string(player3)
newplayer3 = []
player3points = 0

newplayer3, player1points = cards.drop_pair(player3)
print cards.hand_string(newplayer3)   


#sorts player4 cards
player4.sort()
print "sorted hand for player 4:"
print cards.hand_string(player4)
newplayer4 = []
player4points = 0

newplayer4, player1points = cards.drop_pair(player4)
print cards.hand_string(newplayer4)


#sorts player5 cards
player5.sort()
print "sorted hand for player 5:"
print cards.hand_string(player5)
newplayer5 = []
player5points = 0

newplayer5, player1points = cards.drop_pair(player5)
print cards.hand_string(newplayer5)

#sorts player6 cards
player6.sort()
print "sorted hand for player 6:"
print cards.hand_string(player6)
newplayer6 = []
player6points = 0

newplayer6, player1points = cards.drop_pair(player6)
print cards.hand_string(newplayer6)

在循环方面遇到了很大的麻烦,非常感谢我能得到的任何帮助,提前谢谢你

I'm completely new to python and I have no idea what to do with this program.

I'm trying to build a mini card game that does the following

  1. There are six players.
  2. At the start of each hand, each player is dealt four cards.
  3. For each player, all pairs in their hand are put down. A player gets one point for a pair consisting of two cards with rank less than 10 (with an ace counting as greater than 10). A player gets two points for a payer consisting of two cards with rank 10 or greater. If the hand as two pair, both are put down. The player gets the appropriate points (1 or 2) for the kind of pair.
  4. The hand ends after the pairs have been dropped. (Players do not actually enter input or make decisions. I told you it was a boring game.) All cards are returned to the deck, it is reshuffled, and a new hand is started.
  5. After each round, the players are listed in descending order of total points. If two players are tied for the same number of points, they are printed with the lowest-numbered player first.
  6. After six rounds, the game ends. The first player in the score list (as defined above) wins the game

Output should look like this:

Hand 1 deal:
Player 1's hand: 5D 5H KD AS
Player 2's hand: 7D 8D 9C JS
Player 3's hand: 3D 3H 6S 7C
Player 4's hand: 4C 6D 8S TH
Player 5's hand: 4H 5C 7S QC
Player 6's hand: 5S 6H 9H KH

Dropping pairs:
Player 1 dropped 1 pair.
Player 2 dropped no pairs.
Player 3 dropped 1 pair.
Player 4 dropped no pairs.
Player 5 dropped no pairs.
Player 6 dropped no pairs.

Score:
Player 1: 1
Player 3: 1
Player 2: 0
Player 4: 0
Player 5: 0
Player 6: 0

The modules I have for this game include.

CARDS.py

import string
import random

suits = ['S', 'C', 'D', 'H']
longsuits = ['spades', 'clubs', 'diamonds', 'hearts']

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
longranks = ['two', 'three', 'four', 'five', 'six', 'seven', 'eight',
        'nine', 'ten', 'jack', 'queen', 'king', 'ace']

ranklist = string.join(ranks, "")

ranklookup = {}
for i in range(len(ranks)):
    ranklookup[ranks[i]] = i

suitlookup = {}
for i in range(len(suits)):
    suitlookup[suits[i]] = i

class Card:
    """
    Class to hold information about a single playing card.  The card's rank
    and suit are stored.

    The constructor takes two arguments, the rank and suit of the card.  The
    rank and suit must be values from the ranks and suits list.

    >>> c1 = Card('8', 'C')
    >>> c2 = Card('K', 'H')
    >>> print c1
    8C
    >>> print c2
    KH
    """

    def __init__(self, rank, suit):
        self.__rank = ranklookup[rank]
        self.__suit = suitlookup[suit]

    def __cmp__(self, other):
        """
        Compare two card objects.

        >>> c1 = Card('8', 'C')
        >>> c2 = Card('K', 'H')
        >>> c1<c2
        True
        >>> c1>c2
        False
        >>> c1==c2
        False
        """
        if self.__rank == other.__rank:
            return cmp(self.__suit, other.__suit)
        else:
            return cmp(self.__rank, other.__rank)

    def __str__(self):
        """
        Return a two-character string representing the card.

        >>> c1 = Card('8', 'C')
        >>> str(c1)
        '8C'
        """
        return self.shortname()
    def __repr__(self):
        """
        Return a the Python code required to construt the card.

        >>> c1 = Card('8', 'C')
        >>> print repr(c1) .split(".",1)[1]
        Card('8', 'C')
        """
        return "%s.Card('%s', '%s')" % (self.__module__, ranks[self.__rank], suits[self.__suit])

    def suit(self):
        """
        Return a character representing the card's suit.  This will be one of the
        characters from suits.

        >>> c1 = Card('8', 'C')
        >>> c1.suit()
        'C'
        """
        return suits[self.__suit]

    def rank(self):
        """
        Return a character with the card's rank.  This will be one of the
        characters from ranks.

        >>> c1 = Card('8', 'C')
        >>> c1.rank()
        '8'
        """
        return ranks[self.__rank]

    def shortname(self):
        """
        Output a short two-character description of the card.

        >>> c1 = Card('8', 'C')
        >>> c1.shortname()
        '8C'
        """
        return ranks[self.__rank] + suits[self.__suit]

    def longname(self):
        """
        Return a long English description of the card.

        >>> c1 = Card('8', 'C')
        >>> c1.longname()
        'eight of clubs'
        """
        return longranks[self.__rank] + " of " + longsuits[self.__suit]



testhand = [ Card('9', 'H'), Card('6', 'C'), Card('7', 'S'), Card('6', 'D'), Card('A', 'H') ]

def deck():
    """
    Return an *unshuffled* deck of cards (list of card objects).

    >>> d = deck()
    >>> print hand_string(d)
    2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AS 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AC 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AD 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AH
    >>> print len(d)
    52
    """
    d = []
    for suit in range(len(suits)):
        for rank in range(len(ranks)):
            c = Card(ranks[rank], suits[suit])
            d.append(c)

    return d


def small_deck():
    """
    Return a small *unshuffled* deck of cards (list of card objects).  This is
    smaller than a regular deck and can be used for testing.

    >>> d = small_deck()
    >>> print hand_string(d)
    9S TS JS QS KS AS 9C TC JC QC KC AC 9D TD JD QD KD AD 9H TH JH QH KH AH
    >>> print len(d)
    24
    """
    d = []
    for suit in range(len(suits)):
        for rank in [7,8,9,10,11,12]:
            c = Card(ranks[rank], suits[suit])
            d.append(c)

    return d



def start_pair(hand):

    """
        Return index of first card in first pair of the hand.
        The index is for the order the hand has after sorting.
        If there are no pairs, return -1.

        Side effect:  The hand is sorted.
    """
    hand.sort()
    start = -1
    for i in range(len(hand)-1, 0, -1):
        if hand[i].rank() == hand[i-1].rank():
            start = i -1
    return start





def drop_pair(hand):
    """
    Remove a pair from the hand (list of card objects) if possible.  Return
    the new hand and the number of pairs dropped (0 or 1).  A "pair" is two
    cards with the same rank.

    If there is more than one pair, only the first is removed.

    The hand MUST be sorted by rank before this function is called.  This
    can be done with:
        hand.sort()

    >>> testhand.sort()
    >>> print hand_string(testhand)
    6C 6D 7S 9H AH
    >>> newhand, pts = drop_pair(testhand)
    >>> print hand_string(newhand)
    7S 9H AH
    >>> print pts
    1
    """
    newhand = hand[:]
    for i in range(len(newhand)-1):
        if newhand[i].rank() == newhand[i+1].rank():
            del(newhand[i+1])
            del(newhand[i])
            return newhand, 1
    return newhand, 0



def hand_string(hand):
    """
    Create a string that represents the cards in the player's hand.

    >>> hand_string(testhand)
    '6C 6D 7S 9H AH'
    >>> hand_string([])
    ''
    """

    return " ".join( [c.shortname() for c in hand] )


def _test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    _test()

end of module.

and

pointsort.py

'''
    Module to sort players by points and player numbers.  See definition for
    function sortPlayers() for details of sort order.
'''

def playerToSort(p):
    ''' Represent player number such that lower-numbered players sort before higher-numbered '''
    return -p

def playerFromSort(p):
    ''' Extract original player number from playerToSort() representation '''
    return -p

def sortPlayers(playerNum, points):
    ''' Sort players by total points, then by player number within total points.
        Player numbers are sorted such that low-numbered players are highest.

        Returns list of tuples (playerNumber, points), sorted in *increasing* order
        by the two criteria.
    '''
    keys = []
    for n in playerNum:
        keys.append(playerToSort(n))

    order = []
    for i in range(len(points)):
        order.append((points[i], keys[i]))

    order.sort()
    result = []
    for i in range(len(order)):
        result.append((playerFromSort(order[i][1]), order[i][0]))

    return result

if __name__ == "__main__":
    points = [3, 4, 1, 2, 0, 3]
    number = [2, 1, 3, 4, 0, 5]
    order = sortPlayers(number, points)
    # Note that the following prints results in the WRONG order for A4 
    for i in range(len(order)):
        print "Player " + str(order[i][0]) + " had " + str(order[i][1]) + " points."



i was really hoping if someone could help me with the loops that this program needs, especially the point loop system. 
this what i have so far, 

import cards
import random

new = cards.small_deck()

print cards.hand_string(new)
print len(new)


player1 = []
player2 = []
player3 = []
player4 = []
player5 = []
player6 = []
#shuffle the cards
random.shuffle(new)
num = input('How many cards to deal to each player? ')
while num > 0:
        player1.append(new.pop(0))
        player2.append(new.pop(0))
        player3.append(new.pop(0))
        player4.append(new.pop(0))
        player5.append(new.pop(0))
        player6.append(new.pop(0))
        num = num - 1
#prints out 8 cards for each person

print 'the cards remaining in the deck are: '
print len(new)

#sorts player1 cards and removes the pairs
player1.sort()
print "sorted hand for player 1:"
print cards.hand_string(player1)
newplayer1 = []
player1points = 0

newplayer1, player1points = cards.drop_pair(player1)
print cards.hand_string(newplayer1)

#sorts player2 cards
player2.sort()
print "sorted hand for player 2:"
print cards.hand_string(player2)
newplayer2 = []
player2points = 0

newplayer2, player1points = cards.drop_pair(player2)
print cards.hand_string(newplayer2)



#sorts player3 cards
player3.sort()
print "sorted hand for player 3:"
print cards.hand_string(player3)
newplayer3 = []
player3points = 0

newplayer3, player1points = cards.drop_pair(player3)
print cards.hand_string(newplayer3)   


#sorts player4 cards
player4.sort()
print "sorted hand for player 4:"
print cards.hand_string(player4)
newplayer4 = []
player4points = 0

newplayer4, player1points = cards.drop_pair(player4)
print cards.hand_string(newplayer4)


#sorts player5 cards
player5.sort()
print "sorted hand for player 5:"
print cards.hand_string(player5)
newplayer5 = []
player5points = 0

newplayer5, player1points = cards.drop_pair(player5)
print cards.hand_string(newplayer5)

#sorts player6 cards
player6.sort()
print "sorted hand for player 6:"
print cards.hand_string(player6)
newplayer6 = []
player6points = 0

newplayer6, player1points = cards.drop_pair(player6)
print cards.hand_string(newplayer6)

I have great troubles with loops and will apreciate any help I can get, thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2024-11-07 06:29:57

您似乎根本不使用 pointsort.py,这很好,因为我最终不需要它来实现您的预​​期输出。

# main.py
import cards
import random

deck = cards.small_deck()

class Player(object):
    def __init__(self, number):
        self.number = number
        self.points = 0
        self.hand = []

    def __str__(self):
        return "Player %d" % self.number

players = [Player(num + 1) for num in xrange(6)]  # create six numbered players
rounds = 6
hand_size = 4

for round_num in xrange(rounds):
    #shuffle the cards
    random.shuffle(deck)
    print "Hand %d deal:" % (round_num + 1)
    # deal cards
    for _ in xrange(hand_size):
        for player in players:
            # draw from the back instead of the front: possibly slightly more efficient
            player.hand.append(deck.pop())

    # sort player cards
    for player in players:
        player.hand.sort()
        print "%s's hand: %s" % (player, cards.hand_string(player.hand))
    print

    print "Dropping pairs:"
    for player in players:
        #removes the pairs
        new_hand, dropped_cards, pairs = cards.drop_pairs(player.hand)
        deck.extend(player.hand)  # realistically we can dump the hand now anyway.
        player.hand = []

        player.points += pairs

        how_many = pairs
        plural = "s"
        if pairs == 0:
            how_many = "no"
        elif pairs == 1:
            plural = ""
        print "%s dropped %s pair%s" % (player, how_many, plural)
    print

    print "Score:"
    for player in players:
        print "%s: %d" % (player, player.points)
    print

我还将 drop_pair 更改为 drop_pairs,并使其找到所有对,而不仅仅是一对。如果找到 3 个同类值 2 分,4 个同类值 3 分。

# in cards.py
def drop_pairs(hand):
    new_hand = hand[:]
    dropped_cards = []
    pairs = 0
    for card1, card2 in zip(hand, hand[1:]):  # look at every two-in-a-row cards
        if card1.rank() == card2.rank():
            try:
                new_hand.remove(card1)
            except ValueError:
                pass
            else:
                dropped_cards.append(card1)
            try:
                new_hand.remove(card2)
            except ValueError:
                pass
            else:
                dropped_cards.append(card2)
            pairs += 1
    return new_hand, dropped_cards, pairs

我没有查看您的所有代码,也没有比“循环帮助”更具体的指导,我虽然帮不了什么忙,但我注意到了一些事情:

  • 在 python 中,你可以直接迭代列表,而不是从 0 迭代到 len - 1 并检查索引。例如:

    # 这可以工作,但不推荐
    对于范围内的套装(len(suits)):
        对于范围内的排名(len(ranks)):
            c = 卡(等级[等级], 套装[套装])
            d.追加(c)
    
    # 这更干净
    对于西装中的西装:
        对于排名:
            d.append(卡牌(等级、花色))
    
  • 但是,有时您确实想要迭代一系列连续数字,或者您只是希望某些代码运行一定次数,在这种情况下您可以使用 range (或 xrange,在 python 2 中对于大范围更有效)

    # 不要这样做
    当 num > 时0:
        做某事()
        数字 = 数字 - 1
    
    # 改为这样做!
    对于 xrange(num) 中的 i:
        做某事()
    

此外,如果您不需要当前数字的“i”变量,则可以使用名称“_”,它经常被重新分配,并且是垃圾数据的好地方。

  • 您还可以嵌套列表。如果您不确定将拥有多少个列表,这非常有用。也就是说,如果你有六名玩家:

    <前><代码># 不不不
    玩家1 = []
    玩家2 = []
    玩家3 = []
    玩家4 = []
    玩家5 = []
    玩家6 = []

    # 更好的...
    玩家 = [[], [], [], [], [], []]

    # 是的!
    玩家=[]
    对于 xrange(6) 中的 _:
    玩家.append([])

    # 还有一个名为“列表推导式”的快捷方式,它允许我们
    # 让我们的列表定义更加简洁。
    玩家 = [[] for _ in xrange(6)]

后两个例子的优点是你可以通过改变数字6,甚至使用变量来轻松改变游戏中有多少个玩家。他们还减少了代码重复:每个玩家处理牌的逻辑被复制粘贴 6 次,而他们之间只改变了一个数字。这不太可扩展:如果您想添加或删除玩家,则必须在代码中添加或删除许多重复的块。反而:

    # no! bad!
    player1 = []
    player2 = []
    player3 = []
    player4 = []
    player5 = []
    player6 = []

    player1.do_stuff()
    player2.do_stuff()
    player3.do_stuff()
    player4.do_stuff()
    player5.do_stuff()
    player6.do_stuff()

    # much easier!
    for player in players:
        player.do_stuff()

You appear to not use pointsort.py at all, which is fine, since I didn't end up needing it for your expected output.

# main.py
import cards
import random

deck = cards.small_deck()

class Player(object):
    def __init__(self, number):
        self.number = number
        self.points = 0
        self.hand = []

    def __str__(self):
        return "Player %d" % self.number

players = [Player(num + 1) for num in xrange(6)]  # create six numbered players
rounds = 6
hand_size = 4

for round_num in xrange(rounds):
    #shuffle the cards
    random.shuffle(deck)
    print "Hand %d deal:" % (round_num + 1)
    # deal cards
    for _ in xrange(hand_size):
        for player in players:
            # draw from the back instead of the front: possibly slightly more efficient
            player.hand.append(deck.pop())

    # sort player cards
    for player in players:
        player.hand.sort()
        print "%s's hand: %s" % (player, cards.hand_string(player.hand))
    print

    print "Dropping pairs:"
    for player in players:
        #removes the pairs
        new_hand, dropped_cards, pairs = cards.drop_pairs(player.hand)
        deck.extend(player.hand)  # realistically we can dump the hand now anyway.
        player.hand = []

        player.points += pairs

        how_many = pairs
        plural = "s"
        if pairs == 0:
            how_many = "no"
        elif pairs == 1:
            plural = ""
        print "%s dropped %s pair%s" % (player, how_many, plural)
    print

    print "Score:"
    for player in players:
        print "%s: %d" % (player, player.points)
    print

I also changed drop_pair to drop_pairs, and made it find all of the pairs instead of just one. If it finds three-of-a-kind it's worth 2 points, and four-of-a-kind is worth 3.

# in cards.py
def drop_pairs(hand):
    new_hand = hand[:]
    dropped_cards = []
    pairs = 0
    for card1, card2 in zip(hand, hand[1:]):  # look at every two-in-a-row cards
        if card1.rank() == card2.rank():
            try:
                new_hand.remove(card1)
            except ValueError:
                pass
            else:
                dropped_cards.append(card1)
            try:
                new_hand.remove(card2)
            except ValueError:
                pass
            else:
                dropped_cards.append(card2)
            pairs += 1
    return new_hand, dropped_cards, pairs

I didn't look over all of your code, and without more specific guidance than 'help with loops' I can't help too much, but a few things I noticed:

  • In python you can iterate directly over a list instead of iterating from 0 to len - 1 and checking the indices. For example:

    # this will work, but it's not recommended
    for suit in range(len(suits)):
        for rank in range(len(ranks)):
            c = Card(ranks[rank], suits[suit])
            d.append(c)
    
    # this is much cleaner
    for suit in suits:
        for rank in ranks:
            d.append(Card(rank, suit))
    
  • However, sometimes you DO want to iterate over a range of consecutive numbers, or you just want some code to run a certain number of times, in which case you can use range (or xrange, which in python 2 is more efficient for large ranges)

    # don't do this
    while num > 0:
        do_something()
        num = num - 1
    
    # Do this instead!
    for i in xrange(num):
        do_something()
    

Also, if you don't need the 'i' variable for the current number, you can use the name '_', which gets reassigned often and is a good place for junk data.

  • You can also nest lists. This is very useful if you're not sure how many lists you're going to have. That is to say, if you have six players:

    # no no no
    player1 = []
    player2 = []
    player3 = []
    player4 = []
    player5 = []
    player6 = []
    
    # better...
    players = [[], [], [], [], [], []]
    
    # yes!
    players = []
    for _ in xrange(6):
        players.append([])
    
    # There's also a shortcut called List Comprehensions, which allows us
    # to be even more concise with our list definitions.
    players = [[] for _ in xrange(6)]
    

The advantage of the latter two examples is that you can easily change how many players there are in the game by changing the number 6, or even using a variable instead. They also cut down on code repetition: your logic for each player dealing with their cards is copy pasted 6 times with only a single number changed between them. This isn't very extensible: if you wanted to add or remove players you would have to add or remove that many repeated blocks from your code. Instead:

    # no! bad!
    player1 = []
    player2 = []
    player3 = []
    player4 = []
    player5 = []
    player6 = []

    player1.do_stuff()
    player2.do_stuff()
    player3.do_stuff()
    player4.do_stuff()
    player5.do_stuff()
    player6.do_stuff()

    # much easier!
    for player in players:
        player.do_stuff()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文