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
194 changes: 194 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
# Word Game


# Intro to user
# selects difficulty ?
# word bank -
# maybe in its own class maynot,
# maybe one word bank depending difficulty?
# Find word in word bank.


# Display game board to user
# Image/ASCII art
# Blank letter spaces
# Letters already guessed?
# Ask user to guess a letter
# Store the user input
# CHeck array for guessed
# If it's in array ask for another guess.
# If not then store in array.
# check if guess is in word.
# Update screen accordingly.
# Add letter
# take flower away (tries).
# Loop ends:
# If tries run out.
# Show a losing screed
# Or if whole word is guessed.
# Show a winning screen.

# Ask user if they want to play again.
# if yes then loop to begining of Game
# Or no then display a thank and goodbye.

###################################
# Word Game
###################################
# require "Faker"
require_relative "word_bank"

class Board

attr_accessor :letters_guessed, :tries_remaining
attr_reader :word, :blank

def initialize(word)
@word = word
@blank = "_" * @word.length
@letters_guessed = []
@tries_remaining = 4
@tries_pictures = [
"
_,
.-'_|
_| (_|
(_|
",
"
_,
.-'_| ,
_| (_| _|\\
(_| (_|
",
"
_, _,
.-'_| , .-'_|
_| (_| _|\\ _| (_|
(_| (_| (_|
",
"
_, _,
.-'_| , .-'_| ,
_| (_| _|\\ _| (_| _|\\
(_| (_| (_| (_|
"
]
end

def puts_pictures
puts @tries_pictures
end

def display
output = ""
output += @tries_pictures[@tries_remaining-1] + "\n"
output += @blank + "\n"
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 you've rolled all this up into an instance method. Very concise.

output += "Letters Guessed: #{@letters_guessed.join(' ')}"
return output
end

# def add_letter(guess)
# letter_location = @word.index(guess)
# @blank[letter_location] = guess
# end

def add_letter(guess)
letter_location = []
# word = "coffee"
@word.split('').each_with_index do |letter,index|
# puts "checking #{letter}, index is #{index}"
# puts "checking if letter: #{letter} is guess: #{guess}"
if letter == guess
# puts "#{letter} is #{guess} at index #{index}"
letter_location << index
# puts "letter_location is now #{letter_location}"
end
end
letter_location.each do |index|
@blank[index] = guess
end
end

end


# Intro to user
puts "Welcome to Kate and Angela's Word Game!"

# selects difficulty ?
# word bank -
word_bank = initialize_word_bank()
# maybe in its own class maynot,
# maybe one word bank depending difficulty?
# Find word in word bank.
# game_word = word_bank.sample

game_board = Board.new(word_bank.sample.upcase)

# Display game board to user
# Image/ASCII art -
# "/////////*"
# Blank letter spaces
# Letters already guessed?
#


user_has_won = !(game_board.blank.include?"_")
user_has_lost = game_board.tries_remaining == 0

until (user_has_won || user_has_lost)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you make these calculations for user_has_won and user_has_lost multiple times, they might be a good candidate for an instance method on Board.


puts game_board.display

# Ask user to guess a letter

puts "Please guess a letter: "
guess = gets.chomp.upcase
if game_board.letters_guessed.include?guess
puts "You've already guessed that!"
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 logic all looks good, but the code might be a little cleaner if it were all wrapped up into its own method. Maybe call it something like take_turn.

else
game_board.letters_guessed << guess
game_board.letters_guessed.sort!
if game_board.word.include?guess
game_board.add_letter(guess)
else
puts "Wrong!"
game_board.tries_remaining -= 1
end
end

user_has_won = !(game_board.blank.include?"_")
user_has_lost = game_board.tries_remaining == 0
end

# Check if this is a letter????

# Store the user input
# CHeck array for guessed
# If it's in array ask for another guess.
# If not then store in array.
# check if guess is in word.
# Update screen accordingly.
# Add letter
# take flower away (tries).

# Loop ends:
# If tries run out.
# Show a losing screed
# Or if whole word is guessed.
# Show a winning screen.

if user_has_won
puts game_board.display
puts "You win!!!!"
end

if user_has_lost
puts "Sorry! You lost :("
puts "The word was #{game_board.word}!"
end

# Ask user if they want to play again.
# if yes then loop to begining of Game
# Or no then display a thank and goodbye.
124 changes: 124 additions & 0 deletions word_bank.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
def initialize_word_bank
return ["ear",
"heart",
"irritate",
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 love that you've separated your words out into a separate file. This makes it very clear which part of the program is which, and the reader doesn't have to scroll through a hundred lines of words at the top of your file.

"lyrical",
"moor",
"copy",
"natural",
"sedate",
"dramatic",
"bruise",
"run",
"save",
"late",
"advise",
"subdued",
"class",
"release",
"protest",
"sleepy",
"dizzy",
"afraid",
"muddle",
"hissing",
"accurate",
"snake",
"doctor",
"film",
"humdrum",
"miscreant",
"damage",
"arch",
"grandiose",
"dusty",
"tan",
"include",
"bustling",
"inform",
"sofa",
"writer",
"sulky",
"strip",
"outstanding",
"subtract",
"boiling",
"shelf",
"suggest",
"wreck",
"brake",
"sprout",
"acid",
"attach",
"auspicious",
"hole",
"insect",
"excited",
"reproduce",
"encourage",
"grape",
"oceanic",
"drain",
"angry",
"ticket",
"glove",
"stick",
"winter",
"whip",
"lying",
"typical",
"ablaze",
"separate",
"shop",
"nine",
"craven",
"strengthen",
"coherent",
"rub",
"birthday",
"railway",
"ruin",
"separate",
"coat",
"verdant",
"warlike",
"puny",
"friend",
"elegant",
"quince",
"glass",
"courageous",
"cemetery",
"well-to-do",
"bump",
"store",
"thaw",
"abounding",
"press",
"liquid",
"own",
"dazzling",
"resonant",
"workable",
"unique",
"sloppy",
"tame",
"line",
"superficial",
"box",
"witty",
"alarm",
"faint",
"rebel",
"spurious",
"announce",
"murky",
"walk",
"sand",
"zoom",
"reaction",
"grass",
"knowing",
"reminiscent",
"overjoyed"]
end