红宝石猜词游戏



我是Ruby的新手,并且使用这个刽子手风格的猜词游戏。我有两个主要问题。这是我现在正在使用的内容:

class Word_game
def initialize(word)
@word = word.downcase 
@display_word = "_ " * word.length
end
def guess_the_word(word_guess)
word_guess.downcase 
@word.split("").each_with_index do |word_letter, index|
if word_guess == word_letter
@display_word[index] = word_guess 
p @display_word
puts "You're getting somewhere! Keep trying!"
end 
end
if !@word.include? (word_guess)
puts "Nope, guess again..."
end

def win? 
if @word == @display_word
puts "Congratulations you won!!! You are the word master!!!"
true
else
false
end
end 
def lose?
if @attempts == 0 
puts "You lose!!"
true 
end 
end 
puts "Welcome to the Word Guessing Game! Let's see if YOU have what it TAKES!!!"
puts "This is a 2 player game. "
puts "Player 1... please enter a word for Player 2 to guess!"
puts ">>"
game_word = gets.chomp 
game = Word_game.new(game_word)
attempts = 0
guessed_letters = []
until @attempts == game_word.length 
puts "Ok Player 2, Guess a letter! GO!!!"
letter_guess = gets.chomp
if guessed_letters.include? letter_guess
puts "You already guessed that letter! Enter a new one."
letter_guess = gets.chomp
end
guessed_letters << letter_guess
game.guess_the_word(letter_guess)
if game.win?
attempts += 1 
else game.lose? 
end
end
end 
  • 首先,如果单词是hello,则单词进度应如下所示:

    h _ e _ _ o   
    
  • 取而代之的是,空格不在正确的位置,看起来像这样(运行我的代码的实际结果):

.

Ok Player 2, Guess a letter! GO!!!
h
"h _ _ _ _ "
You're getting somewhere! Keep trying!
Ok Player 2, Guess a letter! GO!!!
o
"h _ o _ _ "
You're getting somewhere! Keep trying!
Ok Player 2, Guess a letter! GO!!!
e
"he_ o _ _ "
You're getting somewhere! Keep trying!
Ok Player 2, Guess a letter! GO!!!
l
"hel o _ _ "
You're getting somewhere! Keep trying!
"hello _ _ "

当用户猜到这个词时,它不会把我的"恭喜"语句并结束游戏。

我也坚持我的"输"方法。我不确定如何修复该方法,以便在用户用完尝试次数并打印"lose"语句时游戏结束。

感谢您的帮助!

我认为你使输出太复杂了。我会在一个数组中跟踪单词和猜测。而不是display_word变量,我会让它成为一个方法,可能是"to_s">

顺便说一下,Ruby 约定是使用 CamelCase 类名。

class WordGame
def initialize(word)
@word = word.downcase.chars
@guesses = ["_"] * @word.size
end
def to_s
@guesses.join " "
end

这应该可以解决您的间距问题。这也将简化猜测。

此外,检查您是否已经使用了该字母可能应该由 WordGame 类处理。

对于您的第一个问题,您的@display_word按如下方式开始:

[0] = '_' # For h
[1] = ' '
[2] = '_' # For e
[3] = ' '
...

例如,当您猜测'e'时,您会:

@display_word[index] = word_guess

其中index等于 1,"hello"中的第二个字符,因此您可以看到它不会写入@display_word中的'e'索引。

对于您的第二个问题,有多种方法可以解决它。例如,我会做一些事情,比如使用@attempts_remaining从值 10 左右开始,然后使用现有代码:

if !@word.include? (word_guess)
@attempts_remaining -= 1  # Count failure to guess
puts "Nope, guess again..."
end

然后:

def win? 
# If you've guessed all the letters, there's no '_' left in the display word
if !@display_word.include? ('_')
puts "Congratulations you won!!! You are the word master!!!"
true
else
false
end
end 
def lose?
if @attempts_remaining == 0 
puts "You lose!!"
true 
end 
end 

最后,调整until回路终止条件:

until game.win? or game.lose?

可以删除对win?lose?的现有调用。

(单词缺失游戏)

把" " 把">总回合" 把" " 把"=>[第一轮,第二轮,第三轮第四轮]" 把" " 把 "=> [总共 5 次尝试]" 把" " 一=" 二=" 三=" 四=" 把" " 把" --第一轮按-- => (1)" 一=gets.to_i 把 '======================' 把"问题:=> ( K ?N G )" 把 " c=5 5.倍做 字符串 1 = 'i' 字符串 ="我" 把 "答案:=> 尝试 NO:#{c}" string2 = gets.chomp if (字符串 1==字符串 2) 把">好工作正确保存" 破 Elsif (stringone == string2 ) 把">好工作正确保存" 破 还 把"-Worng spaling-" 结束 c -=1 结束 把">四舍五入" 如果 C<1 把">尝试结束游戏" 退出 结束 把 '======================' 把 "--第二轮按-- => (2)" 二=gets.to_i 把 '======================' 把"问题:=> (P L ?Y )" 5.倍做 字符串 1 = 'a' 字符串 = "A" 把 "答案:=> 尝试 NO:#{c}" string2 = gets.chomp if (字符串 1==字符串 2) 把">好工作正确保存" 破 Elsif (stringone==string2) 把">好工作正确保存" 破 还 把"-Worng spaling-" 结束 c -=1 如果 C<1 把">尝试结束游戏" 退出 结束 结束 把">四舍五入">

puts '==================='
puts "--ROUND Three press-- => (3)"
three=gets.to_i
puts '==================='
puts "Question:=>  ( S P ? T )"
5.times do
string1 = 'o' 
stringone= 'O'
puts "Answer:=>  Try NO:#{c}*"
string2 = gets.chomp
if (string1==string2)
puts "_Good Work correct spaling_"
break
elsif (stringone == string2)
puts "_Good Work correct spaling_"
break
else
puts "-Worng spaling-"
end
c -=1
if c<1
puts " *Tries Over Game End* "
exit
end
end
puts "   *Round Over*  "
puts '==================='
puts "--ROUND Four press-- => (4)"
four=gets.to_i
puts '==================='
puts "Question:=>  ( G ? M E )"
5.times do
string1 = 'a' 
stringone = "A"
puts "Answer:=>  Try NO:#{c}*"
string2 = gets.chomp
if (string1==string2)
puts "_Good Work correct spaling_"
break
elsif (stringone == string2)
puts "_Good Work correct spaling_"
break
else
puts "-Worng spaling-"
end
c -=1
if c<1
puts " *Tries Over Game End* "
exit
end
end
puts "**Yahoo Congragualtion complete All Round**"

最新更新