用于纸牌游戏战争的Python编程

时间:2015-06-27 20:21:10

标签: python python-3.x war

这个程序应该玩纸牌游戏War。好的,所以我需要能够提示用户继续播放并通过按Enter键让他们响应。我不确定如何实现这一目标。帮助

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()

2 个答案:

答案 0 :(得分:0)

正如@IanAuld所提到的,你没有在任何地方调用你的Keep_playing()函数。而不是调用main(),只需调用Keep_playing()。如果您只是在没有“按Enter继续播放”的情况下启动游戏,只需将Keep_playing()函数的第一行调用main(),以便它运行然后继续检查用户是否需要继续。

此外,您在Keep_playing()中有一个无限循环。调用main()后,将输入(...)部分放在while循环中。这样,它将重新启动每个循环并更改still_playing变量。

它应该是这样的:

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=""
    Enter=""
    while still_playing==Enter:
        main()
        still_playing=input('Press "Enter" to continue playing')

keep_playing()

您可以阅读this SO question以了解如何实现阅读“输入”键作为输入(这很简单,所以不要担心)

此外,作为Python(和大多数其他语言)的良好实践,将变量函数名称设置为小写,因为大写名称通常保留供类使用。即使这不会被传播或维持下去,养成这种习惯也是好事。

答案 1 :(得分:0)

假设您的main的其余部分正在工作(以及导入的Deck类):您的两个主要问题是您忘记调用main以及用于测试变量Enter的方法。由于您为输入输入输入,您只需要针对空字符串进行测试,或者只测试它是否为空 - 因为默认情况下输入会删除换行符。

def play_hand():
    print ("all the play code goes here")
    #just a simple exit here no return needed
    '''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=[] ... etc ...'''

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.'''
    return input('Press "Enter" to continue playing')

def main():
#    the_deck = Card.Deck() # create deck instance (depending on how shuffle method is written this may need to go in while loop)
    still_playing = "" #Set still_playing to empty string to more easily use input
    while not still_playing: #this will keep going as long as still_playing is an empty string
        print("hello main") # cause it fun :) - also as you can put an intro here or the shuffle so:
#        the_deck.shuffle() 
#        print( "===== shuffled deck =====" )
#        the_deck.display()

        play_hand()
        # Do all your other stuff here
        still_playing = keep_playing() #Calls the function and stores returned value (can do this with just input but using return to show an option)

main()

作为辅助说明,您需要了解如何在代码中隔离问题。为此:以下显示了输入以继续问题的可测试实例(和解决方案)。运行它,它只会测试(在这种情况下解决) 输入问题 ,这就是Stack Overflow对minimal, complete, verifiable example的意思。