samedi 27 juin 2015

Python programming for card game War

This program is supposed to play the card game War. Okay so I need to be able to prompt the user to keep playing and have them respond by hitting the enter key. I'm not sure how to make this happen. Help?

import cards

# Create a deck of cards
the_deck = cards.Deck()

# Shuffle the deck, then display it in 13 columns
the_deck.shuffle()
print( "===== shuffled deck =====" )
the_deck.display()


def main():
    '''This function deals out half the deck to each
    player. It sorts the rank so as to solve the problem
    with Aces causing an infinite game'''
    player1_list=[]
    player2_list=[]
    for i in range( 26 ):
        p1_temp= the_deck.deal()
        player1_list.append( p1_temp )
        p2_temp= the_deck.deal()
        if (p2_temp.rank()==1):
            player1_list.append(p2_temp)
            player2_list.append(player1_list.pop(0))
        else:
            player2_list.append(p2_temp)


    print()
    # Card dealt to Player #1
    player1_card = player1_list.pop( 0 )
    print( "===== player #1 =====" )
    print( "Card dealt to player #1:", player1_card )
    print( player1_list )
    print()
    #Card dealt to Player #2
    player2_card=player2_list.pop(0)
    print( "===== player #2 =====" )
    print("Card dealt to player #2:",player2_card)
    print( player2_list )

    # Compare the two cards using overloaded operators
    print()
    if player1_card == player2_card:
        print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
    elif player1_card > player2_card:
        print("Player #1 wins:",player1_card,"of higher rank than",player2_card)
    else:
        print("Player #2 wins:",player2_card,"of higher rank than",player1_card)
        print()
main()
def keep_playing():
    '''Determines whether the player wants to continue. If so they press the
    Enter key and the function calls main to start all over.'''
    still_playing=input('Press "Enter" to continue playing')
    Enter=1
    while still_playing==Enter:
        main()
keep_playing()

Aucun commentaire:

Enregistrer un commentaire