From 4d13332d344556fa5df6df962524d637daa090d0 Mon Sep 17 00:00:00 2001 From: Cara E Comfort Date: Fri, 17 Feb 2017 16:12:39 -0800 Subject: [PATCH 1/3] Image files --- dandelions1.txt | 3 +++ dandelions2.txt | 3 +++ dandelions3.txt | 3 +++ dandelions4.txt | 3 +++ dandelions5.txt | 3 +++ dandelions6.txt | 3 +++ stem.txt | 6 ++++++ 7 files changed, 24 insertions(+) create mode 100644 dandelions1.txt create mode 100644 dandelions2.txt create mode 100644 dandelions3.txt create mode 100644 dandelions4.txt create mode 100644 dandelions5.txt create mode 100644 dandelions6.txt create mode 100644 stem.txt diff --git a/dandelions1.txt b/dandelions1.txt new file mode 100644 index 0000000..e68bc34 --- /dev/null +++ b/dandelions1.txt @@ -0,0 +1,3 @@ + .;:;:. + ::;:;: + ';:;;' diff --git a/dandelions2.txt b/dandelions2.txt new file mode 100644 index 0000000..12a0af5 --- /dev/null +++ b/dandelions2.txt @@ -0,0 +1,3 @@ + .;:; `::` + ::;:': / + ';:;;' ` diff --git a/dandelions3.txt b/dandelions3.txt new file mode 100644 index 0000000..bdb6d91 --- /dev/null +++ b/dandelions3.txt @@ -0,0 +1,3 @@ + .;: `::` + ::;: / + ';:;;' ` diff --git a/dandelions4.txt b/dandelions4.txt new file mode 100644 index 0000000..0de4be6 --- /dev/null +++ b/dandelions4.txt @@ -0,0 +1,3 @@ +. `::` +:: / +';:; ` diff --git a/dandelions5.txt b/dandelions5.txt new file mode 100644 index 0000000..6d79cd6 --- /dev/null +++ b/dandelions5.txt @@ -0,0 +1,3 @@ + `::` +: / +'; ` diff --git a/dandelions6.txt b/dandelions6.txt new file mode 100644 index 0000000..662d850 --- /dev/null +++ b/dandelions6.txt @@ -0,0 +1,3 @@ + `::` + / +' ` diff --git a/stem.txt b/stem.txt new file mode 100644 index 0000000..8e30a7e --- /dev/null +++ b/stem.txt @@ -0,0 +1,6 @@ +_ || +>'. || _ + `> ||.'< + `>|/ <` + `||/` +^^^^^^^^^^^^^^^^^ From 5e4fe0d59b5d59d0898c029b5b67fb6446b72889 Mon Sep 17 00:00:00 2001 From: Cara E Comfort Date: Fri, 17 Feb 2017 16:13:25 -0800 Subject: [PATCH 2/3] Word guess game --- word-guess.rb | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 word-guess.rb diff --git a/word-guess.rb b/word-guess.rb new file mode 100644 index 0000000..a001bb6 --- /dev/null +++ b/word-guess.rb @@ -0,0 +1,131 @@ +require "colorize" +require 'random_word_generator' + +class Word + attr_reader :current_status, :complete_word + + def initialize (word) + @complete_word = word + @current_status = generate_dashes + end + + def generate_dashes + number_of_dashes = "" + @complete_word.length.times do + number_of_dashes += "_" + end + return number_of_dashes + end + + def letter_sentinel(guess) + # check to see if letter matches (.chars would be like .to_a for a string.) + @complete_word.chars.each_with_index do |letter, index| + if guess == letter + @current_status[index] = letter + end + end + # check to see if whole word matches + if @complete_word == guess + @current_status = @complete_word + end + @current_status + end + + def correct_guess?(guess) + @complete_word.include?(guess) || @complete_word == guess + end + +end + +class Game + def initialize (difficulty) + + if difficulty == "low" + @word = Word.new(RandomWordGenerator.of_length(rand (10..14))) + elsif difficulty == "medium" + @word = Word.new(RandomWordGenerator.of_length(rand (6..9))) + elsif difficulty == "high" + @word = Word.new(RandomWordGenerator.of_length(rand (3..5))) + else + puts "Hmm I don't recognize #{difficulty}. I'll just generate a random word" + + " for you." + @word = Word.new(RandomWordGenerator.word) + end + + @number_of_guesses = 6 + @guesses = [] + end + + def play + until @number_of_guesses == 0 || @word.current_status == @word.complete_word + puts @word.complete_word + puts "\nGuesses so far: #{@guesses.join(", ")}" + draw_top(@number_of_guesses -1) + draw_stem + puts @word.current_status + print "Guess a letter: " + + guess = gets.chomp.downcase + if !@guesses.include?(guess) && (guess.length == 1 || guess.length == @word.complete_word.length) + @guesses << guess + make_a_guess + elsif @guesses.include?(guess) + puts "\nYou have already guessed #{guess}." + else + puts "\nPlease guess a letter or the entire word." + end + end + end_game + end + + def make_a_guess + @word.letter_sentinel(@guesses.last) + if !@word.correct_guess?(@guesses.last) + @number_of_guesses -= 1 + end + end + + def draw_stem + x = ["./stem.txt"] + File.open(x[0]) do |f| + f.each_line do |line| + print line.colorize(:green) + end + end + end + + def draw_top(index) + tops = ["./dandelions6.txt", "./dandelions5.txt", "./dandelions4.txt", "./dandelions3.txt", "./dandelions2.txt", "./dandelions1.txt"] + File.open(tops[index]) do |f| + f.each_line do |line| + print line.colorize(:magenta) + end + end + end + +# finish + def end_game + if @number_of_guesses == 0 + puts + draw_stem + puts "Ou! You lost :(\n" + "The word was '#{@word.complete_word}'" + else + puts "\nYOU WON!" + end + puts "The word was '#{@word.complete_word}'" + end +end + +# Play the game until the user wants to quit +while true + puts "Chose your difficulty level. Low, medium or high: " + game = Game.new(gets.chomp.downcase) + game.play + + print "\nWould you like to play again? Enter 'Yes' or 'No': " + anwser = gets.chomp.downcase + if anwser == "no" + break + end +end From 392330f81a0e1341104634072680b39aa102b0aa Mon Sep 17 00:00:00 2001 From: Cara E Comfort Date: Fri, 17 Feb 2017 20:38:54 -0800 Subject: [PATCH 3/3] Deleted cheat line that showed the word --- word-guess.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/word-guess.rb b/word-guess.rb index a001bb6..4a04edb 100644 --- a/word-guess.rb +++ b/word-guess.rb @@ -58,7 +58,7 @@ def initialize (difficulty) def play until @number_of_guesses == 0 || @word.current_status == @word.complete_word - puts @word.complete_word + #puts @word.complete_word puts "\nGuesses so far: #{@guesses.join(", ")}" draw_top(@number_of_guesses -1) draw_stem