简体   繁体   English

字谜游戏的娱乐

[英]Recreation of a wordpuzzle game

I am trying to code a wordpuzzle game, this is a snippet of the code I am stuck on, I feel like I have tried everything.我正在尝试编写一个字谜游戏,这是我坚持的代码片段,我觉得我已经尝试了一切。

answers are a list of the words that are available to be found in the puzzle and their coordinates and the directions they are in. The answers present here are a snippet for testing purposes.答案是可在拼图中找到的单词列表及其坐标和所在方向。此处提供的答案是用于测试目的的片段。 What seems to happen is that program will find a word and say "this is working", but at the same time say "it is not a word", i dont know how to pass this.似乎发生的事情是程序会找到一个词并说“这正在工作”,但同时说“这不是一个词”,我不知道如何通过这个。

The program prints the available answers after each round for testing purposes.该程序在每一轮之后打印可用的答案以用于测试目的。

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

After considering the discussion in the comments and making a few changes of my own, here's the code I came up with:在考虑了评论中的讨论并自己进行了一些更改之后,这是我想出的代码:

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)

And here's an example game:这是一个示例游戏:

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!

Why the player_index shenanigans, instead of just looping over the players?为什么是player_index恶作剧,而不仅仅是循环播放器? Because otherwise, after Tom had guessed the last word, it still would've prompted Jerry for a guess, because the loop over the players wouldn't have ended yet.因为不然的话,在汤姆猜到最后一个字之后,它仍然会提示杰瑞猜测,因为玩家的循环还没有结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM