Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/game.rb

Instance Method Summary collapse

Constructor Details

#initialize(words) ⇒ Game

Returns a new instance of Game.



8
9
10
11
12
# File 'lib/game.rb', line 8

def initialize words
  @words = words
  @results = []
  @turn = 0
end

Instance Method Details

#display_definition(word) ⇒ Object



60
61
62
63
# File 'lib/game.rb', line 60

def display_definition word
  definition = fetch_definition(word)
  puts "\n#{word.blue} means: #{definition}" if !definition.empty?
end

#end_gameObject



81
82
83
84
85
86
# File 'lib/game.rb', line 81

def end_game
  puts
  puts
  puts "#{"Congratulations!".green} Here is a star for you: #{'*'.yellow}"
  puts
end

#fetch_definition(word) ⇒ Object



55
56
57
58
# File 'lib/game.rb', line 55

def fetch_definition word
  definitions = `dict "#{word}" 2>/dev/null | grep '     ' | head -2`.chomp.gsub("     ","").split(/[\r\n]/)
  definitions.uniq.join(" -- ")
end

#get_an_answer_for(correct_answer) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/game.rb', line 38

def get_an_answer_for correct_answer
  STDOUT.write("> ")
  answer = STDIN.gets.chomp

  while answer != '?' and proximity(answer, correct_answer) > 1
    @results << 0
    display_definition(answer)
    puts
    puts "Nope. Try again.".red
    puts
    STDOUT.write("> ")
    answer = STDIN.gets.chomp
  end

  answer
end

#hit_rateObject



20
21
22
23
24
25
26
27
28
# File 'lib/game.rb', line 20

def hit_rate
  number_of_turns_we_remember = [@words.size, @turn].min
  recent_results = number_of_turns_we_remember < @results.size ? @results[@results.size - number_of_turns_we_remember, @results.size] : @results
  hits = recent_results.inject(0){|sum, value| sum+=value; sum}
  misses = recent_results.size - hits

  rate = 100 - misses / number_of_turns_we_remember.to_f * 100
  rate >= 0 ? rate : 0
end

#pick_choices_for(word) ⇒ Object



14
15
16
17
18
# File 'lib/game.rb', line 14

def pick_choices_for word
  others = @words.reject{|w| w == word}
  choices = (others.shuffle[0,4] + [word])
  choices.shuffle
end

#play(&block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/game.rb', line 107

def play &block
   (puts "Could not find any words to play with."; return) if @words.empty?

  @words.shuffle.each{|word|
    @turn += 1

    sentence, correct_answer = yield word
    (puts "Could not find anything to play with for word #{word}."; next) if sentence.nil? or correct_answer.nil?

    show_question_for(correct_answer, sentence)
    answer = get_an_answer_for(correct_answer)
    respond_to_answer(answer, correct_answer)

    (end_game; return) if @turn >= @words.size and (hit_rate >= 95)
  }

  play(&block)
end

#proximity(one, other) ⇒ Object



34
35
36
# File 'lib/game.rb', line 34

def proximity one, other
  Amatch::Levenshtein.new(romanize(one).downcase).match(romanize(other).downcase)
end

#respond_to_answer(answer, correct_answer) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/game.rb', line 88

def respond_to_answer answer, correct_answer
  puts
  if answer == '?'
    @results << 0
    puts "The answer was: #{correct_answer.red}."
    display_definition(correct_answer)
    sleep 2
  elsif proximity(answer, correct_answer) > 0
    @results << 1
    puts "Sort of... the answer was: #{correct_answer.yellow}."
    display_definition(correct_answer)
    sleep 1
  else
    @results << 1
    puts "Correct! The answer was: #{correct_answer.green}."
    display_definition(correct_answer)
  end
end

#romanize(w) ⇒ Object



30
31
32
# File 'lib/game.rb', line 30

def romanize w
  w.gsub("ä", "ae").gsub("ö", "oe").gsub("ü", "ue").gsub("ß", "ss")
end

#show_question_for(word, sentence) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/game.rb', line 65

def show_question_for word, sentence
  puts
  puts
  puts
  puts "Turn #{@turn}".blue
  puts "#{@words.size} words".blue
  color = hit_rate < 75 ? :red : (hit_rate < 90 ? :yellow : 'green')
  puts "Hit rate: #{'%i' % hit_rate}%".send(color)
  puts "------------------------------------------------------------".blue
  puts
  puts ((sentence =~ /\b#{Regexp.escape(word)}\b/i) != nil ? sentence.gsub(/\b#{Regexp.escape(word)}\b/i, "______") : sentence.gsub(word, "______")).strip
  puts 
  puts "Choices: [" + " #{pick_choices_for(word).join(" - ")} ".blue + "]"
  puts
end