# # game of war # # Matt Bishop, ECS 36A Winter 2019 # import cards import sys # # print a hand # def prhand(s, d): # print the introduction no matter what print(s, end=' ') # if there's nothing in the hand, end the line # otherwise, print the hand if len(d) == 0: print("") else: ld = len(d) for i in range(ld-1): print(d[i], end=' ') print(d[ld-1]) # # does the user want to end it all? # we end on EOF and ignore anything else # returns True to exit, False to continue # def enditall(): # give user a chance to enter something try: # any char they type is irrelevant input("(EOF to exit) >>") except EOFError: # EOF -- exit return True except Exception as e: # oops -- whatever happened, we're done print(e) return True # keep going! return False # # this plays the game # we use 5 lists: # myhand: list of cards in my hand this round # yourhand: list of cards in your hand this round # tmyhand: list of cards I won this round # tyourhand: list of cards you won this round # hold: list of cards that are equal during play # at the end of each round, cards from txhand become # cards in xhand; hold is not cleared until next win # def main(): # # create and shuffle the deck # d = cards.Deck() d.shuffle() # # now deal and announce the hands # we emulate a real deal, one card # to each person, alternating # myhand = [ ] yourhand = [ ] count = 0; while(count < 52): if count % 2 == 0: myhand.append(d.cards[count]) else: yourhand.append(d.cards[count]) count += 1 prhand(" My hand:", myhand) prhand(" Your hand:", yourhand) # # initialize the temp arrays # hold: cards go here when they are equal # tmyhand: cards go here when I win them # tyourhand: cards go here when you win them # hold = [ ] tmyhand = [ ] tyourhand = [ ] # # game continues until someone has no cards left # while len(myhand) != 0 and len(yourhand) != 0: # # play a round # while len(myhand) > 0 and len(yourhand) > 0: # each lay one card; see who wins (or if # it's a push, ie., cards are equal) if myhand[0].rcmp(yourhand[0]) > 0: # I win; move cards to tmyhand # and delete from both hands # note we also add any held cards tmyhand.append(myhand.pop(0)); tmyhand.append(yourhand.pop(0)); tmyhand.extend(hold) hold.clear() elif myhand[0].rcmp(yourhand[0]) < 0: # I win; move cards to tmyhand # and delete from both hands # note we also add any held cards tyourhand.append(myhand.pop(0)); tyourhand.append(yourhand.pop(0)); tyourhand.extend(hold) hold.clear() else: # neither; put both cards on hold hold.append(myhand.pop(0)); hold.append(yourhand.pop(0)); # # now print the state of the hands # print('----------------------------') prhand(" My hand:", myhand) prhand("Won so far:", tmyhand) print('----------------------------') prhand(" Your hand:", yourhand) prhand("Won so far:", tyourhand) # we only print hold if there are cards there if hold: print('----------------------------') prhand("Equals:", hold) # see if the user wants to end the game if enditall(): print(" I have %2d cards" % (len(myhand) + len(tmyhand))) print("You have %2d cards" % (len(yourhand) + len(tyourhand))) return # # end of the round # move what was won into the hand # myhand.extend(tmyhand) tmyhand.clear() yourhand.extend(tyourhand) tyourhand.clear() # # end of the game; say who won # if len(myhand) > 0: print("I win!") else: print("You win!") # # you're off! # main()