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

My solution #28

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
40 changes: 40 additions & 0 deletions Eriks-Game/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require_relative 'view'
require_relative 'model'

class WallController
include WallView

def run!
wall = Wall.new
drinker = Drinker.new

Print::run_spinner
Print::title_screen

loop do
Print::menu
case Print::fetch_user_input
when "c"
Print::count_bottles(wall.bottles)
when "a"
wall.add_bottle(Print::serialize_bottle)
when "d"
if wall.empty?
Print::empty_message
exit
end
wall.drink_bottle(Print::drink_id(wall.bottles).to_i, drinker)
if drinker.drunk?
Print::drunk_message
end
when "q"
puts "Goodbye!"
exit
else
Print::error_message
end
end
end
end

WallController.new.run!
63 changes: 63 additions & 0 deletions Eriks-Game/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'faker'
require 'pry'

class Bottle
attr_reader :id, :title, :description, :completed

def initialize args
@id = args[:id]
@title = args[:title]
@description = args[:description]
end
end

class Wall
attr_reader :bottles

def initialize
@primary_id = 0
@bottles = []
populate_dummy_bottles
end

def add_bottle(input)
@bottles << Bottle.new(input.merge(fetch_id))
end

def drink_bottle(bottleId, drinker)
@bottles.delete_if { |n| n.id == bottleId }
drinker.drink()
end

def populate_dummy_bottles
5.times do
add_bottle(title: Faker::Lorem.word, description: Faker::Lorem.sentence)
end
end

def empty?
@bottles.count == 0
end

private

def fetch_id
{id: @primary_id += 1 }
end
end

class Drinker
attr_reader :intoxication

def initialize
@intoxication = 0
end

def drink
@intoxication += 1
end

def drunk?
@intoxication > 3
end
end
83 changes: 83 additions & 0 deletions Eriks-Game/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module WallView

module Print

class << self
def run_spinner
print "Ubering to the bar (please wait) "
5.times { print "."; sleep 1; }
print "\n"
end

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

def title_screen
title = <<TITLE

********** || **********
* 99 Bottles *
********** || **********

TITLE
puts title
end

def menu
menu = <<EOS

***** Welcome *****
- (C)ount the bottles
- (A)dd a bottle
- (D)rink a bottle
- (Q)uit this awful game
***** *****

EOS
puts menu
end

#does print_list know too much about the underlying object???

def count_bottles(bottles)
puts "There are #{bottles.count} bottles on the wall."
end

def serialize_bottle
{}.tap do |obj|
["\nEnter the title:", "\nEnter the description:"].each do |t|
if obj.empty?
obj[:title] = fetch_user_input(t)
else
obj[:description] = fetch_user_input(t)
end
end
end
end

def drink_id(bottles)
chosen_bottle = bottles.sample

puts "\nDrinking bottle #{chosen_bottle.id}"
puts "\nThis is a delicious bottle of #{chosen_bottle.title}"
puts "\nCritics describe this beer as \"#{chosen_bottle.description}\""
chosen_bottle.id
end

def fetch_user_input(question=nil)
puts question if question
print "> "
gets.chomp.downcase
end

def drunk_message
puts "Don't get too intoxicated!"
end

def empty_message
puts "Sorry, time to head home. The wall is empty"
end
end
end
end