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

Hangman! #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
38 changes: 38 additions & 0 deletions Nicks-Game/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative 'view'
require_relative 'model'

class GameController
include GameView

def run!
hangman = Hangman.new

Print::run_spinner
Print::title_screen

loop do
Print::menu
case Print::fetch_user_input
when "P"
hangman.reset
Print::play(hangman.play)
loop do
hangman.guess(Print::fetch_user_input)
if hangman.done
Print::finish(hangman.word)
break
else
Print::play(hangman.play)
end
end
when "Q"
puts "We're done"
exit
else
Print::error_message
end
end
end
end

GameController.new.run!
46 changes: 46 additions & 0 deletions Nicks-Game/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Hangman
attr_reader :word, :guessed
MAX_TRIES = 10

def initialize
@word = File.readlines('words.txt').sample.strip.upcase
Copy link
Member

Choose a reason for hiding this comment

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

You only ever use @word.chars. Might as well append a .chars and rename this @word_chars.

The Law of Demeter is more of a suggestion.

Copy link
Author

Choose a reason for hiding this comment

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

word is referred to by the controller for the finish method. Should I handle that some other way?

@letters_left = @word.dup
@guessed = []
@tries = 0
end

# apply a user's guessed character
def guess(char)
@guessed.push(char)
@tries += 1
@letters_left.delete! char
end

# is the game over?
def done
@tries >= MAX_TRIES || guessmatch
end

# do the user's combined guesses reveal the secret word?
def guessmatch
@letters_left.empty?
end

# start a new game
def reset
initialize
Copy link
Member

Choose a reason for hiding this comment

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

I don't think I've ever done this before. I like the use case.

end

# sends hash of gamestate (string, tries) through controller to view
def play
playString = ''
Copy link
Member

Choose a reason for hiding this comment

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

lower snakecase is the common convention.

word.chars.each do |letter|
if guessed.include?(letter)
playString += letter + ' '
else
playString += '_ '
end
end
{ playString: playString, tries: MAX_TRIES - @tries }
end
end
57 changes: 57 additions & 0 deletions Nicks-Game/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module GameView

module Print

class << self
def run_spinner
print "Loading (please wait) "
5.times { print "."; sleep 0.5; }
print "\n"
end

def error_message
puts "That's not a command key. Try again!"
end

def title_screen
title = <<TITLE

********** || **********
* Hangman *
********** || **********

TITLE
puts title
end

def menu
menu = <<EOS

***** Welcome *****
- (P)lay
- (Q)uit
***** *****

EOS
puts menu
end

def play(gamestate)
puts "Guess a letter (#{gamestate[:tries]} tries remaining) (ctrl-c to quit):"
puts gamestate[:playString]
end

def finish(word)
puts "The word was #{word}!"
puts "Thanks for playing!"
Print::run_spinner
end

def fetch_user_input(question=nil)
puts question if question
print "> "
gets.chomp.upcase
end
end
end
end
8 changes: 8 additions & 0 deletions Nicks-Game/words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cats
dogs
moose
geese
horses
elephants
tarsiers
armadillos