From c580a84c78298ba29d1a6dacb721b209db1c1e72 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Mon, 12 Feb 2018 17:24:17 -0800 Subject: [PATCH 01/19] Skeleton Game class --- word_guess.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 word_guess.rb diff --git a/word_guess.rb b/word_guess.rb new file mode 100644 index 0000000..b21090e --- /dev/null +++ b/word_guess.rb @@ -0,0 +1,15 @@ +require 'faker' +require 'colorize' + +class Game + attr_reader :word, :board, :attempts, :themes + + def initialize + @word = "" + @board = "" + @attempts_left = 5 + @themes = [ "music", "food", "science" ] + end + +end +game = Game.new From e1038ea9eb95893efa962ead807e5c699ca4d289 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Mon, 12 Feb 2018 17:31:21 -0800 Subject: [PATCH 02/19] Add get_theme_word method --- word_guess.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/word_guess.rb b/word_guess.rb index b21090e..eaeeb9a 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -11,5 +11,15 @@ def initialize @themes = [ "music", "food", "science" ] end + def get_theme_word theme + case theme + when "music" + return Faker::Music.instrument + when "food" + return Faker::Food.ingredient + when "science" + return Faker::Science.element + end + end end game = Game.new From a92c9057a3d7a9aa812c779fc1394d65bb983040 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Mon, 12 Feb 2018 17:34:51 -0800 Subject: [PATCH 03/19] Add create_board and display_board method --- word_guess.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/word_guess.rb b/word_guess.rb index eaeeb9a..78a1961 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -21,5 +21,15 @@ def get_theme_word theme return Faker::Science.element end end + + def create_board + word_size = @word.length + @board = "-" * word_size + end + + def display_board + show_flower + puts @board + end end game = Game.new From 55f0ae2fdc97249310aa114f8b267e5be4dff046 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Mon, 12 Feb 2018 17:36:47 -0800 Subject: [PATCH 04/19] Add pick_theme method --- word_guess.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/word_guess.rb b/word_guess.rb index 78a1961..d0b82ee 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -11,6 +11,13 @@ def initialize @themes = [ "music", "food", "science" ] end + def pick_theme + puts "Choose theme:" + puts @themes + chosen_theme = gets.chomp + return chosen_theme + end + def get_theme_word theme case theme when "music" From 2593f09b638fdcc64b2681338475a67585d8066e Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Mon, 12 Feb 2018 17:39:18 -0800 Subject: [PATCH 05/19] Add play_game method --- word_guess.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/word_guess.rb b/word_guess.rb index d0b82ee..00e936f 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -38,5 +38,12 @@ def display_board show_flower puts @board end + + def play_game + theme = pick_theme + @word = get_theme_word theme + create_board + end + end game = Game.new From 181c8eacf576826ba5bab2bfb62408c36eee3ba6 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Mon, 12 Feb 2018 17:41:52 -0800 Subject: [PATCH 06/19] Add show_flower method --- word_guess.rb | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/word_guess.rb b/word_guess.rb index 00e936f..818dbe3 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -39,6 +39,64 @@ def display_board puts @board end + def show_flower + case @attempts_left + when 5 + puts """ + (@)(@)(@)(@)(@) + ,\,\,|,/,/, + _\|/_ + |_____| + | | + |___| + """.yellow + when 4 + puts """ + (@)(@)(@)(@) + ,\,\,|,/,/, + _\|/_ + |_____| + | | + |___| + """.magenta + when 3 + puts """ + (@)(@)(@) + ,\,\,|,/,/, + _\|/_ + |_____| + | | + |___| + """.green + when 2 + puts """ + (@)(@) + ,\,\,|,/,/, + _\|/_ + |_____| + | | + |___| + """.blue + when 1 + puts """ + (@) + ,\,\,|,/,/, + _\|/_ + |_____| + | | + |___| + """.light_red + when 0 + puts """ + Game Over + ,\,\,|,/,/, + _\|/_ + |_____| + | | + |___| + """.red + end + end def play_game theme = pick_theme @word = get_theme_word theme From 3a6b1428650cc6ea31023042edf0b1020a5f3bb3 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Mon, 12 Feb 2018 17:43:24 -0800 Subject: [PATCH 07/19] Add guessed_letter method --- word_guess.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/word_guess.rb b/word_guess.rb index 818dbe3..b133ddb 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -97,6 +97,12 @@ def show_flower """.red end end + + def guessed_letter + puts "Please pick a letter (a-z). Your attempts_left are #{@attempts_left}" + return guessed_letter = gets.chomp + end + def play_game theme = pick_theme @word = get_theme_word theme From 35a85dae8a85435ea69164f9d10f00639e9447d3 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Mon, 12 Feb 2018 17:45:44 -0800 Subject: [PATCH 08/19] Add match letter --- word_guess.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/word_guess.rb b/word_guess.rb index b133ddb..4990155 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -103,6 +103,9 @@ def guessed_letter return guessed_letter = gets.chomp end + def match_letter + return @word.include?guessed_letter + end def play_game theme = pick_theme @word = get_theme_word theme From 667ffbbf6cfc5725c2d54f49c6d84eb8b2cd2f34 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 11:35:50 -0800 Subject: [PATCH 09/19] Add update board method --- word_guess.rb | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/word_guess.rb b/word_guess.rb index 4990155..0179119 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -2,7 +2,7 @@ require 'colorize' class Game - attr_reader :word, :board, :attempts, :themes + attr_accessor :word, :board, :attempts, :themes def initialize @word = "" @@ -100,17 +100,48 @@ def show_flower def guessed_letter puts "Please pick a letter (a-z). Your attempts_left are #{@attempts_left}" - return guessed_letter = gets.chomp + letter = gets.chomp + return letter end - def match_letter - return @word.include?guessed_letter + def match_letter letter_guessed + return @word.downcase.include?letter_guessed end + + def update_board letter_guessed + # @word.split("").each_index + word_split = @word.split("") + get_word_index = word_split.each_index.select {|index| word_split[index].downcase == letter_guessed} + # @board.split("").each_index{} + board_split = @board.split("") + get_word_index.each {|index_element| board_split[index_element] = word_split [index_element]} + @board = board_split.join + end + def play_game theme = pick_theme @word = get_theme_word theme create_board - end + # until @attempts_left == 0 + # display_board + # letter_guessed = guessed_letter + # if match_letter(letter_guessed) + # puts "Right guess" + # update_board(letter_guessed) + # else + # @attempts_left -= 1 + # puts "Sorry! wrong guess" + # end + # check_win + # end + # puts "Sorry, no more attempts left. It was #{@word}" + # end + def check_win + if @board == @word + puts "Yay! you win." + exit + end + end end game = Game.new From 40049dbc95295698f0b42a2835280d0aa8694ea8 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 12:43:21 -0800 Subject: [PATCH 10/19] Add conditional structure to play game --- word_guess.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/word_guess.rb b/word_guess.rb index 0179119..1f8027f 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -122,20 +122,20 @@ def play_game theme = pick_theme @word = get_theme_word theme create_board - # until @attempts_left == 0 - # display_board - # letter_guessed = guessed_letter - # if match_letter(letter_guessed) - # puts "Right guess" - # update_board(letter_guessed) - # else - # @attempts_left -= 1 - # puts "Sorry! wrong guess" - # end - # check_win - # end - # puts "Sorry, no more attempts left. It was #{@word}" - # end + until @attempts_left == 0 + display_board + letter_guessed = guessed_letter + if match_letter(letter_guessed) + puts "Right guess" + update_board(letter_guessed) + else + @attempts_left -= 1 + puts "Sorry! wrong guess" + end + check_win + end + puts "Sorry, no more attempts left. It was #{@word}" + end def check_win if @board == @word @@ -145,3 +145,4 @@ def check_win end end game = Game.new +game.play From fd3799ff8bbe64a8fc5607eb72489a92fae79b3e Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 13:25:45 -0800 Subject: [PATCH 11/19] Initialized used_letters_array --- word_guess.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/word_guess.rb b/word_guess.rb index 1f8027f..34c2e03 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -2,13 +2,14 @@ require 'colorize' class Game - attr_accessor :word, :board, :attempts, :themes + attr_accessor :word, :board, :attempts, :themes, :used_letters_array def initialize @word = "" @board = "" @attempts_left = 5 @themes = [ "music", "food", "science" ] + @used_letters_array = [] end def pick_theme @@ -128,21 +129,30 @@ def play_game if match_letter(letter_guessed) puts "Right guess" update_board(letter_guessed) + if used_letters_array.include?letter_guessed + puts "You have already tried this letter. Used letters: #{@used_letters_array.join(", ")}".red + end + used_letters_array << letter_guessed else - @attempts_left -= 1 - puts "Sorry! wrong guess" + if used_letters_array.include?letter_guessed + puts "You have already tried this letter. Used letters: #{@used_letters_array.join(", ")}".red + else + used_letters_array << letter_guessed + @attempts_left -= 1 + puts "Sorry! wrong guess" + end end check_win end - puts "Sorry, no more attempts left. It was #{@word}" + puts "Sorry, no more attempts left. It was #{@word}".blue end def check_win if @board == @word - puts "Yay! you win." + puts "Yay! you win.".bold exit end end end game = Game.new -game.play +game.play_game From edd556728c95d118d4d101760f6a1f6595a0d283 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 13:50:13 -0800 Subject: [PATCH 12/19] Handle inappropriate user input --- word_guess.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/word_guess.rb b/word_guess.rb index 34c2e03..b5df198 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -101,7 +101,12 @@ def show_flower def guessed_letter puts "Please pick a letter (a-z). Your attempts_left are #{@attempts_left}" - letter = gets.chomp + letter = gets.chomp.downcase + until letter =~ /^[a-zA_Z\s]$/ + puts "That is not a letter!".red + puts "Please pick a letter (a-z). Your attempts_left are #{@attempts_left}" + letter = gets.chomp.downcase + end return letter end From 2f5b8299509e96c30342c3440fd85ba831ceffe5 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 13:58:57 -0800 Subject: [PATCH 13/19] Add include_letter method --- word_guess.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/word_guess.rb b/word_guess.rb index b5df198..0539074 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -124,6 +124,10 @@ def update_board letter_guessed @board = board_split.join end + def include_letter letter_guessed + return used_letters_array.include?letter_guessed + end + def play_game theme = pick_theme @word = get_theme_word theme @@ -134,12 +138,12 @@ def play_game if match_letter(letter_guessed) puts "Right guess" update_board(letter_guessed) - if used_letters_array.include?letter_guessed + if include_letter(letter_guessed) puts "You have already tried this letter. Used letters: #{@used_letters_array.join(", ")}".red end used_letters_array << letter_guessed else - if used_letters_array.include?letter_guessed + if include_letter(letter_guessed) puts "You have already tried this letter. Used letters: #{@used_letters_array.join(", ")}".red else used_letters_array << letter_guessed From 95fe998fe8c4343dabe20cfe76329111783fb04f Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 14:21:39 -0800 Subject: [PATCH 14/19] Add display_letter method --- word_guess.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/word_guess.rb b/word_guess.rb index 0539074..c4b22d1 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -128,6 +128,12 @@ def include_letter letter_guessed return used_letters_array.include?letter_guessed end + def display_letter letter_guessed + if include_letter letter_guessed + puts "You have already tried this letter. Used letters: #{@used_letters_array.join(", ")}".red + end + end + def play_game theme = pick_theme @word = get_theme_word theme @@ -138,18 +144,13 @@ def play_game if match_letter(letter_guessed) puts "Right guess" update_board(letter_guessed) - if include_letter(letter_guessed) - puts "You have already tried this letter. Used letters: #{@used_letters_array.join(", ")}".red - end + display_letter letter_guessed used_letters_array << letter_guessed else - if include_letter(letter_guessed) - puts "You have already tried this letter. Used letters: #{@used_letters_array.join(", ")}".red - else - used_letters_array << letter_guessed + display_letter letter_guessed + used_letters_array << letter_guessed @attempts_left -= 1 - puts "Sorry! wrong guess" - end + puts "Sorry! wrong guess" end check_win end From 4bf0977ec73ea4f287fe3623857e65342abbef73 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 15:03:46 -0800 Subject: [PATCH 15/19] Handle inappropriate user input for theme --- word_guess.rb | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/word_guess.rb b/word_guess.rb index c4b22d1..8efef34 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -8,14 +8,20 @@ def initialize @word = "" @board = "" @attempts_left = 5 - @themes = [ "music", "food", "science" ] + @themes = [ "music", "color", "science" ] @used_letters_array = [] end def pick_theme puts "Choose theme:" puts @themes - chosen_theme = gets.chomp + chosen_theme = gets.chomp.downcase + until chosen_theme == @themes[0] || chosen_theme == @themes[1] || chosen_theme == @themes[2] + puts "That is not valid!".red + puts "Choose theme:" + puts @themes + chosen_theme = gets.chomp + end return chosen_theme end @@ -23,8 +29,8 @@ def get_theme_word theme case theme when "music" return Faker::Music.instrument - when "food" - return Faker::Food.ingredient + when "color" + return Faker::Color.color_name when "science" return Faker::Science.element end @@ -154,15 +160,25 @@ def play_game end check_win end - puts "Sorry, no more attempts left. It was #{@word}".blue + puts "Sorry, no more attempts left. You lost :(. It was #{@word}".blue end def check_win if @board == @word - puts "Yay! you win.".bold + puts "Yay! you win :).".bold exit end end end -game = Game.new -game.play_game + +class Player + attr_reader :name + + def initialize name + @name = name + puts "Are you game #{@name}!".bold + + end +end +game_instance = Game.new +game_instance.play_game From 18907c252bcdc5ecb90dd352813138fc285b3e7a Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 15:26:05 -0800 Subject: [PATCH 16/19] Add player class and instance variable player to Game class --- word_guess.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/word_guess.rb b/word_guess.rb index 8efef34..9e1d742 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -2,9 +2,10 @@ require 'colorize' class Game - attr_accessor :word, :board, :attempts, :themes, :used_letters_array + attr_accessor :word, :board, :attempts, :themes, :used_letters_array, :player - def initialize + def initialize player + @player = player @word = "" @board = "" @attempts_left = 5 @@ -172,13 +173,14 @@ def check_win end class Player - attr_reader :name + attr_accessor :name def initialize name @name = name - puts "Are you game #{@name}!".bold - + puts "Are you game #{@name}!".bold.blue.on_white.blink end end -game_instance = Game.new + +player_instance = Player.new("Anonymous11") +game_instance = Game.new(player_instance) game_instance.play_game From e725bf277775d6ed2e26912c048e11e76e19dcc0 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 16:12:36 -0800 Subject: [PATCH 17/19] Add pick_theme method to class Player --- word_guess.rb | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/word_guess.rb b/word_guess.rb index 9e1d742..b81607c 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -13,18 +13,6 @@ def initialize player @used_letters_array = [] end - def pick_theme - puts "Choose theme:" - puts @themes - chosen_theme = gets.chomp.downcase - until chosen_theme == @themes[0] || chosen_theme == @themes[1] || chosen_theme == @themes[2] - puts "That is not valid!".red - puts "Choose theme:" - puts @themes - chosen_theme = gets.chomp - end - return chosen_theme - end def get_theme_word theme case theme @@ -142,7 +130,7 @@ def display_letter letter_guessed end def play_game - theme = pick_theme + theme = @player.pick_theme(@themes) @word = get_theme_word theme create_board until @attempts_left == 0 @@ -155,12 +143,15 @@ def play_game used_letters_array << letter_guessed else display_letter letter_guessed - used_letters_array << letter_guessed + if !include_letter letter_guessed + used_letters_array << letter_guessed @attempts_left -= 1 + end puts "Sorry! wrong guess" end check_win end + show_flower puts "Sorry, no more attempts left. You lost :(. It was #{@word}".blue end @@ -179,6 +170,20 @@ def initialize name @name = name puts "Are you game #{@name}!".bold.blue.on_white.blink end + + def pick_theme themes + puts "Choose theme:" + puts themes + chosen_theme = gets.chomp.downcase + until chosen_theme == themes[0] || chosen_theme == themes[1] || chosen_theme == themes[2] + puts "That is not valid!".red + puts "Choose theme:" + puts themes + chosen_theme = gets.chomp + end + return chosen_theme + end + end player_instance = Player.new("Anonymous11") From 3fa5fde6c4820f1527548a240b6bafec32dff630 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 16:29:07 -0800 Subject: [PATCH 18/19] Add guessed_letter to class Player --- word_guess.rb | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/word_guess.rb b/word_guess.rb index b81607c..5e2f713 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -36,6 +36,7 @@ def display_board end def show_flower + puts "Used letters: #{@used_letters_array.join(", ")}".bold case @attempts_left when 5 puts """ @@ -94,16 +95,6 @@ def show_flower end end - def guessed_letter - puts "Please pick a letter (a-z). Your attempts_left are #{@attempts_left}" - letter = gets.chomp.downcase - until letter =~ /^[a-zA_Z\s]$/ - puts "That is not a letter!".red - puts "Please pick a letter (a-z). Your attempts_left are #{@attempts_left}" - letter = gets.chomp.downcase - end - return letter - end def match_letter letter_guessed return @word.downcase.include?letter_guessed @@ -125,7 +116,7 @@ def include_letter letter_guessed def display_letter letter_guessed if include_letter letter_guessed - puts "You have already tried this letter. Used letters: #{@used_letters_array.join(", ")}".red + puts "You have already tried this letter. Used letters.".red end end @@ -135,9 +126,9 @@ def play_game create_board until @attempts_left == 0 display_board - letter_guessed = guessed_letter + letter_guessed = @player.guessed_letter(@attempts_left) if match_letter(letter_guessed) - puts "Right guess" + puts "Right guess! :)".magenta update_board(letter_guessed) display_letter letter_guessed used_letters_array << letter_guessed @@ -184,6 +175,18 @@ def pick_theme themes return chosen_theme end + def guessed_letter attempts_left + puts "Please pick a letter (a-z). Your attempts_left are #{attempts_left}" + letter = gets.chomp.downcase + until letter =~ /^[a-zA_Z\s]$/ + puts "That is not a letter!".red + puts "Please pick a letter (a-z). Your attempts_left are #{attempts_left}" + letter = gets.chomp.downcase + end + return letter + end + + end player_instance = Player.new("Anonymous11") From 33e1cb4811e1af506375a00288a9704564f4305e Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Tue, 13 Feb 2018 17:12:55 -0800 Subject: [PATCH 19/19] Add class Image and comments --- word_guess.rb | 225 +++++++++++++++++++++++++++----------------------- 1 file changed, 123 insertions(+), 102 deletions(-) diff --git a/word_guess.rb b/word_guess.rb index 5e2f713..22bcb74 100644 --- a/word_guess.rb +++ b/word_guess.rb @@ -2,9 +2,11 @@ require 'colorize' class Game - attr_accessor :word, :board, :attempts, :themes, :used_letters_array, :player + attr_reader :word, :board, :attempts, :themes, :used_letters_array, :player, :image - def initialize player +# Constructor + def initialize player, image + @image = image @player = player @word = "" @board = "" @@ -13,7 +15,7 @@ def initialize player @used_letters_array = [] end - +# Returns word based on theme def get_theme_word theme case theme when "music" @@ -31,13 +33,125 @@ def create_board end def display_board - show_flower + @image.show_flower @used_letters_array, @attempts_left puts @board end - def show_flower - puts "Used letters: #{@used_letters_array.join(", ")}".bold - case @attempts_left +# Returns boolean value + def match_letter letter_guessed + return @word.downcase.include?letter_guessed + end + + + def update_board letter_guessed + word_split = @word.split("") + # Returns index number of rightly guessed letter + get_word_index = word_split.each_index.select {|index| word_split[index].downcase == letter_guessed} + board_split = @board.split("") + # Fill the board with right letters + get_word_index.each {|index_element| board_split[index_element] = word_split [index_element]} + # Returns a joined string + @board = board_split.join + end + +# Returns boolean value + def include_letter letter_guessed + return used_letters_array.include?letter_guessed + end + + def display_letter letter_guessed + if include_letter letter_guessed + puts "You have already tried this letter.".red + end + end + + def play_game + theme = @player.pick_theme(@themes) + @word = get_theme_word theme + create_board + until @attempts_left == 0 + display_board + letter_guessed = @player.guessed_letter(@attempts_left) + # If lette matches output message, update board. + if match_letter(letter_guessed) + puts "Right guess! :)".magenta + update_board(letter_guessed) + # Show message + display_letter letter_guessed + # Guessed letters are stored in an array + used_letters_array << letter_guessed + else + display_letter letter_guessed + # If does not include letter still push the letter in used letter array and reduce the attempts_left by 1 + if !include_letter letter_guessed + used_letters_array << letter_guessed + @attempts_left -= 1 + end + puts "Sorry! wrong guess" + end + # Declares winner + check_win + end + # Display images based on attempts_left + @image.show_flower @used_letters_array, @attempts_left + puts "Sorry, no more attempts left. You lost :(. It was #{@word}".blue + end + + def check_win + if @board == @word + puts "Yay! you win :).".bold + exit + end + end +end + +class Player + attr_reader :name + + def initialize name + @name = name + puts "Are you game #{@name}!".bold.blue.on_white.blink + end + +# User chooses theme + def pick_theme themes + puts "Choose theme:" + puts themes + chosen_theme = gets.chomp.downcase + # Until valid theme is entered keep asking + until chosen_theme == themes[0] || chosen_theme == themes[1] || chosen_theme == themes[2] + puts "That is not valid!".red + puts "Choose theme:" + puts themes + chosen_theme = gets.chomp + end + return chosen_theme + end + +# Returns letter guessed by user + def guessed_letter attempts_left + puts "Please pick a letter (a-z). Your attempts_left are #{attempts_left}" + letter = gets.chomp.downcase + until letter =~ /^[a-zA_Z\s]$/ + puts "That is not a letter!".red + puts "Please pick a letter (a-z). Your attempts_left are #{attempts_left}" + letter = gets.chomp.downcase + end + return letter + end +end + +class Image + attr_reader :image_type + + def initialize image_type + @image_type = image_type + end + + def show_flower used_letters_array, attempts_left + # Remove duplicates and return a string of each element + puts "Used letters: #{used_letters_array.uniq.join(", ")}".bold + case attempts_left when 5 puts """ (@)(@)(@)(@)(@) @@ -94,101 +208,8 @@ def show_flower """.red end end - - - def match_letter letter_guessed - return @word.downcase.include?letter_guessed - end - - def update_board letter_guessed - # @word.split("").each_index - word_split = @word.split("") - get_word_index = word_split.each_index.select {|index| word_split[index].downcase == letter_guessed} - # @board.split("").each_index{} - board_split = @board.split("") - get_word_index.each {|index_element| board_split[index_element] = word_split [index_element]} - @board = board_split.join - end - - def include_letter letter_guessed - return used_letters_array.include?letter_guessed - end - - def display_letter letter_guessed - if include_letter letter_guessed - puts "You have already tried this letter. Used letters.".red - end - end - - def play_game - theme = @player.pick_theme(@themes) - @word = get_theme_word theme - create_board - until @attempts_left == 0 - display_board - letter_guessed = @player.guessed_letter(@attempts_left) - if match_letter(letter_guessed) - puts "Right guess! :)".magenta - update_board(letter_guessed) - display_letter letter_guessed - used_letters_array << letter_guessed - else - display_letter letter_guessed - if !include_letter letter_guessed - used_letters_array << letter_guessed - @attempts_left -= 1 - end - puts "Sorry! wrong guess" - end - check_win - end - show_flower - puts "Sorry, no more attempts left. You lost :(. It was #{@word}".blue - end - - def check_win - if @board == @word - puts "Yay! you win :).".bold - exit - end - end -end - -class Player - attr_accessor :name - - def initialize name - @name = name - puts "Are you game #{@name}!".bold.blue.on_white.blink - end - - def pick_theme themes - puts "Choose theme:" - puts themes - chosen_theme = gets.chomp.downcase - until chosen_theme == themes[0] || chosen_theme == themes[1] || chosen_theme == themes[2] - puts "That is not valid!".red - puts "Choose theme:" - puts themes - chosen_theme = gets.chomp - end - return chosen_theme - end - - def guessed_letter attempts_left - puts "Please pick a letter (a-z). Your attempts_left are #{attempts_left}" - letter = gets.chomp.downcase - until letter =~ /^[a-zA_Z\s]$/ - puts "That is not a letter!".red - puts "Please pick a letter (a-z). Your attempts_left are #{attempts_left}" - letter = gets.chomp.downcase - end - return letter - end - - end - +image_instance = Image.new("ascii") player_instance = Player.new("Anonymous11") -game_instance = Game.new(player_instance) +game_instance = Game.new(player_instance, image_instance) game_instance.play_game