2017-03-23 3 views
2

私のクラスでは、私は働くGo Fishのゲームを作らなければなりません。私はこれまでクラスに入っていましたが、手に戻ってハンドを送り返す方法を理解できないので、チェックステートメントを作って、彼が4種類あるかどうかを調べることができます。助言がありますか?Pythonの魚に行く

class Card(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): 
     self.rank = rank 
     self.suit = suit 

    def __str__(self): 
     rep = self.rank + self.suit 
     return rep 


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) + " " 
     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) 

    def check(self,player): 
     print(player1) 
     for self.cards in rep: 
      print("i") 
      ##################### 
class Deck(Hand): 
    """ A deck of playing 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): 
     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 ("Out of cards!") 

def main(): 
    deck1 = Deck() 
    deck1.populate() 
    deck1.shuffle() 
    player1 = Hand() 
    player2 = Hand() 
    hands = [player1, player2] 
    deck1.deal(hands, per_hand = 5) 
    print(player1) 
    player1.check(player1) 
main() 
+2

プレイヤーのような感じです。それでは、ハンドを自分でチェックしてみませんか?すでにそれをやっていることの始まりがあるようです( 'check'は決してプレイヤーを参照しません)。あなたの意図が 'check'関数の目的であるかどうかははっきりしていません。それらのカードのために最高のハンドを返すか、それとも誰が​​勝者か、他のプレイヤーが渡されたのかを把握する必要がありますか? –

答えて

1

実行可能なGo Fishの動作バージョンで、コンソールから再生できるこのコードをチェックしてください。使用方法を確認してください:

def makeTurn(self): 
     print '%s\'s hand: %s' % (self.name,self.displayHand()) 
     chooseCard = raw_input('What card do you ask for? ').strip() 
     if chooseCard == 'quit': 
      sys.exit(0) 
     if chooseCard not in self.hand: 
      print 'You don\'t have that card. Try again! (or enter quit to exit)' 
      chooseCard = self.makeTurn() 
     return chooseCard 

ここでは完全なPython Go Fish gameです。

import random 
import sys 
from collections import defaultdict 

class HumanPlayer(object): 
    def __init__(self,deck): 
     self.hand = defaultdict(int) 
     self.book = [] 
     self.deck = deck #making a copy of deck, all changes within 
         #this class should affect the global deck 
     self.score = 0 
     self.name = raw_input('Name yourself: ') 

    def Draw(self): #assuming that deck is a global 
     cardDrawn = self.deck.pop() #removes the last card from deck 
     self.hand[cardDrawn] += 1 #adds card to hand 
     print '%s drew %s.' % (self.name,cardDrawn) 
     self.checkForBooks() 

    def checkForBooks(self): 
#  Removes all items of which are 4. 
     for key,val in self.hand.items(): #can't use iteritems() because we are modifying hand in loop 
      if val == 4: #completed a book 
       self.book.append(key) 
       print '%s completed the book of %s\'s.' % (self.name,key) 
       self.score += 1 
       del self.hand[key] 
     self.emptyCheck() 

    def emptyCheck(self): 
     if len(self.deck)!=0 and len(self.hand)==0: #checks if deck/hand is empty 
      self.Draw() 
    def displayHand(self): #Displays current hand, cards separated by spaces 
     return ' '.join(key for key,val in self.hand.iteritems() 
         for i in range(val)) #meh, make it prettier 

    def makeTurn(self): 
     print '%s\'s hand: %s' % (self.name,self.displayHand()) 
     chooseCard = raw_input('What card do you ask for? ').strip() 
     if chooseCard == 'quit': 
      sys.exit(0) 
     if chooseCard not in self.hand: 
      print 'You don\'t have that card. Try again! (or enter quit to exit)' 
      chooseCard = self.makeTurn() 
     return chooseCard 

    def fishFor(self,card): 
     if card in self.hand: # if card in hand, returns count and removes the card from hand 
      val = self.hand.pop(card) 
      self.emptyCheck() 
      return val 
     else: 
      return False 
    def gotCard(self,card,amount): 
     self.hand[card] += amount 
     self.checkForBooks() 



class Computer(HumanPlayer): 
    def __init__(self,deck): 
     self.name = 'Computer' 
     self.hand = defaultdict(int) 
     self.book = [] 
     self.deck = deck 
     self.opponentHas = set() 
     self.score = 0 

    def Draw(self): #assuming that deck is a global 
     cardDrawn = self.deck.pop() #removes the last card from deck 
     self.hand[cardDrawn] += 1 #adds card to hand 
     print '%s drew a card.' % (self.name) 
     self.checkForBooks() 

    ##AI: guesses cards that knows you have, then tries cards he has at random. 
    ##Improvements: remember if the card was rejected before, guess probabilities 
    def makeTurn(self): 
#  print self.displayHand(),self.opponentHas 
     candidates = list(self.opponentHas & set(self.hand.keys())) #checks for cards in hand that computer knows you have 
     if not candidates: 
      candidates = self.hand.keys() #if no intersection between those two, random guess 
     move = random.choice(candidates) 
     print '%s fishes for %s.' % (self.name,move) 
     return move 

    def fishFor(self,card): #Same as for humans players, but adds the card fished for to opponentHas list. 
     self.opponentHas.add(card) 
     if card in self.hand: # if card in hand, returns count and removes the card from hand 
      val = self.hand.pop(card) 
      self.emptyCheck() 
      return val 
     else: 
      return False 

    def gotCard(self,card,amount): 
     self.hand[card] += amount 
     self.opponentHas.discard(card) 
     self.checkForBooks() 

class PlayGoFish(object): 
    def __init__(self): 
     self.deck = ('2 3 4 5 6 7 8 9 10 J Q K A '*4).split(' ') 
     self.deck.remove('') 
     self.player = [HumanPlayer(self.deck),Computer(self.deck)] #makes counting turns easier 

    def endOfPlayCheck(self):#checks if hands/decks are empty using the any method 
      return self.deck or self.player[0].hand or self.player[1].hand 

    def play(self): 
     random.shuffle(self.deck) 
     for i in xrange(9): # Deal the first cards 
      self.player[0].Draw() 
      self.player[1].Draw() 
     turn = 0 
     while self.endOfPlayCheck(): 
      print '\nTurn %d (%s:%d %s:%d) %d cards remaining.' % (turn,self.player[0].name, 
        self.player[0].score,self.player[1].name,self.player[1].score,len(self.deck)) 
      whoseTurn = turn%2 
      otherPlayer = (turn+1)%2 
      while True: #loop until player finishes turn 
       cardFished = self.player[whoseTurn].makeTurn() 
       result = self.player[otherPlayer].fishFor(cardFished) 
       if not result: #Draws and ends turn 
        self.player[whoseTurn].Draw() 
        break 
       print '%s got %d more %s.' % (self.player[whoseTurn].name,result, cardFished) 
       self.player[whoseTurn].gotCard(cardFished,result) 
       if not self.endOfPlayCheck(): break 
      turn+=1 
     print '\nScores: \n%s: %d\n%s: %d\n' % (self.player[0].name,self.player[0].score, 
              self.player[1].name,self.player[1].score) 
     if self.player[0].score>self.player[1].score: 
      print self.player[0].name,'won!' 
     elif self.player[0].score==self.player[1].score: 
      print 'Draw!' 
     else: 
      print self.player[1].name,'won!' 

if __name__=="__main__": 
    game = PlayGoFish() 
    game.play() 
+1

初心者にはたくさんのコードがあります:) –

+7

ポスターの宿題を放棄したばかりの人は、他の人の仕事の完全なコピー/ペーストの例を与えてしまったのは残念です。私はあなたのオフィス全体が、自分の仕事をして学習するのではなく、SOからコピー/ペーストすることで学んだコーディング担当者で満たされ、将来彼らをサポートして仕事をしてくれることを願っています。 –

2

あなたの言ったことは本当に意味をなさない。 はクラスです。 player1はそのクラスのインスタンスです。個人的な言葉で言えば、Brandonhumanに戻す方法を尋ねるのと並行して、彼の歯を数えることができます。あなたはそうではありません:あなたは単にすべての人間に作用する歯を数える手順を使います。あなたのコードで特異的に見ると

、私はそれを必要とする必要があるライン

player1.check(player1) 

すべてで、この取引を

player1.check() 

player1でいると思います。このコールはチェックメソッドに行きます。ここでは、メソッドを呼び出したオブジェクトが自動的に最初の引数として自己と表示されます。 2番目のものを取り除いて使用するだけです

def check(self): 

混乱の原因を明確にしていますか?

関連する問題