According to the "Aha" in the ideas to achieve this problem, but with the result is not the same, I myself with a card to try, found that my results like a little, maybe I understand the deviation.
#Kitten Fishing#calculate the number of each card on the table#Use the Defaultdict class and set the default type to int, which is the default value of 0#cardcounts = defaultdict (int)#but Deque has a corresponding method.defHenhenhaahaa (): fromCollectionsImportdeque hen= Deque ()#Hen's handHaa = Deque ()#Haa's HandTable = Deque ()#the cards on the table #Hand ligation Initialization forCardinch[2,4,1,2,5,6]: Hen.append (card) forCardinch[3,1,3,5,6,4]: Haa.append (card)#when two people's hands are not for snacks, the game continues #If someone is playing the same card as a card on the table #It takes two cards and all the cards in the middle to take them away. #no more than two cards on the table. #as long as the number of target cards is calculated, this can be retrieved #Refresh the cards on the table (play cards, draw cards) defrefreshtable (person, card): Table.append (Card)ifTable.count (Card) >=2: whileTable.count (Card) >0:person.append (Table.pop ()) whileLen (hen)!=0 andLen (haa)! =0:#print "tab:", List (table)i = Hen.popleft ()#hen the cards first#print "Hen put", IRefreshtable (hen, i)#Hen Action#print "hen:", List (hen) #print "tab:", List (table)j = Haa.popleft ()#Haa after the card#print "Haa put", JRefreshtable (Haa, J)#haa Action#print "Haa:", List (HAA)#print "tab:", List (table)#print "Next Turn" ifLen (hen)! =0:Print "Hen Win" Print "Hen:", List (hen)Else: Print "Haa Win" Print "Haa:", List (HAA)Print "Ontables" Print "Tab:", List (table)if __name__=="__main__": Henhenhaahaa ()#the results of my own hands-on simulation are not the same as in the book . #I don't think I have a problem with my own answer. #The result of a single-step simulation is the same.
Python data structures and algorithms-kitty fishing (using queues)