Created
March 23, 2012 14:42
-
-
Save apirak/2171293 to your computer and use it in GitHub Desktop.
Hangman game with simple class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'hangman_class' | |
hangman = Hangman.new | |
hangman.start | |
until hangman.finish | |
puts hangman.answer | |
print "#{hangman.limit} Enter your best guess: " | |
hangman.guess(gets) | |
end | |
hangman.say_good_bye |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hangman | |
attr_reader :finish, :answer, :limit | |
def initialize | |
@words = %w{spiderman superman powerpuf hangman} | |
@finish = false | |
@limit = 8 | |
@win = false | |
end | |
def start | |
@word = @words[rand(@words.length-1)] | |
@answer = "_" * @word.length | |
end | |
def guess(user_answer) | |
@limit -= 1 | |
user_answer = user_answer[0..-2] | |
if user_answer == 'exit' || limit == 0 | |
@finish = true | |
return | |
end | |
if user_answer.length == 1 | |
index = 0 | |
@word.each_char do |w| | |
if w == user_answer | |
@answer[index] = user_answer | |
end | |
index += 1 | |
end | |
end | |
if @answer == @word || user_answer == @word | |
@finish = true | |
@win = true | |
end | |
end | |
def say_good_bye | |
if @win | |
puts "You are the winner" | |
else | |
puts "Bye loser, the answer is #{@word}" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'hangman_class' | |
require 'test/unit' | |
class TestHangman < Test::Unit::TestCase | |
def test_startup_value | |
Hangman.class_eval do | |
attr_accessor :words, :finish, :limit, :win | |
end | |
hangman = Hangman.new | |
assert(hangman.words.length > 0, "should have more than one in dictionary") | |
assert_equal(hangman.finish, false) | |
assert_equal(hangman.limit, 8) | |
assert_equal(hangman.win, false) | |
end | |
def test_exit | |
Hangman.class_eval do | |
attr_accessor :word | |
end | |
hangman = Hangman.new | |
hangman.guess("exit") | |
assert(hangman.finish, "should exit when user type exit") | |
end | |
def test_find_one_char | |
Hangman.class_eval do | |
attr_accessor :word, :answer | |
end | |
hangman = Hangman.new | |
hangman.start | |
hangman.word = "powerpuf" | |
hangman.answer = "________" | |
hangman.guess("x\n") | |
assert_equal("________", hangman.answer) | |
hangman.guess("o\n") | |
assert_equal("_o______", hangman.answer) | |
hangman.guess("p\n") | |
assert_equal("po___p__", hangman.answer) | |
end | |
def test_quick_answer | |
Hangman.class_eval do | |
attr_accessor :word, :answer | |
end | |
hangman = Hangman.new | |
hangman.start | |
hangman.word = "powerpuf" | |
hangman.answer = "________" | |
hangman.guess("powerpuf\n") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment