-
Notifications
You must be signed in to change notification settings - Fork 27
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
Ampers: Abinnet, Alex #19
base: master
Are you sure you want to change the base?
Changes from all commits
f2bb51c
8f105e7
4e1b97c
4b26beb
682d710
bfcc378
1b8da73
dd1f571
9c986fa
8f1b90e
89e4372
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
require 'faker' #comment ln:50 && uncomment ln:51 for harry potter theme | ||
require 'random_word_generator' | ||
require 'colorize' | ||
|
||
# main class which initiates games and calls other classes based on user_response | ||
class GamePlay | ||
attr_accessor :play, :wrong_guesses, :progress | ||
|
||
def initialize(play) | ||
@play = play | ||
end | ||
|
||
def quit_or_play_game | ||
if @play == "n" | ||
puts "\nGoodbye!" | ||
exit | ||
elsif @play == "y" | ||
new_word = GenerateWord.new | ||
word_array = new_word.create_fake_word | ||
user_guess = Guess.new(word_array) | ||
picture = Display.new(6) | ||
picture.print_flower | ||
|
||
while @play == "y" | ||
print "Word: #{user_guess.progress}\n" | ||
puts "Incorrect guesses: #{user_guess.wrong_guesses.join(" ")}" | ||
|
||
if word_array.join(" ") == user_guess.progress | ||
puts "\nCongrats! You win!" | ||
exit | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error in indentation |
||
|
||
print "Enter letter > " | ||
letter = gets.chomp.downcase | ||
user_guess.compare(letter) | ||
end | ||
end | ||
end | ||
end | ||
|
||
# creates a random word for the user to guess | ||
class GenerateWord | ||
attr_accessor :word | ||
|
||
def initialize | ||
@word = word | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this accomplishing?
And have |
||
end | ||
|
||
def create_fake_word | ||
@word = RandomWordGenerator.word | ||
#@word = Faker::HarryPotter.character | ||
@word = @word.split("") | ||
return @word | ||
end | ||
|
||
end | ||
|
||
class Guess | ||
attr_accessor :letter, :word, :progress, :wrong_guesses | ||
|
||
def initialize(word) | ||
@word = word | ||
progress = [] | ||
@wrong_guesses = [] | ||
@word.length.times do | ||
progress.push("_") | ||
end | ||
@progress = progress.join(" ") | ||
end | ||
|
||
def compare(letter) | ||
@letter = letter | ||
if @letter == "quit" | ||
quit_game = GamePlay.new("n") | ||
quit_game.quit_or_play_game | ||
elsif @word.include?(@letter) | ||
@progress = progress.split(" ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clever use of spaces (" ") to separate the underscores and removing them before you search for the letter. This is a bit inefficient, because each deletion and insertion requires large portions of the array to be updated, but for small words it's reasonable. |
||
@word.each_with_index do |value, index| | ||
if value == @letter | ||
@progress.delete_at(index) | ||
@progress.insert(index, @letter) | ||
end | ||
end | ||
@progress = progress.join(" ") | ||
else | ||
@wrong_guesses.push(@letter) | ||
end | ||
|
||
flowers_remaining = 6 - @wrong_guesses.length | ||
flower_points = Display.new(flowers_remaining) | ||
|
||
if flowers_remaining > 0 | ||
puts flower_points.print_flower | ||
else | ||
puts "\nNo more guesss! Game over! \n" | ||
@word = @word.join("") | ||
puts "The word was: #@word" | ||
exit | ||
end | ||
end | ||
end | ||
|
||
class Display | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class is indented, it shouldn't be. |
||
attr_accessor :flowers, :flowers_remaining | ||
|
||
def initialize(flowers_remaining) | ||
@flowers_remaining = flowers_remaining | ||
@flower = "(@)".colorize(:red) | ||
end | ||
|
||
def print_flower | ||
puts "\n" | ||
@flowers_remaining.times { |i| print "#{@flower}" } | ||
puts | ||
puts " \\,\\,\\,|,/,/,/".colorize(:green) | ||
puts " \\_\\__|__/_/ ".colorize(:green) | ||
puts " |________| ".colorize(:blue) | ||
puts " | Roses | ".colorize(:blue) | ||
puts " |________| ".colorize(:blue) | ||
puts | ||
end | ||
end | ||
|
||
# prompts user | ||
puts "Would you like to play Hangman?" | ||
puts "- Type 'Y' for yes" | ||
puts "- Yype 'N' for no" | ||
puts "- Type 'Quit' to exit game\n" | ||
print "\nEnter selection > " | ||
user_response = gets.chomp.downcase | ||
|
||
initiate = GamePlay.new(user_response) | ||
initiate.quit_or_play_game |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting that you found this gem, nice work!