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
1 change: 1 addition & 0 deletions drm2-hangman/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gem 'random-word'
12 changes: 12 additions & 0 deletions drm2-hangman/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
GEM
specs:
random-word (1.3.0)

PLATFORMS
ruby

DEPENDENCIES
random-word

BUNDLED WITH
1.13.6
73 changes: 73 additions & 0 deletions drm2-hangman/lib/Game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require_relative 'Screen'
require_relative 'WordFactory'

class Game
def initialize
@guesses = []
@misses = []
@words = WordFactory.get_words
@screen = Screen.new
Copy link

Choose a reason for hiding this comment

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

as mentioned below, some attr_accessors would help here.

Copy link
Author

Choose a reason for hiding this comment

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

Most of these are meant to be private, so is there a reason to assign accessors?

Copy link

Choose a reason for hiding this comment

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

Ok, I see what you're thinking. In the code I've seen here, I'd say that most ruby-folks would use attr_readers or attr_accessors whenever they're referring to instance variables, but maybe there is not much reason to in cases like this. I mean, there is always the aspect of changing everything to a custom getter/setter easily, but that honestly doesn't happen very often.

Copy link
Member

Choose a reason for hiding this comment

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

You can define attr_readers and attr_accessors under a call to private. I recommend always using the methods. Ruby has this stupid behavior: @ivar_with_typo #=> nil. Method calls with typos will raise an exception.

end

def run
loop do
state = @screen.get_state @misses.count
lines = show_lines

@screen.write "Game State: "
@screen.write state

@screen.write "Word or Phrase: "
@screen.write lines

unless lines.include? '_'
@screen.write "You Win!"

quit_game
end

if @misses.count == 6
@screen.write "Game Over!"
@screen.write "The word or phrase was: #{@words.map{ |word| word.get_word }.join(' ')}"

quit_game
end

input = @screen.ask_for_input("What is your guess?").downcase

response = check_guess input

@screen.write response if response
end
end

def show_lines
@words.map{ |word|
word.get_lines @guesses
}.join ' '
end

def check_guess guess
quit_game if guess == "quit"

return "Invalid input!" unless input_is_valid? guess

if @guesses.include? guess
return "You already guessed that letter!"
end

@guesses.push guess

@misses.push guess if [email protected]{ |word| word.get_word }.join.include? guess

return nil
end

def input_is_valid? input
return !!(input =~ /[a-z]/i) && (input.length == 1)
Copy link

Choose a reason for hiding this comment

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

standard ruby style is to avoid using the return keyword unless it's an early return

Copy link
Author

Choose a reason for hiding this comment

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

Another thing I've realized with further challenges. Will fix soon. Thanks.

Copy link

Choose a reason for hiding this comment

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

gotcha, cool

end

def quit_game
exit
end
end
86 changes: 86 additions & 0 deletions drm2-hangman/lib/Screen.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
class Screen
def initialize
@states = [ <<'STATE0', <<'STATE1', <<'STATE2', <<'STATE3', <<'STATE4', <<'STATE5', <<'STATE6' ]
+-----+
| |
|
|
|
|
|
+-----------+
STATE0
+-----+
| |
| (x)
|
|
|
|
+-----------+
STATE1
+-----+
| |
| (x)
| |
| |
|
|
+-----------+
STATE2
+-----+
| |
| (x)
| /|
| |
|
|
+-----------+
STATE3
+-----+
| |
| (x)
| /|\
| |
|
|
+-----------+
STATE4
+-----+
| |
| (x)
| /|\
| |
| /
|
+-----------+
STATE5
+-----+
| |
| (x)
| /|\
| |
| / \
|
+-----------+
STATE6
end

# index is entirely based on the number of misses
def get_state index
@states[index]
end
Copy link

Choose a reason for hiding this comment

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

The general practice on our team has been to avoid comments when possible. Here for instance, renaming the variable to from index to number_of_misses would do the trick.


# ask user for input (with or without a question)
def ask_for_input question = nil
write question if question

print "> "
gets.chomp
end

# helper method for writing to the console (in case we want to format the output)
def write text
puts text
end
end
23 changes: 23 additions & 0 deletions drm2-hangman/lib/Word.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Word
def initialize word
@word = word.downcase
end

def has_letter? letter
@word.include? letter.downcase
end

def get_letters
@word.chars
end

def get_word
@word
end
Copy link

Choose a reason for hiding this comment

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

check out attr_reader (and attr_accessor while you're at it) in the ruby documentation.

Copy link
Author

Choose a reason for hiding this comment

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

Once, I finish up the challenges, I'll be going back through to refactor everything. I've been learning quite a bit by going straight through these, and that is one of the things I've picked up on (thanks to RuboCop).


def get_lines guesses
@word.chars.map{ |char|
Copy link

Choose a reason for hiding this comment

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

Once you've got the reader method in place, then @word can become just word here.

(guesses.include? char) ? char : '_'
}.join
end
end
13 changes: 13 additions & 0 deletions drm2-hangman/lib/WordFactory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative 'Word'
require 'random-word'

class WordFactory
# return a random word or phrase
def self.get_words
randoms = (RandomWord.nouns.next).split '_'

randoms.map do |word|
Word.new(word)
end
end
end
5 changes: 5 additions & 0 deletions drm2-hangman/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require_relative 'lib/Game'

game = Game.new

game.run