Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

&&: Brittany Jones and Monalisa's word guess #12

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 215 additions & 0 deletions word_guess.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
require 'faker'
require 'colorize'

class Game
attr_reader :word, :board, :attempts, :themes, :used_letters_array, :player, :image

# Constructor
def initialize player, image
@image = image
@player = player
@word = ""
@board = ""
@attempts_left = 5
@themes = [ "music", "color", "science" ]
@used_letters_array = []
end

# Returns word based on theme
def get_theme_word theme

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, a theme!

case theme
when "music"
return Faker::Music.instrument
when "color"
return Faker::Color.color_name
when "science"
return Faker::Science.element
end
end

def create_board
word_size = @word.length
@board = "-" * word_size
end

def display_board
@image.show_flower @used_letters_array, @attempts_left
puts @board
end

# 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}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clever use of Enumerables.

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]$/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good defensive programming, good use of a regex!

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have this instance variable? What purpose does it serve?

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 """
(@)(@)(@)(@)(@)
,\,\,|,/,/,
_\|/_
|_____|
| |
|___|
""".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
end
image_instance = Image.new("ascii")
player_instance = Player.new("Anonymous11")
game_instance = Game.new(player_instance, image_instance)
game_instance.play_game