【问题标题】:Finding letters that are near, exact or not in a user input string查找用户输入字符串中接近、准确或不准确的字母
【发布时间】:2017-11-14 12:22:07
【问题描述】:

我目前正在为儿童开发一个小的修改版 Hangman in Rails。游戏首先从文本文件中随机生成一个单词,用户必须通过输入一个四个字母的单词来猜测该单词。每个单词由每个字符分割,例如“r”、“e”、“a”、“l”,并返回一条消息,说明它们对单词的影响。

随机生成的单词是“真实的”

输入 rlax

输出 Correct, Close, Correct, Incorrect

我尝试了其他一些我在网上找到但没有奏效的方法,而且我对 Ruby 和 Rails 还很陌生。希望有人可以指导我正确的方向。

这是一些代码

def letterCheck(lookAtLetter)   
  lookAHead = lookAtLetter =~ /[[:alpha:]]/ 
end 

def displayWord         
  $ranWordBool.each_index do |i|        
      if($ranWordBool[i])           
          print $ranWordArray[i]        
          $isWin += 1                   
      else
          print "_"     
      end               
  end           
end 

def gameLoop            
  turns = 10            
  turnsLeft = 0     
  lettersUsed = []  
  while(turnsLeft < turns)      
     $isWin = 0                 
     displayWord                    

     if($isWin == $ranWordBool.length)          
       system "cls"                         
       puts "1: Quit"                           
       puts "The word is #{$ranWord} and You Win"   
       puts "Press any key to continue"     
       return               
     end            

     print "\n" + "Words Used: "            
     lettersUsed.each_index do |looper|     
     print " #{lettersUsed[looper]} "   
     end                    
     puts "\n" + "Turns left: #{turns - turnsLeft}" 

     puts "Enter a word"        
     input = gets.chomp         
     system "cls"   

     if(input.length != 4)  
       puts "Please enter 4 lettered word"
     elsif(letterCheck(input))  
       if(lettersUsed.include?(input))  
         puts "#{input} already choosen"    
         elsif($ranWordArray.include?(input))
           puts "Close"
           $ranWordArray.each_index do |i|  
             if(input == $ranWordArray[i])
               $ranWordBool[i] = true   
             end    
             if($ranWordBool[i] = true)
               puts "Correct"
             else
               puts "Incorrect"
             end
           end          
          else
            lettersUsed << input        
            turnsLeft += 1          
          end                   
          else
            puts "Not a letter"     
          end                       
        end                         
        puts "You lose"             
        puts "The word was #{$ranWord}"     
        puts "Press any key to continue"    
      end           

      words = []            
      File.foreach('words.txt') do |line|       
      words << line.chomp               
      end           

      while(true)
        $ranWord = words[rand(words.length) + 1]    
        $ranWordArray = $ranWord.chars          
        $ranWordBool = []                           
        $ranWordArray.each_index do |i|             
        $ranWordBool[i] = false         
      end           

      system "cls"          
      gameLoop
      input = gets.chomp
      shouldQuit(input) 
    end 

【问题讨论】:

  • github.com/GlobalNamesArchitecture/damerau-levenshtein 和/或 Google 的“Damerau-Levenstein 距离”和“Jaro-Winkler 距离”。
  • @max 正如我所说的,我还很新,但我会进一步研究
  • 你正在寻找的是类似于 levenshtein 距离的东西,它给出了两个字符串之间的相似度。
  • 不完全。我想根据他们与猜测正确随机词的距离来返回反馈。我不想使用任何类型的 API 只是标准 Ruby 功能@max

标签: ruby-on-rails ruby rubygems


【解决方案1】:

类似的东西:

# Picking random word to guess
word = ['open', 'real', 'hang', 'mice'].sample

loop do
  puts "So, guess the word:"

  input_word = gets.strip
  if word == input_word
    puts("You are right, the word is: #{input_word}")
    break
  end

  puts "You typed: #{input_word}"

  # Split both the word to guess and the suggested word into array of letters
  word_in_letters  = word.split('')
  input_in_letters = input_word.split('')

  result = []
  # Iterate over each letter in the word to guess
  word_in_letters.each_with_index do |letter, index|
    # Pick the corresponding letter in the entered word
    letter_from_input = input_in_letters[index]

    if letter == letter_from_input
      result << "#{letter_from_input} - Correct"
      next
    end

    # Take nearby letters by nearby indexes
    # `reject` is here to skip negative indexes
    # ie: letter 'i' in a word "mice"
    #   this will return 'm' and 'c'
    # ie: letter 'm' in a word "mice"
    #   this will return 'i'
    letters_around =
      [index - 1, index + 1]
      .reject { |i| i < 0 }
      .map { |i| word_in_letters[i] }
    if letters_around.include?(letter_from_input)
      result << "#{letter_from_input} - Close"
      next
    end

    result << "#{letter_from_input} - Incorrect"
  end

  puts result.join("\n")
end

【讨论】:

  • 是的,但关闭不太有效@nattfodd。例如,如果这个词是 hang 并且我输入了 ngha,那么我会接近,接近,接近,接近
  • 所以请具体说明 - 您期望从“关闭”消息到这封信的行为是什么?
猜你喜欢
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多