【问题标题】:Python programming for card game War纸牌游戏 War 的 Python 编程
【发布时间】:2015-06-28 14:32:13
【问题描述】:

这个程序应该玩纸牌游戏战争。好的,所以我需要能够提示用户继续玩游戏并让他们通过按 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()

【问题讨论】:

  • 为什么会运行?你不会在任何地方调用它。

标签: python python-3.x war


【解决方案1】:

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

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

作为次要注意事项,您需要了解如何隔离代码中的问题。为此:以下显示了您遇到的 enter to continue 问题的可测试实例(和解决方案)。运行它,它只会测试(在这种情况下解决)输入问题,这就是 Stack Overflow 的 minimal, complete, verifiable example 的意思。

【讨论】:

    【解决方案2】:

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

    此外,您在 Keep_playing() 中有一个无限循环。调用 main() 后,将 input(...) 部分放入 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 了解如何实现将“enter”键作为输入读取(这很简单,所以不用担心)

    此外,作为 Python(和大多数其他语言)中的一种良好做法,将变量 函数名称设为小写,因为大写名称通常保留给类使用。即使这不会被传播或维持,但养成这个习惯是件好事。

    【讨论】:

    • 您的代码有一些错误:一个总是会退出(由于 string 1 不等于 int 1); 2 它会在回车时退出,因为您的 while 循环检查值不正确(输入去掉 \n 所以 Enter 不应该是 1
    • 我更多地关注输入/循环在问题中的位置,而不是解析输入的正确方法,但你是对的。我已经编辑了我的答案以执行(我相信)所需的功能。但是,我相信您的回答就“正确”的代码结构而言更为充分。
    猜你喜欢
    • 2013-02-27
    • 2013-04-17
    • 1970-01-01
    • 2013-03-01
    • 2021-04-18
    • 1970-01-01
    • 2018-09-11
    • 2012-02-29
    • 2017-12-17
    相关资源
    最近更新 更多