2011-04-10 9 views
1

저는 파이썬에 완전히 익숙하지 않아이 프로그램으로 무엇을 할 지 전혀 모릅니다. 간단한 파이썬 카드 게임

나는 여섯 명 선수가 있습니다 다음

  1. 않는 미니 카드 게임을 만들려고 해요.
  2. 각 핸드의 시작 부분에서 각 플레이어는 4 장의 카드를받습니다.
  3. 각 플레이어마다 손에있는 모든 페어가 내려집니다. 플레이어는 랭크가 10보다 작은 두 개의 카드로 구성된 한 쌍의 득점을 얻습니다 (에이스는 10보다 큽니다). 플레이어는 랭크가 10 이상인 두 개의 카드로 구성된 지불 자에 대해 두 포인트를 얻습니다. 두 쌍의 손으로 둘 다 내려 놓으십시오. 플레이어는 쌍의 종류에 대해 적절한 포인트 (1 또는 2)를 얻습니다.
  4. 쌍을 떨어 뜨린 후 손이 끝납니다. (플레이어는 실제로 입력을하지 않거나 결정을 내리지 않습니다. 나는 지루한 게임이라고 말했습니다.) 모든 카드는 데크에 반환되고, 다시 개편되며, 새로운 핸드가 시작됩니다.
  5. 각 라운드 종료 후 플레이어가 총점의 내림차순으로 나열됩니다. 두 명의 플레이어가 같은 수의 포인트로 묶인 경우 가장 낮은 번호의 플레이어가 먼저 인쇄됩니다.
  6. 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) 

+0

서식을 수정하십시오 (게시물 수정, Markdown 도움말 참조). 이것은 매우 읽을 수 없습니다. – dancek

+1

서식을 수정했습니다. 어떤 프로그래밍 경험이 있으며, 어떤 수업을 필요로합니까? 나는 그것이 객체 지향 프로그래밍이 아닌 것 같아요. – slezica

+0

당신은 루프에 관한 문제에 대해 좀 더 구체적으로 알아볼 필요가 있습니다. 또한 짧은 코드로 문제를 설명 할 수 있다면 더 쉽게 답변을 얻을 수 있습니다. –

답변

1

당신은 내가 때문에, 괜찮있는 모든 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_pairs에 drop_pair을 변경하고, 대신 하나의 쌍 모두 찾을했다. 발견 한 경우 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 

나는 당신의 모든 코드를 살펴,보다 구체적인 지침없이하지 않았다 2 점 가치, 4 - - - 종류의 가치가 셋 뿐인 '루프에 대한 도움'보다 너무 많이 도움이되지 수 있지만, 몇 가지 내가주의 :

파이썬에서
  • 대신 LEN 0에서 반복의 목록 바로 위에 반복 할 수 - 인덱스를 1 및 확인 .예를 들어 :

    # 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)) 
    
  • 그러나, 때로는 연속 번호의 범위를 반복하고 싶은가, 아니면 그냥 몇 가지 코드가이 범위를 사용할 수있는 경우 특정 횟수를 실행하려면 (또는 xrange , 이는 파이썬 2는 현재 번호의 '난'변수가 필요하지 않은 경우, 당신은 '_'이름을 사용할 수 있습니다, 또한) 큰 범위에 대한

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

더 효율적이다 자주 재 할당되며 정크 데이터를위한 좋은 장소입니다.

  • 목록을 중첩 할 수도 있습니다. 얼마나 많은 목록을 가지고 있는지 확실하지 않은 경우 매우 유용합니다. 당신이 여섯 플레이어가있는 경우 즉, 말을하는 것입니다 :

    # 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)] 
    

후자의 두 예제의 장점은 쉽게도 숫자 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()