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

Jack Chester Solutions #29

Open
wants to merge 1 commit 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
Binary file added JacksGame/.DS_Store
Binary file not shown.
41 changes: 41 additions & 0 deletions JacksGame/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative 'view'
require_relative 'model'

class Control
include GameView
Copy link
Member

Choose a reason for hiding this comment

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

2 spaces.


def run!
Print::title_screen
Copy link
Member

Choose a reason for hiding this comment

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

2 spaces :)


loop do
Print::menu
case Print::get_input
when "1"
name = get_input("What's your Grandma's name?")
talk_to_gma(name)
when "2"
Print::show_instructions
when "3"
exit
else
Print::error_message
end
end
end

def talk_to_gma(name)
grandma = DeafGrandma.new(name)
loop do
phrase = get_input("Say something to Grandma:")
grandma.converse(phrase)
end
end

def get_input(prompt=nil)
puts prompt if prompt
print "> "
gets.chomp
end
end

Control.new.run!
46 changes: 46 additions & 0 deletions JacksGame/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class DeafGrandma
attr_reader :age, :name
attr_accessor :asleep, :sleep_count

def initialize(name)
@name = name
@age = 100
@asleep = false
@sleep_count = 0
end

def converse(input)

if input.upcase == input
if @asleep
Copy link
Member

Choose a reason for hiding this comment

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

Why not use the reader method?

@sleep_count += 1
if @sleep_count == 3
@asleep = false
Copy link
Member

Choose a reason for hiding this comment

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

Keep in mind that here you're calling the instance variable directly. If you use self.asleep then you send a message to the object instead which gives you more flexibility in the long run if the definition of asleep were to change.

def asleep
  in_bed && tucked_in
end

Copy link

Choose a reason for hiding this comment

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

Would it be correct to say this is akin to solely using the accessors (getX/setX) of certain other languages?

Honestly, it is also a bit nice to not see @ all over the place (I realized this when I started my MVCgame implementation) though that's a stylistic thing. Reminds me of CoffeeScript syntax though the details are pretty different.

@sleep_count = 0
puts "*Grandma " + @name + " jolts awake!*"
puts "OH HI THERE SONNY!"
else
puts "*Grandma " + @name + " moves slightly in her sleep*"
end
elsif input == "EAT, GRANDMA!"
puts "MMMM PORIDGE!"
puts "*snores*"
@asleep = true
elsif input == "HOW OLD ARE YOU?"
puts "I'M 100 YEARS OLD SONNY!"
elsif input == "BYE"
puts "SEE YOU SONNY!"
exit
else
puts "NOT SINCE 1964!"
end
else
if @asleep
puts "*Grandma " + @name + " snores loudly*"
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 also use interpolation over concatenation.

http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html

else
puts "WHAT WAS THAT SONNY?"
end
end
end
end

58 changes: 58 additions & 0 deletions JacksGame/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module GameView

module Print

class <<self
Copy link
Member

Choose a reason for hiding this comment

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

  • Do you know what's happening here? Check out the book Metaprogramming Ruby 2.
  • Why might you use it?


def error_message
puts "Invalid command, try again!"
end

def title_screen
title = <<TITLE

************ || **************
* Ultimate Grandma Simulator *
************ || **************

"Just like the real thing!"

TITLE
puts title
end

def menu
menu = <<EOS

********* Menu *********
- Say hi to Grandma! (1)
- How to play (2)
- Exit (3)
********* *********

EOS
puts menu
end

def show_instructions
instructions = <<INS

Talk to Grandma any time you want! Be sure to give her a good name.

Grandma is a little hard of hearing, so be sure to speak up!

Make sure Grandma gets the food she needs, tell her "EAT, GRANDMA!"
but watch out, food puts her to sleep! Yell three times to wake her up.

INS
puts instructions
end

def get_input(prompt=nil)
puts prompt if prompt
print "> "
gets.chomp
end
end
end
end