diff --git a/drm2-hangman/Gemfile b/drm2-hangman/Gemfile new file mode 100644 index 0000000..084f309 --- /dev/null +++ b/drm2-hangman/Gemfile @@ -0,0 +1 @@ +gem 'random-word' diff --git a/drm2-hangman/Gemfile.lock b/drm2-hangman/Gemfile.lock new file mode 100644 index 0000000..ffe4b40 --- /dev/null +++ b/drm2-hangman/Gemfile.lock @@ -0,0 +1,12 @@ +GEM + specs: + random-word (1.3.0) + +PLATFORMS + ruby + +DEPENDENCIES + random-word + +BUNDLED WITH + 1.13.6 diff --git a/drm2-hangman/lib/Game.rb b/drm2-hangman/lib/Game.rb new file mode 100644 index 0000000..c3fc984 --- /dev/null +++ b/drm2-hangman/lib/Game.rb @@ -0,0 +1,73 @@ +require_relative 'Screen' +require_relative 'WordFactory' + +class Game + def initialize + @guesses = [] + @misses = [] + @words = WordFactory.get_words + @screen = Screen.new + end + + def run + loop do + state = @screen.get_state @misses.count + lines = show_lines + + @screen.write "Game State: " + @screen.write state + + @screen.write "Word or Phrase: " + @screen.write lines + + unless lines.include? '_' + @screen.write "You Win!" + + quit_game + end + + if @misses.count == 6 + @screen.write "Game Over!" + @screen.write "The word or phrase was: #{@words.map{ |word| word.get_word }.join(' ')}" + + quit_game + end + + input = @screen.ask_for_input("What is your guess?").downcase + + response = check_guess input + + @screen.write response if response + end + end + + def show_lines + @words.map{ |word| + word.get_lines @guesses + }.join ' ' + end + + def check_guess guess + quit_game if guess == "quit" + + return "Invalid input!" unless input_is_valid? guess + + if @guesses.include? guess + return "You already guessed that letter!" + end + + @guesses.push guess + + @misses.push guess if !@words.map{ |word| word.get_word }.join.include? guess + + return nil + end + + def input_is_valid? input + return !!(input =~ /[a-z]/i) && (input.length == 1) + end + + def quit_game + exit + end +end diff --git a/drm2-hangman/lib/Screen.rb b/drm2-hangman/lib/Screen.rb new file mode 100644 index 0000000..d3d9fe1 --- /dev/null +++ b/drm2-hangman/lib/Screen.rb @@ -0,0 +1,86 @@ +class Screen + def initialize + @states = [ <<'STATE0', <<'STATE1', <<'STATE2', <<'STATE3', <<'STATE4', <<'STATE5', <<'STATE6' ] ++-----+ +| | +| +| +| +| +| ++-----------+ +STATE0 ++-----+ +| | +| (x) +| +| +| +| ++-----------+ +STATE1 ++-----+ +| | +| (x) +| | +| | +| +| ++-----------+ +STATE2 ++-----+ +| | +| (x) +| /| +| | +| +| ++-----------+ +STATE3 ++-----+ +| | +| (x) +| /|\ +| | +| +| ++-----------+ +STATE4 ++-----+ +| | +| (x) +| /|\ +| | +| / +| ++-----------+ +STATE5 ++-----+ +| | +| (x) +| /|\ +| | +| / \ +| ++-----------+ +STATE6 + end + + # index is entirely based on the number of misses + def get_state index + @states[index] + end + + # ask user for input (with or without a question) + def ask_for_input question = nil + write question if question + + print "> " + gets.chomp + end + + # helper method for writing to the console (in case we want to format the output) + def write text + puts text + end +end diff --git a/drm2-hangman/lib/Word.rb b/drm2-hangman/lib/Word.rb new file mode 100644 index 0000000..3c45d45 --- /dev/null +++ b/drm2-hangman/lib/Word.rb @@ -0,0 +1,23 @@ +class Word + def initialize word + @word = word.downcase + end + + def has_letter? letter + @word.include? letter.downcase + end + + def get_letters + @word.chars + end + + def get_word + @word + end + + def get_lines guesses + @word.chars.map{ |char| + (guesses.include? char) ? char : '_' + }.join + end +end diff --git a/drm2-hangman/lib/WordFactory.rb b/drm2-hangman/lib/WordFactory.rb new file mode 100644 index 0000000..a10cae5 --- /dev/null +++ b/drm2-hangman/lib/WordFactory.rb @@ -0,0 +1,13 @@ +require_relative 'Word' +require 'random-word' + +class WordFactory + # return a random word or phrase + def self.get_words + randoms = (RandomWord.nouns.next).split '_' + + randoms.map do |word| + Word.new(word) + end + end +end diff --git a/drm2-hangman/main.rb b/drm2-hangman/main.rb new file mode 100644 index 0000000..e0c6b9e --- /dev/null +++ b/drm2-hangman/main.rb @@ -0,0 +1,5 @@ +require_relative 'lib/Game' + +game = Game.new + +game.run