lib/cinch/plugins/hangman.rb in cinch_hangman-0.0.2 vs lib/cinch/plugins/hangman.rb in cinch_hangman-0.0.3
- old
+ new
@@ -1,58 +1,18 @@
module Cinch::Plugins
class Hangman
include Cinch::Plugin
- match /hang guess (.*)/i, :method => :guess
- match /hang new (.*) (.*)/i, :method => :new_game
- match /hang status/i, :method => :status
+ match /hang (.*)/i, :method => :guess
+ match /hang new (#\S*) (.*)/i, :method => :new_game
def new_game(m, channel, answer)
- @game = Game.new(answer)
+ @game = CinchHangman::Game.new(answer)
Channel(channel).send(@game.describe)
end
def guess(m, guess)
@game.guess(guess)
m.reply @game.describe
end
def status(m)
m.reply @game.describe
end
- end
- class Game
- def initialize(answer, max_guesses = 6)
- @answer = answer
- @max_guesses = max_guesses
- @correct_chars = Set.new
- @incorrect_guesses = []
- end
- def guess(guess)
- if @answer.include?(guess)
- @correct_chars.merge(guess.chars)
- else
- @incorrect_guesses << guess
- end
- end
- def describe
- if @correct_chars.empty? && @incorrect_guesses.empty?
- "(#{hint}) a new hangman riddle has started."
- elsif won
- "(#{hint}) that hangman riddle was solved, awesome!"
- elsif lost
- "(#{hint}) that hangman riddle was just too difficult, that sucks!"
- else
- "(#{hint}) #{guesses_left} guesses left on the current hangman riddle."
- end
- end
- def won
- @correct_chars.superset?(Set.new(@answer.chars))
- end
- def lost
- guesses_left == 0
- end
- def guesses_left
- @max_guesses - @incorrect_guesses.size
- end
- def hint
- @answer.chars.map { |char| @correct_chars.include?(char) ? char : "_" }.join(" ")
- end
-
end
end