diff --git a/ascii_art.rb b/ascii_art.rb new file mode 100644 index 0000000..5f4383f --- /dev/null +++ b/ascii_art.rb @@ -0,0 +1,37 @@ +class AsciiArt + attr_reader :body, :candle_light, :candle_body + attr_writer :candle_light, :candle_body + def initialize + @body = [ +" ||||| + @@@@@@@@@ + {~@~@~@~} +@@@@@@@@@@@@@ +{~@ HAPPY @~} +{ BIRTHDAY } +@@@@@@@@@@@@@".blue +] + # @candle_light = [" `````"] #don't move + # @candle_body = [" |||||"] #don't move + @candle_light = " `````" #don't move + + + end + + def change_candles + a = @candle_light.split('') + a.pop + @candle_light = a.join + end + + def take_away_candle + change_candles + puts @candle_light.yellow + puts @body + end + + def show_cake + puts @candle_light.yellow + puts @body + end +end diff --git a/main.rb b/main.rb new file mode 100644 index 0000000..fc4fe32 --- /dev/null +++ b/main.rb @@ -0,0 +1,43 @@ +require 'artii' +require_relative 'user_input' +require_relative 'random_word' +require_relative 'ascii_art' +require 'colorize' +require 'random_word_generator' + +penny = RandomWord.new +user = UserInput.new +cake = AsciiArt.new + +# game greeting and instructions +puts "Welcome to Word-Guess!".blue +puts "=".blue * 22 +puts "GAME INSTRUCTIONS:".colorize(:green) +puts "1.) Guess the word I'm thinking for a chance to have my Birthday Cake.".green +puts "2.) For every incorrect letter you choose, I will blow out one candle.".green +puts "3.) If you guess incorrectly 5 times, you lose and I will happliy keep my cake.".green +cake.show_cake +puts "READY...".red +puts "SET...".red + +# loops until underscore array == random word || length of wrong_letters == 5 +until penny.underscore == penny.word || user.wrong_letters.length == 5 + puts "Guess >> ".green + + # check user input letter + user.check_letter(penny, user, cake) + + # checks for loser and exits game + if user.wrong_letters.length == 5 + puts + puts "=".colorize(:light_blue) * 16 + puts "The word was #{penny.word.join.upcase}" + puts "YOU LOSE, I WIN!".blue + end +end + +# checks for winner and outputs win message +if penny.underscore == penny.word + puts "YOU WIN!!!. You can eat my birthday cake!".green + cake.show_cake +end diff --git a/random_word.rb b/random_word.rb new file mode 100644 index 0000000..92a8a26 --- /dev/null +++ b/random_word.rb @@ -0,0 +1,33 @@ +class RandomWord + attr_reader :word, :underscore + + def initialize() + # word_arr = ["envelope", "cat", "dragon", "coffee", "water", "pencil"] + # @word = word_arr[rand(word_arr.length)].split('') + @word = RandomWordGenerator.of_length(5).split('') + @length = @word.length + @underscore = ("_ " * @length).split(' ') + end + + + def rewrite_underscore(user_input) + # if user correctly guesses whole world, they win. + if @word.join == user_input.current_letter + arr = user_input.current_letter.split('') + arr.length.times do |i| + @underscore[i] = arr[i] + end + + else + # find index(es) of letter(s) of random word + arr = @word.each_index.select{|n| @word[n] == user_input.current_letter} + # replace underscore with right letter + arr.each do |i| + @underscore[i] = user_input.current_letter + end + print @underscore + end + + end + +end diff --git a/user_input.rb b/user_input.rb new file mode 100644 index 0000000..4abf167 --- /dev/null +++ b/user_input.rb @@ -0,0 +1,52 @@ +class UserInput + attr_reader :current_letter, :wrong_letters + def initialize + @current_letter = "" + @wrong_letters = [] + @right_letters = [] + end + + def check_letter(random_word, user_input, cake) + guess_display + @current_letter = gets.chomp.downcase + # checks that user input is actually a letter, makes them try again if not. + until @current_letter =~ /[[:alpha:]]/ + puts "Invalid guess - please only guess one letter." + return + end + # if user correctly guesses whole word, they win. + if random_word.word.join == @current_letter + random_word.rewrite_underscore(user_input) + return + # if random word array includes @current_letter + elsif random_word.word.include?(@current_letter) + if @right_letters.include?(@current_letter) + puts "You've already tried that one, try something different!" + return + end + @right_letters << @current_letter + puts "Good job, keep guessin'" + # replace underscore at that index with user_input(@current_letter) + random_word.rewrite_underscore(user_input) + return + else + if @wrong_letters.include?(@current_letter) + puts "You've already tried that one, try something different!" + return + end + @wrong_letters << @current_letter + puts "WRONG, you loss a candle. Try Again".red + cake.take_away_candle + return + end + end + + # prints the already guessed letters + def guess_display + already_guessed = @wrong_letters + @right_letters + already_guessed = already_guessed.join(', ') + + puts "You've already guessed: #{already_guessed}".yellow + end + +end