我是Ruby的新手,正在创建一个刽子手游戏。到目前为止,我的代码将单词与正确的单词进行比较。但我希望它比较字母。所以基本上,如果secrect单词是胶水,用户输入G,它将是不正确的,但如果用户输入胶水,它将是正确的。我需要它像刽子手一样逐个字母比较。
这有点麻烦。我在下面附上了我的代码。
secret_word = []
puts "enter a word to be guessed"
secret_word = gets.chomp
guess_letters = []
guess = ""
guess_limit = 3
guess_count = 0
out_of_guesses = false
while guess != secret_word and !out_of_guesses
if guess_count < guess_limit
puts "enter your guess: "
guess = gets.chomp()
guess_letters << guess
guess_count +=1
puts "you have used these letters thus far #{guess_letters.join(", ")}"
else
out_of_guesses = true
end
end
if out_of_guesses
puts "you Lose, the word was #{secret_word}"
else
puts "you win"
end
我不确定您使用的是哪种刽子手规则,但这里有一个粗略的草案,允许三次失败的尝试并使用小写字符
def guess_word(word, tries)
if tries < 1
puts "You are hanged!"
elsif word.empty?
puts "You guessed it! You are saved from the gallows!"
else
print "Enter character: "
c = STDIN.getc.downcase
STDIN.getc # get rid of newline
if word.index(c).nil?
puts "Ooops, #{c} was wrong!"
guess_word(word, tries - 1)
else
puts "#{c} was correct!"
guess_word(word.sub(/["#{c}"]/, ''), tries)
end
end
end
if __FILE__ == $0
TRIES = 3
print "Enter word to guess: "
word = gets.chomp
guess_word(word.downcase, 3)
end
这是未经测试的。
游戏规则 刽子手在其维基上给出。我假设试图猜测这个词的玩家在绞刑架上的人的所有七个部分(头、脖子、左臂、身体、右臂、左腿、右腿(都被抽中时输了。
帮助程序方法
画出被绞死的人
首先创建一个可用于绘制部分或全部刽子手的哈希:
MAN = [" On", " |n", "\", "|", "/n", " |n/", " \"].
map.each_with_object([""]) { |s,arr| arr << (arr.last + s) }.
each.with_index.with_object({}) { |(s,i),h| h[i] = s }
关键是错误猜测的数量。例如:
puts MAN[2]
O
|
puts MAN[6]
O
|
|/
|
/
跟踪单词字母的位置
接下来创建一个哈希,其键是秘密单词的唯一字母,其值是单词中键位置的索引数组。
def construct_unknown(word)
word.each_char.with_index.with_object({}) { |(c,i),h| (h[c] ||= []) << i }
end
例如
unknown = construct_unknown("beetle")
#=> {"b"=>[0], "e"=>[1, 2, 5], "t"=>[3], "l"=>[4]}
我们还将为位置已知的字母创建一个空哈希:
known = {}
将猜测的字母从哈希unknown
移动到哈希known
如果猜到的字母是unknown
键,则该键和值将移动到known
。
def move_unknown_to_known(letter, unknown, known)
known.update(letter=>unknown[letter])
unknown.delete(letter)
end
例如(对于上面的unknown
和known
(,
move_unknown_to_known("e", unknown, known)
unknown #=> {"b"=>[0], "t"=>[3], "l"=>[4]}
known #=> {"e"=>[1, 2, 5]}
看看猜测者是赢了还是输了
在猜出一个字母后,我们将确定玩家何时赢了或输了,或者继续:
def win?(word_size, known)
known.values.flatten.sum == word_size
end
def lose?(wrong_guess_count)
wrong_guess_count == HANGMAN.size
end
例如
win?(word.size, known)
#=> false
lose?(6) #=> false
lose?(7) #=> true
显示已知字母
def display_known(word_size, known)
known.each_with_object('_' * word_size) { |(k,a),s| a.each { |i| s[i] = k } }
end
例如(召回word #=> "beetle"
(,
puts display_known(word.size, known)
_ee__e
主要方法
现在,我们已准备好编写 main 方法。
def hangman
puts "Player 2, please avert your eyes for a moment."
print "Player 1: enter a secret word with at least two letters: "
word = gets.chomp.downcase
unknown = construct_unknown(word)
known = {}
wrong_guess_count = 0
loop do
puts display_known(word.size, known)
puts MAN[wrong_guess_count] if wrong_guess_count > 0
if win?(word.size, known)
puts "You win! You win! Congratulations!"
break
end
if lose?(wrong_guess_count)
puts "Sorry, but you've run out of guesses"
break
end
print "Player 2: enter a letter or your guess of the word: "
guess = gets.chomp.downcase
if guess.size > 1
if guess == word
puts word
puts "You win! You win! Congratulations!"
break
else
puts "Sorry, that's not the word"
wrong_guess_count += 1
end
elsif unknown.key?(guess)
nbr = unknown[guess].size
puts nbr == 1 ? "There is 1 #{guess}" : "There are #{nbr} #{guess}'s"
move_unknown_to_known(guess, unknown, known)
else
puts "Sorry, the word contains no #{guess}'s"
wrong_guess_count += 1
end
end
end
例
在向两位选手和观众解释规则后,嘉宾主持人最后说:"别忘了,在猜测一个字母或单词时,它必须表达为一个问题......就一会。。。拿着...有人告诉我,没有必要把它作为一个问题来框定
"。假设单词是beetle
,字母猜测是't'
、'i'
、'a'
、'l'
、'r'
、's'
、't'
、'u'
、'e'
、'beetle'
。
hangman
Player 2, please avert your eyes for a moment.
Player 1: enter a secret word with at least two letters: beetle
______
Player 2: enter a letter or your guess of the word: t
There is 1 t
___t__
Player 2: enter a letter or your guess of the word: i
Sorry, the word contains no i's
___t__
O
Player 2: enter a letter or your guess of the word: a
Sorry, the word contains no a's
___t__
O
|
Player 2: enter a letter or your guess of the word: l
There is 1 l
___tl_
O
|
Player 2: enter a letter or your guess of the word: r
Sorry, the word contains no r's
___tl_
O
|
Player 2: enter a letter or your guess of the word: s
Sorry, the word contains no s's
___tl_
O
|
|
Player 2: enter a letter or your guess of the word: t
Sorry, the word contains no t's
___tl_
O
|
|/
Player 2: enter a letter or your guess of the word: u
Sorry, the word contains no u's
___tl_
O
|
|/
|
/
Player 2: enter a letter or your guess of the word: e
There are 3 e's
_eetle
O
|
|/
|
/
Player 2: enter a letter or your guess of the word: beetle
beetle
You win! You win! Congratulations!