简体   繁体   English

Python中的字母猜谜游戏

[英]Letter guessing game in python

I'm attempting to code a basic letter game in python. 我正在尝试用python编写基本的字母游戏。 In the game, the computer moderator picks a word out of a list of possible words. 在游戏中,计算机主持人从可能的单词列表中选择一个单词。 Each player (computer AI and human) is shown a series of blanks, one for each letter of the word. 每个玩家(计算机AI和人类)都显示一系列空白,每个单词字母一个。 Each player then guesses a letter and a position, and are told one of the following: 然后,每个玩家猜出一个字母和一个位置,并被告知以下其中一项:

1) That letter belongs in that position (the best outcome) 1)那封信属于那个位置(最好的结果)

2) That letter is in the word, but not in that position 2)该字母在单词中,但不在该位置

3) That letter is not in any of the remaining blank spaces 3)该字母不在任何剩余的空格中

When the word has been fully revealed, the player to guess the most letters correctly wins a point. 当单词完全显示出来时,猜猜最多字母的玩家将赢得一点。 The computer moderator picks another word and starts again. 计算机主持人选择另一个单词,然后重新开始。 The first player to five points wins the game. 第一个获得5分的玩家赢得比赛。 In the basic game, both players share the same set of blanks they're filling in, so the players benefit from each other's work. 在基本游戏中,两个玩家共享相同的空白集,因此他们将从彼此的工作中受益。

My Question--- 我的问题 - -

I've got the random word coming in just fine. 我有一个随机的单词就好了。 Now, I'd like to count the number of characters in the word so I know how many spaces to indicate. 现在,我想计算单词中的字符数,以便知道要显示的空格数。 How would I go about this? 我将如何处理?

Once that's working properly, I assume it won't be too hard to compare the player/AI guesses with the word and proceed accordingly. 一旦工作正常,我认为将玩家/ AI的猜测与单词进行比较并进行相应的操作就不会太难。

Thanks! 谢谢!

Number of characters in the word: len() function 单词中的字符数: len()函数

In [15]: len('jabberwocky')
Out[15]: 11

An example of a mask: 遮罩的示例:

In [16]: mask = ' '.join(('_' for i in range(len('jabberwocky'))))

In [18]: mask
Out[18]: '_ _ _ _ _ _ _ _ _ _ _'
#         j a b b e r w o c k y

References on everything involved in the mask example: 关于遮罩示例中涉及的所有内容的参考:

  1. str.join() method. str.join()方法。
  2. range() built-in function. range()内置函数。
  3. generator expressions (used to generate N underscores). 生成器表达式 (用于生成N个下划线)。

All in all, what it does is: 总而言之,它的作用是:

  1. Count the characters in the word 'jabberwocky' 计算单词“ jabberwocky”中的字符
  2. Generate n underscores 产生n个下划线
  3. Join them by a space. 通过空格加入他们。

On a cursory analysis of your project, I believe you can find all the answers you need in the Python documentation. 通过对项目的粗略分析,我相信您可以在Python文档中找到所需的所有答案。 Addressing your first question, see here: 解决您的第一个问题,请参见此处:

http://docs.python.org/3.2/library/functions.html#len http://docs.python.org/3.2/library/functions.html#len

Addressing future questions that you may have as you need to deal with strings, see here: http://docs.python.org/3.2/tutorial/introduction.html#strings 解决您将来在处理字符串时可能遇到的问题,请参见此处: http : //docs.python.org/3.2/tutorial/introduction.html#strings

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

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