Skip to content
Open
Show file tree
Hide file tree
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
276 changes: 276 additions & 0 deletions game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
require 'colorize'
require 'Faker'
class Game

def initialize

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Instance methods should be indented within their class.

@wrong_guesses = []
@answer = Faker::Dessert.variety.downcase
no_spaces = @answer.delete(" ")
no_split = no_spaces.split("")
@one_answer = no_split.join(" ")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I like your word selection!

@hidden_answer = @one_answer.gsub(/[a-z]/, '_')
@art =[
" _
\/\\ )\\

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This art takes up a large amount of space in your file, making it difficult to read. A good solution to this is to put it into another file, something like art.rb.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That will also let you use Atom's auto-indent functionality without messing up your art.

_ __)_)__ .'`--`'.
)\\_ .-'._'-'_.'-. \/ ^ ^ \\
.'`---`'. .'.' \/o\\'\/o\\ '.'. \\ \\\/\\\/\\\/ \/
\/ <> <> \\ : ._: 0 :_. : \\ '------' _J_
| A |: \\\\\/\\_\/\\\/\/ : | _\/)_ .'`---`'.
\\ <\\_\/> \/ : :\\\/\\_\/\\\/: : \/ .'`----`'.\/.'0\\ 0\\ \\
_?_._`\"`_.'`'-:__:__:__:__:-' \/.'<\\ \/> \\: o |
.'`---`'.`` _\/( \/\\ |:,___A___,|' V===V \/
\/.'a . a \\.'`---`'. __(_(__ \\' \\_____\/ \/'._____.'
|: ___ \/.'\/\\ \/\\ \\ .-'._'-'_.'-:.______.' _?_
\\' \\_\/ |: ^ | .'.' (o\\'\/o) '.'. .'`\"\"\"`'.
'._____.'\\' 'vvv' \/ \/ :_\/_: A :_\\_: \\ \/ ^.^ \\
'.__.__.' | : \\'=...='\/ : | \\ `===` \/
\\ : :'.___.': : \/ `-------`
'-:__:__:__:__:-'

",
"
\/\\
_ __)_)__
)\\_ .-'._'-'_.'-.
.'`---`'. .'.' \/o\\'\/o\\ '.'.
\/ <> <> \\ : ._: 0 :_. : \\ _J_
| A |: \\\\\/\\_\/\\\/\/ : | _\/)_ .'`---`'.
\\ <\\_\/> \/ : :\\\/\\_\/\\\/: : \/ .'`----`'.\/.'0\\ 0\\ \\
_?_._`\"`_.'`'-:__:__:__:__:-' \/.'<\\ \/> \\: o |
.'`---`'.`` _\/( \/\\ |:,___A___,|' V===V \/
\/.'a . a \\.'`---`'. __(_(__ \\' \\_____\/ \/'._____.'
|: ___ \/.'\/\\ \/\\ \\ .-'._'-'_.'-:.______.' _?_
\\' \\_\/ |: ^ | .'.' (o\\'\/o) '.'. .'`\"\"\"`'.
'._____.'\\' 'vvv' \/ \/ :_\/_: A :_\\_: \\ \/ ^.^ \\
'.__.__.' | : \\'=...='\/ : | \\ `===` \/
\\ : :'.___.': : \/ `-------`
'-:__:__:__:__:-'

",
"
\/\\
_ __)_)__
)\\_ .-'._'-'_.'-.
.'`---`'. .'.' \/o\\'\/o\\ '.'.
\/ <> <> \\ : ._: 0 :_. : \\ _J_
| A |: \\\\\/\\_\/\\\/\/ : | _\/)_ .'`---`'.
\\ <\\_\/> \/ : :\\\/\\_\/\\\/: : \/ .'`----`'.\/.'0\\ 0\\ \\
'._`\"`_.'`'-:__:__:__:__:-' \/.'<\\ \/> \\: o |
`` _\/( \/\\ |:,___A___,|' V===V \/
.'`---`'. __(_(__ \\' \\_____\/ \/'._____.'
\/.'\/\\ \/\\ \\ .-'._'-'_.'-:.______.' _?_
|: ^ | .'.' (o\\'\/o) '.'. .'`\"\"\"`'.
\\' 'vvv' \/ \/ :_\/_: A :_\\_: \\ \/ ^.^ \\
'.__.__.' | : \\'=...='\/ : | \\ `===` \/
\\ : :'.___.': : \/ `-------`
'-:__:__:__:__:-'

",
"
\/\\
_ __)_)__
)\\_ .-'._'-'_.'-.
.'`---`'. .'.' \/o\\'\/o\\ '.'.
\/ <> <> \\ : ._: 0 :_. : \\
| A |: \\\\\/\\_\/\\\/\/ : | _\/)_
\\ <\\_\/> \/ : :\\\/\\_\/\\\/: : \/ .'`----`'.
'._`\"`_.'`'-:__:__:__:__:-' \/.'<\\ \/> \\
`` _\/( \/\\ |:,___A___,|
.'`---`'. __(_(__ \\' \\_____\/ \/
\/.'\/\\ \/\\ \\ .-'._'-'_.'-:.______.' _?_
|: ^ | .'.' (o\\'\/o) '.'. .'`\"\"\"`'.
\\' 'vvv' \/ \/ :_\/_: A :_\\_: \\ \/ ^.^ \\
'.__.__.' | : \\'=...='\/ : | \\ `===` \/
\\ : :'.___.': : \/ `-------`
'-:__:__:__:__:-'

",
"
\/\\
__)_)__
.-'._'-'_.'-.
.'.' \/o\\'\/o\\ '.'.
\/ : ._: 0 :_. : \\
| : \\\\\/\\_\/\\\/\/ : | _\/)_
\\ : :\\\/\\_\/\\\/: : \/ .'`----`'.
'-:__:__:__:__:-' \/.'<\\ \/> \\
_\/( \/\\ |:,___A___,|
.'`---`'. __(_(__ \\' \\_____\/ \/
\/.'\/\\ \/\\ \\ .-'._'-'_.'-:.______.' _?_
|: ^ | .'.' (o\\'\/o) '.'. .'`\"\"\"`'.
\\' 'vvv' \/ \/ :_\/_: A :_\\_: \\ \/ ^.^ \\
'.__.__.' | : \\'=...='\/ : | \\ `===` \/
\\ : :'.___.': : \/ `-------`
'-:__:__:__:__:-'

",
"
\/\\
__)_)__
.-'._'-'_.'-.
.'.' \/o\\'\/o\\ '.'.
\/ : ._: 0 :_. : \\
| : \\\\\/\\_\/\\\/\/ : | _\/)_
\\ : :\\\/\\_\/\\\/: : \/ .'`----`'.
'-:__:__:__:__:-' \/.'<\\ \/> \\
_\/( \/\\ |:,___A___,|
.'`---`'. __(_(__ \\' \\_____\/ \/
\/.'\/\\ \/\\ \\ .-'._'-'_.'-:.______.'
|: ^ | .'.' (o\\'\/o) '.'.
\\' 'vvv' \/ \/ :_\/_: A :_\\_: \\
'.__.__.' | : \\'=...='\/ : |
\\ : :'.___.': : \/
'-:__:__:__:__:-'

",
"
\/\\
__)_)__
.-'._'-'_.'-.
.'.' \/o\\'\/o\\ '.'.
\/ : ._: 0 :_. : \\
| : \\\\\/\\_\/\\\/\/ : | _\/)_
\\ : :\\\/\\_\/\\\/: : \/ .'`----`'.
'-:__:__:__:__:-' \/.'<\\ \/> \\
\/\\ |:,___A___,|
__(_(__ \\' \\_____\/ \/
.-'._'-'_.'-:.______.'
.'.' (o\\'\/o) '.'.
\/ :_\/_: A :_\\_: \\
| : \\'=...='\/ : |
\\ : :'.___.': : \/
'-:__:__:__:__:-'

",
"





_\/)_
.'`----`'.
\/.'<\\ \/> \\
\/\\ |:,___A___,|
__(_(__ \\' \\_____\/ \/
.-'._'-'_.'-:.______.'
.'.' (o\\'\/o) '.'.
\/ :_\/_: A :_\\_: \\
| : \\'=...='\/ : |
\\ : :'.___.': : \/
'-:__:__:__:__:-'

",
"








\/\\
__(_(__
.-'._'-'_.'-:.
.'.' (o\\'\/o) '.'.
\/ :_\/_: A :_\\_: \\
| : \\'=...='\/ : |
\\ : :'.___.': : \/
'-:__:__:__:__:-'

",
"

















"]
@win = false

puts "Start guessing!\n\n#{@art[0]}\n\n#{@hidden_answer}"
end

def guess_letter(guess)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I like that handling a guess is its own method, but it ends up being pretty complicated. Would it be possible to split it further into multiple small methods? Might make it easier to work with.

guess_result = @answer.index(guess)
if guess_result == nil # negative result

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Watch indentation here.

@wrong_guesses << guess unless @wrong_guesses.include?(guess)
if @wrong_guesses.length == 9
game_result
else
puts "\n\nOops, try again!\n\n"
show_guesses
change_art
puts @hidden_answer
end
elsif guess_result >= 0 # positive result
puts "\n\nNice guess!\n"
answer_array = @one_answer.split('')
puts answer_array
matching_indices = answer_array.each_index.select {|i| answer_array[i] == guess }
matching_indices.each do |i|
replacement_letter = i

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of enumerables to solve this problem.

@hidden_answer[replacement_letter] = guess
end

if @hidden_answer.match(/[\_]/) == nil && @wrong_guesses.length <= 9
change_art
puts @hidden_answer
@win = true
game_result
else
show_guesses
change_art
puts @hidden_answer
end
end
end

def change_art
select_art = @wrong_guesses.length
puts "\n#{@art[select_art].red}"
end

def show_guesses
@wrong_guesses.each do |letter|
print "#{letter}"
end
end

def game_result
if @win == true
puts "\n\nYOU WIN!\n\n#{@answer}"
exit
else
puts "\n\nYOU LOSE! :(\n\n#{@answer}"
exit
end
end

def red
colorize(31)
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Where are these methods used?


def yellow
colorize(33)
end

def blue
colorize(34)
end

end
32 changes: 32 additions & 0 deletions word_guess.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative 'game'

puts "Do you want to play word guess? (Y or N)"

game_answer = gets.chomp.upcase

until game_answer == 'Y' || game_answer == 'N'
puts "That's an invalid answer"
puts "Do you want to play word guess? (Y or N)"
game_answer = gets.chomp.upcase
end

if game_answer == 'Y'
new_game = Game.new
elsif game_answer == 'N'
exit
end

loop do
puts "Guess a letter"
letter = gets.chomp.downcase
letter = letter[0]
check = /^[A-z]+$/.match(letter)
if check == nil
puts "that is an incorrect input, please try again"
else
new_game.guess_letter(letter)
end

end