From 17992a5bd2a5dbf4ee3abf2b251f306af0241e56 Mon Sep 17 00:00:00 2001 From: nkiru Date: Thu, 17 Aug 2017 14:48:23 -0700 Subject: [PATCH 1/6] Create files:main,word and letter. Created method to convert words to underscores. --- letter.rb | 23 +++++++++++++++++++++++ main.rb | 2 ++ word.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 letter.rb create mode 100644 main.rb create mode 100644 word.rb diff --git a/letter.rb b/letter.rb new file mode 100644 index 0000000..668bba4 --- /dev/null +++ b/letter.rb @@ -0,0 +1,23 @@ +class letter + attr accessor :letter + + def initialize(letter) + @letter = letter + end + + def add_word + + #compare to word, add to space if part of word check? + end + + def limit_input + # to ensure only word is added at a time + end + + def ascii_change + # draw art associated with wrong guess + end + + + +end # end of letter class diff --git a/main.rb b/main.rb new file mode 100644 index 0000000..09ac807 --- /dev/null +++ b/main.rb @@ -0,0 +1,2 @@ +require 'letter' +require 'word' diff --git a/word.rb b/word.rb new file mode 100644 index 0000000..856881b --- /dev/null +++ b/word.rb @@ -0,0 +1,40 @@ +# require 'letter' + +class Word + attr_reader :word + + def initialize(word) + @word = word + end + + def display_dash(dash_count) + dash_count = @word.split("").count + dash_count.times do + print " _ " + end + end + + def list(word) + word = "blue" + end + + def check + # counting guesses? + end + + def correct_guess + # display actual guess/word + end + + def wrong_guess + # display incorrect guess/es + end + + def results + end + + +end # end of word class + +barb = Word.new("cat") +puts barb.display_dash("cat") From 790848e91190d7df7985d78fd3f925888cb1de9d Mon Sep 17 00:00:00 2001 From: nkiru Date: Thu, 17 Aug 2017 15:01:38 -0700 Subject: [PATCH 2/6] create method that chooses a random word to guess --- word.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/word.rb b/word.rb index 856881b..47e20f6 100644 --- a/word.rb +++ b/word.rb @@ -14,8 +14,8 @@ def display_dash(dash_count) end end - def list(word) - word = "blue" + def list(random_words) + @word << random_words end def check @@ -36,5 +36,6 @@ def results end # end of word class -barb = Word.new("cat") +random = ["cat", "dog", "frog", "horse", "bird", "monkey", "pidgeon", "mouse", "rabbit", "lama"].sample +barb = Word.new(random) puts barb.display_dash("cat") From 0e831eedcf6fc305a889590c401d0b8119cb79d7 Mon Sep 17 00:00:00 2001 From: nkiru Date: Fri, 18 Aug 2017 14:17:04 -0700 Subject: [PATCH 3/6] Updated main.rb --- letter.rb | 11 +++++--- main.rb | 34 +++++++++++++++++++++++- word.rb | 77 +++++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 100 insertions(+), 22 deletions(-) diff --git a/letter.rb b/letter.rb index 668bba4..3cd7e13 100644 --- a/letter.rb +++ b/letter.rb @@ -1,12 +1,12 @@ -class letter - attr accessor :letter +class Letter + attr_accessor :letter def initialize(letter) @letter = letter end - def add_word - + def add_to_word + #compare to word, add to space if part of word check? end @@ -21,3 +21,6 @@ def ascii_change end # end of letter class + +letter = Letter.new(guess) +puts letter diff --git a/main.rb b/main.rb index 09ac807..7b7af43 100644 --- a/main.rb +++ b/main.rb @@ -1,2 +1,34 @@ -require 'letter' require 'word' + +# User select level of difficulty +puts " + +__ __ _ +\ \ / / | | + \ \ /\ / /__ _ __ __| | + \ \/ \/ / _ \| '__/ _` | + \ /\ / (_) | | | (_| | + \/ \/ \___/|_| \__,_| + + ______ + |___ / + / / ___ _ __ ___ + / / / _ \| '_ \ / _ \ + / /__ (_) | | | | __/ + /_____\___/|_| |_|\___|" + + +puts "Welcome to Word Zone! You have 6 tries to guess a word or be booted out of the game. To unlock the secret token, you will have to complete all three levels.\n Select difficulty level: 1: Easy(Color), 2: Intermediate(Starwars characters), 3: Hard(Superhero powers)!" +level = gets.chomp.to_i + + case level + when 1 + word = Faker::Color.color_name + when 2 + word = Faker::StarWars.character + when 3 + word = Faker::Superhero.power + end + + +word_guess = Word.new(word) diff --git a/word.rb b/word.rb index 47e20f6..3884189 100644 --- a/word.rb +++ b/word.rb @@ -1,41 +1,84 @@ # require 'letter' class Word - attr_reader :word + attr_reader :word, :dash_array, :word_array, :correct_guess, :wrong_guess def initialize(word) @word = word + @dash_array = dash_array + @word_array = word_array + @correct_guess = correct_guess + @wrong_guess = wrong_guess end - def display_dash(dash_count) - dash_count = @word.split("").count - dash_count.times do - print " _ " + def letter_in_word(letter) + # split_word + if @word_array.include?(letter) + # index = @word.each_index.select{|i| @word[i] == letter} + # index.each do |value| + # value = "_" + # puts value end + end end - def list(random_words) - @word << random_words - end +def split_word(word) + @word_array.split("") +end - def check - # counting guesses? +def display_dash(dash_count) + dash_count = @word_array.count + dash_count.times do + print " _ " end +end - def correct_guess - # display actual guess/word - end +# def list(random_words) +# @word << random_words +# end - def wrong_guess - # display incorrect guess/es - end +def check + until @wrong_guesses == 6 || @word == @show_user + puts "Guess a letter:" + @guess = gets.chomp +end - def results +def check_guess(guess) + if @word.include?(guess) + if !@show_user.include?(@guess) + puts "Great! #{guess} is a match!" + @correct_guesses += 1 + replace_letter + end + else + puts "Nope" + @wrong_guesses += 1 + ascii_change end + @total_guesses += 1 + puts @show_user.join +end + + +# def correct_guess +# if @word_array.include?(letter) +# @correct_guess = split_word.display_dash +# end +# # displ ay actual guess/word +# end + + +def wrong_guess + # display incorrect guess/es +end +def results +end end # end of word class random = ["cat", "dog", "frog", "horse", "bird", "monkey", "pidgeon", "mouse", "rabbit", "lama"].sample barb = Word.new(random) puts barb.display_dash("cat") +barb.split_word("cat") +barb.letter_in_word("a") From fdf32683dc703bc2703d035bd966acfadb36cfcb Mon Sep 17 00:00:00 2001 From: nkiru Date: Fri, 18 Aug 2017 14:22:38 -0700 Subject: [PATCH 4/6] Updated word.rb with new methods, shelved letter.rb --- letter.rb | 52 +++++++-------- main.rb | 3 +- word.rb | 184 +++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 153 insertions(+), 86 deletions(-) diff --git a/letter.rb b/letter.rb index 3cd7e13..d9ffc88 100644 --- a/letter.rb +++ b/letter.rb @@ -1,26 +1,26 @@ -class Letter - attr_accessor :letter - - def initialize(letter) - @letter = letter - end - - def add_to_word - - #compare to word, add to space if part of word check? - end - - def limit_input - # to ensure only word is added at a time - end - - def ascii_change - # draw art associated with wrong guess - end - - - -end # end of letter class - -letter = Letter.new(guess) -puts letter +# class Letter +# attr_accessor :letter +# +# def initialize(letter) +# @letter = letter +# end +# +# def add_to_word +# +# #compare to word, add to space if part of word check? +# end +# +# def limit_input +# # to ensure only word is added at a time +# end +# +# def ascii_change +# # draw art associated with wrong guess +# end +# +# +# +# end # end of letter class +# +# letter = Letter.new(guess) +# puts letter diff --git a/main.rb b/main.rb index 7b7af43..bf1510f 100644 --- a/main.rb +++ b/main.rb @@ -1,4 +1,5 @@ -require 'word' +require_relative 'word' +require 'Faker' # User select level of difficulty puts " diff --git a/word.rb b/word.rb index 3884189..4194f8b 100644 --- a/word.rb +++ b/word.rb @@ -1,84 +1,150 @@ -# require 'letter' +require 'colorize' +require 'faker' +#require 'letter' class Word - attr_reader :word, :dash_array, :word_array, :correct_guess, :wrong_guess + attr_accessor :word def initialize(word) - @word = word - @dash_array = dash_array - @word_array = word_array - @correct_guess = correct_guess - @wrong_guess = wrong_guess + @word = word.upcase.split("") + @total_guesses = 0 + @correct_guesses = 0 # compare to word length + @wrong_guesses = 0 # count down + #create array of all the blank spaces + @dash_array = [] + @word.each do |char| + if char == " " + @dash_array.push(" ") + else + @dash_array.push("_ ") + end + end + puts "Can you guess this word?" + puts @dash_array.join + @previous_guesses = [] + track_guesses end - def letter_in_word(letter) - # split_word - if @word_array.include?(letter) - # index = @word.each_index.select{|i| @word[i] == letter} - # index.each do |value| - # value = "_" - # puts value + # user input + def track_guesses + until @wrong_guesses == 6 || @word == @dash_array + print "Guess a letter:" + @guess = gets.chomp.upcase! + #check if user enters the same guess more than once + if @previous_guesses.include?(@guess) == false + @previous_guesses.push(@guess) + puts "Previous guess #{@previous_guesses}" + check_guess(@guess) + else + puts "You already picked that letter." + end + + if @wrong_guesses == 6 + puts "GAME OVER." + puts "The words is #{@word.join}" + exit + end + if @word == @dash_array + puts "Way to go!! The word is #{@dash_array.join}!!" + exit end end end -def split_word(word) - @word_array.split("") -end +# track number of correct and wrong guesses + def check_guess(guess) + if @word.include?(guess) + if !@dash_array.include?(@guess) + puts "Great! #{guess} is a match!" + @correct_guesses += 1 + replace_letter + end + else + puts "Try again" + @wrong_guesses += 1 + ascii_change + end + @total_guesses += 1 + puts @dash_array.join + end -def display_dash(dash_count) - dash_count = @word_array.count - dash_count.times do - print " _ " + def replace_letter + @word.length.times do |i| + if @guess == @word[i] + @dash_array[i] = @guess + end + end end end -# def list(random_words) -# @word << random_words -# end +def ascii_change + case @wrong_guesses + when 1 + puts ("|").colorize(:green) -def check - until @wrong_guesses == 6 || @word == @show_user - puts "Guess a letter:" - @guess = gets.chomp -end + when 2 + puts (" + | | ").colorize(:red) -def check_guess(guess) - if @word.include?(guess) - if !@show_user.include?(@guess) - puts "Great! #{guess} is a match!" - @correct_guesses += 1 - replace_letter - end - else - puts "Nope" - @wrong_guesses += 1 - ascii_change + when 3 + puts (" + | | + | | ___ ___ ___ _ ").colorize(:red) + + when 4 + puts (" + | | + | | ___ ___ ___ _ __ + | | / _ \/ __|/ _ \ '__|").colorize(:red) + + when 5 + puts (" + | | + | | ___ ___ ___ _ __ + | | / _ \/ __|/ _ \ '__| + | |____ (_) \__ \ __/ |").colorize(:blue) + + when 6 + puts (" + | | + | | ___ ___ ___ _ __ + | | / _ \/ __|/ _ \ '__| + | |____ (_) \__ \ __/ | + |______\___/|___/\___|_|").colorize(:red).blink end - @total_guesses += 1 - puts @show_user.join end +# random = ["cat", "dog", "frog", "horse", "bird", "monkey", "pidgeon", "mouse", "rabbit", "lama"].sample -# def correct_guess -# if @word_array.include?(letter) -# @correct_guess = split_word.display_dash -# end -# # displ ay actual guess/word -# end +# User select level of difficulty +puts " +__ __ _ +\ \ / / | | + \ \ /\ / /__ _ __ __| | + \ \/ \/ / _ \| '__/ _` | + \ /\ / (_) | | | (_| | + \/ \/ \___/|_| \__,_| -def wrong_guess - # display incorrect guess/es -end + ______ + |___ / + / / ___ _ __ ___ + / / / _ \| '_ \ / _ \ + / /__ (_) | | | | __/ + /_____\___/|_| |_|\___|" -def results -end -end # end of word class +puts "Welcome to Word Zone! You have 6 tries to guess a word or be booted out of the game. To unlock the secret token, you will have to complete all three levels.\n Select difficulty level: 1: Easy, 2: Intermediate, 3: Hard!" +level = gets.chomp.to_i + + case level + when 1 + word = Faker::Color.color_name + when 2 + word = Faker::StarWars.character + when 3 + word = Faker::Superhero.power + end + -random = ["cat", "dog", "frog", "horse", "bird", "monkey", "pidgeon", "mouse", "rabbit", "lama"].sample -barb = Word.new(random) -puts barb.display_dash("cat") -barb.split_word("cat") -barb.letter_in_word("a") +word_guess = Word.new(word) From 4c215cebc2942fdc578f2b75c6bb7773279afd4b Mon Sep 17 00:00:00 2001 From: nkiru Date: Fri, 18 Aug 2017 14:54:08 -0700 Subject: [PATCH 5/6] Edited ascii art in main.rb; removed duplicate code from main.rb. --- main.rb | 25 +++++++++++++------------ word.rb | 41 ++++------------------------------------- 2 files changed, 17 insertions(+), 49 deletions(-) diff --git a/main.rb b/main.rb index bf1510f..2301e6f 100644 --- a/main.rb +++ b/main.rb @@ -1,25 +1,26 @@ require_relative 'word' -require 'Faker' +require 'faker' +require 'colorize' # User select level of difficulty puts " __ __ _ -\ \ / / | | - \ \ /\ / /__ _ __ __| | - \ \/ \/ / _ \| '__/ _` | - \ /\ / (_) | | | (_| | - \/ \/ \___/|_| \__,_| +\\ \\ / / | | + \\ \\ /\\ / /__ _ __ __| | + \\ \\/ \\/ / _ \\| '__/ _` | + \\ /\\ / (_) | | | (_| | + \\/ \\/ \\___/|_| \\__,_| - ______ + ______ |___ / - / / ___ _ __ ___ - / / / _ \| '_ \ / _ \ - / /__ (_) | | | | __/ - /_____\___/|_| |_|\___|" + / / ___ _ _ __ + / / / _ \\| __\\ / _ | + / /__ (_) | | || __/ + /_____\\___/|_| |_|\\ \\\n\n" -puts "Welcome to Word Zone! You have 6 tries to guess a word or be booted out of the game. To unlock the secret token, you will have to complete all three levels.\n Select difficulty level: 1: Easy(Color), 2: Intermediate(Starwars characters), 3: Hard(Superhero powers)!" +puts "Welcome to Word Zone! You have 6 tries to guess a word or be booted out of the game.\n To unlock the secret token, you will have to complete all three levels.\n Select difficulty level:\n 1: Easy: Color, 2: Intermediate: Starwars Characters, 3: Hard: Superhero power!\n" level = gets.chomp.to_i case level diff --git a/word.rb b/word.rb index 4194f8b..ede4de3 100644 --- a/word.rb +++ b/word.rb @@ -80,22 +80,22 @@ def replace_letter def ascii_change case @wrong_guesses when 1 - puts ("|").colorize(:green) + puts ("|").colorize(:blue) when 2 puts (" - | | ").colorize(:red) + | | ").colorize(:blue) when 3 puts (" | | - | | ___ ___ ___ _ ").colorize(:red) + | | ___ ___ ___ _ ").colorize(:blue) when 4 puts (" | | | | ___ ___ ___ _ __ - | | / _ \/ __|/ _ \ '__|").colorize(:red) + | | / _ \/ __|/ _ \ '__|").colorize(:blue) when 5 puts (" @@ -115,36 +115,3 @@ def ascii_change end # random = ["cat", "dog", "frog", "horse", "bird", "monkey", "pidgeon", "mouse", "rabbit", "lama"].sample - -# User select level of difficulty -puts " - -__ __ _ -\ \ / / | | - \ \ /\ / /__ _ __ __| | - \ \/ \/ / _ \| '__/ _` | - \ /\ / (_) | | | (_| | - \/ \/ \___/|_| \__,_| - - ______ - |___ / - / / ___ _ __ ___ - / / / _ \| '_ \ / _ \ - / /__ (_) | | | | __/ - /_____\___/|_| |_|\___|" - - -puts "Welcome to Word Zone! You have 6 tries to guess a word or be booted out of the game. To unlock the secret token, you will have to complete all three levels.\n Select difficulty level: 1: Easy, 2: Intermediate, 3: Hard!" -level = gets.chomp.to_i - - case level - when 1 - word = Faker::Color.color_name - when 2 - word = Faker::StarWars.character - when 3 - word = Faker::Superhero.power - end - - -word_guess = Word.new(word) From fe319dbfb2b13e961ed2fd44fc399fe5ce61e44c Mon Sep 17 00:00:00 2001 From: nkiru Date: Fri, 18 Aug 2017 15:12:38 -0700 Subject: [PATCH 6/6] Create winner ascii win method --- word.rb | 87 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 38 deletions(-) diff --git a/word.rb b/word.rb index ede4de3..f0c869f 100644 --- a/word.rb +++ b/word.rb @@ -12,17 +12,17 @@ def initialize(word) @wrong_guesses = 0 # count down #create array of all the blank spaces @dash_array = [] - @word.each do |char| - if char == " " - @dash_array.push(" ") - else - @dash_array.push("_ ") - end + @word.each do |char| + if char == " " + @dash_array.push(" ") + else + @dash_array.push("_ ") end - puts "Can you guess this word?" - puts @dash_array.join - @previous_guesses = [] - track_guesses + end + puts "Can you guess this word?" + puts @dash_array.join + @previous_guesses = [] + track_guesses end # user input @@ -30,7 +30,7 @@ def track_guesses until @wrong_guesses == 6 || @word == @dash_array print "Guess a letter:" @guess = gets.chomp.upcase! - #check if user enters the same guess more than once + #check if user enters the same guess more than once if @previous_guesses.include?(@guess) == false @previous_guesses.push(@guess) puts "Previous guess #{@previous_guesses}" @@ -42,30 +42,31 @@ def track_guesses if @wrong_guesses == 6 puts "GAME OVER." puts "The words is #{@word.join}" - exit + exit end if @word == @dash_array puts "Way to go!! The word is #{@dash_array.join}!!" + ascii_win exit end end end -# track number of correct and wrong guesses + # track number of correct and wrong guesses def check_guess(guess) - if @word.include?(guess) - if !@dash_array.include?(@guess) - puts "Great! #{guess} is a match!" - @correct_guesses += 1 - replace_letter - end - else - puts "Try again" - @wrong_guesses += 1 - ascii_change + if @word.include?(guess) + if !@dash_array.include?(@guess) + puts "Great! #{guess} is a match!" + @correct_guesses += 1 + replace_letter end - @total_guesses += 1 - puts @dash_array.join + else + puts "Try again" + @wrong_guesses += 1 + ascii_change + end + @total_guesses += 1 + puts @dash_array.join end def replace_letter @@ -77,41 +78,51 @@ def replace_letter end end -def ascii_change - case @wrong_guesses - when 1 + def ascii_change + case @wrong_guesses + when 1 puts ("|").colorize(:blue) - when 2 + when 2 puts (" | | ").colorize(:blue) - when 3 + when 3 puts (" | | | | ___ ___ ___ _ ").colorize(:blue) - when 4 + when 4 puts (" | | | | ___ ___ ___ _ __ | | / _ \/ __|/ _ \ '__|").colorize(:blue) - when 5 - puts (" + when 5 + puts (" | | | | ___ ___ ___ _ __ | | / _ \/ __|/ _ \ '__| | |____ (_) \__ \ __/ |").colorize(:blue) - when 6 - puts (" + when 6 + puts (" | | | | ___ ___ ___ _ __ | | / _ \/ __|/ _ \ '__| | |____ (_) \__ \ __/ | |______\___/|___/\___|_|").colorize(:red).blink - end -end + end -# random = ["cat", "dog", "frog", "horse", "bird", "monkey", "pidgeon", "mouse", "rabbit", "lama"].sample + def ascii_win + puts " + /$$ /$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$ + | $$ /$ | $$|_ $$_/| $$$ | $$| $$$ | $$| $$_____/| $$__ $$ + | $$ /$$$| $$ | $$ | $$$$| $$| $$$$| $$| $$ | $$ \ $$ + | $$/$$ $$ $$ | $$ | $$ $$ $$| $$ $$ $$| $$$$$ | $$$$$$$/ + | $$$$_ $$$$ | $$ | $$ $$$$| $$ $$$$| $$__/ | $$__ $$ + | $$$/ \ $$$$ | $$ | $$\ $$$| $$$\ $$$| $$ | $$ \ $$ + | $$/ \ $$ /$$$$$$| $$ \ $$| $$$ \ $$| $$$$$$$$| $$ | $$ + |__/ \__/|______/|__/ \__/|__/ \__/|________/|__/ |__/" + end +end