-
Notifications
You must be signed in to change notification settings - Fork 30
Add personal game #22
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
base: master
Are you sure you want to change the base?
Changes from all commits
43ad199
d45b2bc
bffd710
0268997
a5545ad
34403da
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 @@ | ||
| gem 'random-word' |
| 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 |
| 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 | ||
| 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) | ||
|
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. standard ruby style is to avoid using the
Author
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. Another thing I've realized with further challenges. Will fix soon. Thanks. 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. gotcha, cool |
||
| end | ||
|
|
||
| def quit_game | ||
| exit | ||
| end | ||
| end | ||
| 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 | ||
|
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. The general practice on our team has been to avoid comments when possible. Here for instance, renaming the variable to from |
||
|
|
||
| # 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 | ||
| 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 | ||
|
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. check out
Author
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. 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| | ||
|
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. Once you've got the reader method in place, then |
||
| (guesses.include? char) ? char : '_' | ||
| }.join | ||
| end | ||
| end | ||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| require_relative 'lib/Game' | ||
|
|
||
| game = Game.new | ||
|
|
||
| game.run |
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.
as mentioned below, some
attr_accessorswould help here.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.
Most of these are meant to be private, so is there a reason to assign accessors?
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.
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.
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 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.