[英]How to compare letters in two strings Ruby
我是 ruby 和創建劊子手游戲的新手。 到目前為止,我的代碼將單詞與正確的單詞進行了比較。 但我希望它比較字母。 所以基本上,如果秘密詞是glue,用戶輸入G它會不正確,但如果用戶輸入glue它會是正確的。 我需要它像劊子手一樣逐字比較。
對此有點麻煩。 我在下面附上了我的代碼。
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
這是未經測試的..
游戲劊子手的規則在其Wiki中給出。 我假設當絞刑架上的男人的所有七個部分都被繪制(頭部,頸部,左 arm,身體,右 arm,左腿,右腿)時,試圖猜測這個詞的玩家輸了。
輔助方法
畫出被吊死的人
首先創建一個 hash 可用於繪制部分或完整的劊子手:
MAN = [" O\n", " |\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
|
\|/
|
/
跟蹤單詞字母的位置
接下來創建一個 hash,其密鑰是秘密詞的唯一字母,其值是詞中密鑰位置索引的 arrays。
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]}
我們還將為位置已知的字母創建一個空的 hash:
known = {}
將猜測的字母從unknown
的 hash 移動到known
的 hash
如果猜測的字母是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!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.