-
Notifications
You must be signed in to change notification settings - Fork 31
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
nlegg
wants to merge
5
commits into
paircolumbus:master
Choose a base branch
from
nlegg:nlegg
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hangman! #14
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
@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 | ||
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. 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 = '' | ||
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. 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cats | ||
dogs | ||
moose | ||
geese | ||
horses | ||
elephants | ||
tarsiers | ||
armadillos |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
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.
word
is referred to by the controller for thefinish
method. Should I handle that some other way?