【问题标题】:Recreation of a wordpuzzle game字谜游戏的娱乐
【发布时间】:2021-08-09 22:13:56
【问题描述】:

我正在尝试编写一个文字拼图游戏,这是我卡住的代码的 sn-p,我觉得我已经尝试了所有方法。

答案是可在拼图中找到的单词列表及其坐标和所在方向。此处提供的答案是用于测试目的的 sn-p。似乎发生的情况是程序会找到一个词并说“这正在工作”,但同时说“它不是一个词”,我不知道如何通过这个。

程序会在每一轮之后打印可用的答案以供测试。

answers=[(2, 12, 'up', 'reduce'), (11, 0, 'left', 'cherimolla'), (7, 12, 'up', 'leopardbane'), (9, 12, 'upleft', 'bionomical')]
guess = []
move = False

players=[]
players.append("Stephan")

 while True:
    print(answers)
    for i in players:
      guess=input("Player " + str(i) + "'s name: make a guess:")
      guesses=guess.split(",")   
      for i in range(0,len(answers)-1):
        ans = answers[i]     
        if int(ans[0]) == int(guesses[0]) and int(ans[1])==int(guesses[1]) and guesses[2] in ans[2]:
          answers.remove(ans)
          
          print("this is working")
        if (int(ans[0]) != int(guesses[0]) and int(ans[1])!=int(guesses[1]) and ans[2]!=guesses[2] and i==len(answers)-1):
          print("sorry this is not a word")

【问题讨论】:

  • 你为什么提到range(0,len(answers)-1)? range(0,n) 返回 0 到 n-1。我想你可以使用range(0,len)
  • 使用else 代替第二条if 语句。或者,您可以将第二个if 中的所有ands 替换为ors(德摩根定律:a and b 的反面是not a or not b,而不是not a and not b
  • 另外,在循环遍历列表时从列表中删除是一个非常糟糕的主意。在这种情况下,这无关紧要(因为最多有一个答案应该与猜测相符),但一般来说,在修改列表时,循环遍历它的副本。
  • @ram 我添加了 -1,因为它遇到了 listindex out of bounds 的错误
  • @koorkevani 谢谢我这样做了,它仍然打印出一个词,而不是多次,我只希望它打印一次,这就是我添加 i==len(answers)-1 的原因

标签: python for-loop if-statement while-loop boolean


【解决方案1】:

在考虑了 cmets 中的讨论并进行了一些我自己的更改之后,这是我想出的代码:

answers = [
    (2, 12, 'up', 'reduce'),
    (11, 0, 'left', 'cherimolla'),
    (7, 12, 'up', 'leopardbane'),
    (9, 12, 'upleft', 'bionomical')]

players = ["Tom", "Jerry"]

# Index of the current player in the players list
player_index = 0

# While not all answers were guessed
while len(answers) > 0:
    # Ask for a guess
    player_name = players[player_index]
    guess_raw = input(player_name + ", make a guess (x,y,direction,word): ")
    guess = guess_raw.split(",")

    # Transform the guess into a tuple of the same format as the answers
    guess_tuple = (int(guess[0]), int(guess[1]), guess[2], guess[3])

    # Check if the guess is correct
    if guess_tuple in answers:
        print("Success!")
        answers.remove(guess_tuple)
    else:
        print("Failure")

    # Go to the next player
    player_index = (player_index + 1) % len(players)

这是一个示例游戏:

Tom, make a guess (x,y,direction,word): 2,12,up,reduce
Success!
Jerry, make a guess (x,y,direction,word): 2,12,up,reduce
Failure
Tom, make a guess (x,y,direction,word): 11,0,left,cherimolla
Success!
Jerry, make a guess (x,y,direction,word): 7,12,up,leopardbane
Success!
Tom, make a guess (x,y,direction,word): 9,12,upleft,bionomical
Success!

为什么是player_index 恶作剧,而不是循环播放玩家?因为不然的话,在汤姆猜到最后一个字之后,它仍然会提示杰瑞猜测,因为玩家的循环还没有结束。

【讨论】:

  • 谢谢!玩家不猜单词,只猜方向和坐标,然后查看列表中的单词。
猜你喜欢
  • 2014-02-16
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-23
相关资源
最近更新 更多