Skip to content
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'http://rubygems.org'

gem 'rake'
gem 'rspec'
gem 'rspec', '~> 2.11.0'
gem 'activesupport'
gem 'activerecord'
gem 'pg'
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ GEM
multi_json (1.3.4)
pg (0.13.2)
rake (0.9.2.2)
rspec (2.10.0)
rspec-core (~> 2.10.0)
rspec-expectations (~> 2.10.0)
rspec-mocks (~> 2.10.0)
rspec-core (2.10.0)
rspec-expectations (2.10.0)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.1)
rspec-expectations (2.11.2)
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1)
rspec-mocks (2.11.1)
tzinfo (0.3.33)

PLATFORMS
Expand All @@ -37,4 +37,4 @@ DEPENDENCIES
activesupport
pg
rake
rspec
rspec (~> 2.11.0)
29 changes: 16 additions & 13 deletions adventure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@
require_relative 'db/setup'
require_relative 'models/page'
require_relative 'models/book'
require_relative 'db/seed'

page = Page.create(starting_point: true, content: "You wake up on a road. It's foggy and dampy. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?")
Page.create(conclusion: true, parent_id: page.id, content: "Go into the forest")
Page.create(conclusion: true, parent_id: page.id, content: "Walk down the road")

page = Page.starting_point
book = Book.new(page)

until book.complete_game? do
puts book.current_page.content
puts "your options: "
puts " - [#{book.current_page.options.first.content}]"
puts " - [#{book.current_page.options.last.content}]"
option_a = Page.find(book.current_page.options.first)
option_b = Page.find(book.current_page.options.last)

puts "Your options: "
puts "A - #{option_a.preview}"
puts "B - #{option_b.preview}"
puts "What do you want to do? Enter A or B"

book.input( gets )

end

puts
puts book.current_page.content
Copy link
Member

Choose a reason for hiding this comment

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

another student did this and I liked it when you wanted empty lines:

puts "\n#{book.current_page.content}\n"

puts

message = book.current_page.winner ? "YOU WIN" : "YOU LOSE"

puts "------------------------------------------"
puts "| |"
puts "| |"
puts "| ADVENTURE COMPLETE |"
puts " #{message} "
puts "| |"
puts "| |"
puts "------------------------------------------"


puts book.current_page.content

puts "hope you won!"
5 changes: 4 additions & 1 deletion db/migrate/001_creates_page.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
class CreatesPage < ActiveRecord::Migration
def change
create_table :pages do |t|
t.text :preview
t.text :content
t.integer :parent_id
t.boolean :starting_point, default: false
t.boolean :conclusion, default: false
t.boolean :winner, default: false
t.integer :option_a_id
t.integer :option_b_id
end
end
end
39 changes: 39 additions & 0 deletions db/seed.rb
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
# Cleaning Out
Page.delete_all

# Child pages
option_c = Page.create(conclusion: true,
content: "You're seeing things. A bear appears and eats you. YOU DEAD!",
preview: "Walk towards the light... your destiny awaits!")

option_d = Page.create(conclusion: true,
content: "It is! You stumble across Bacon Land where bacon is free and everywhere! WIN!",
Copy link
Member

Choose a reason for hiding this comment

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

yay free bacon!

preview: "You smell more bacon... could it be true!?",
winner: true)

option_e = Page.create(conclusion: true,
content: "No it isn't! FAIL. YOU LOSE!",
preview: "You're bacon sandwich it isn't sitting well. Is that a toilet nearby?")
Copy link
Member

Choose a reason for hiding this comment

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

Free bacon: not the best idea, honestly


option_f = Page.create(conclusion: true,
content: "Well what do ya' know?? YOU WIN!",
preview: "A sign appears saying, 'Go this way and you will win big'..",
winner: true)

option_a = Page.create(conclusion: false,
content: "You're rich! Or not... You must continue on your journey to cash your gold pieces in...",
preview: "A long journey awaits, but it could be worth it...",
option_a_id: option_c.id,
option_b_id: option_d.id)

option_b = Page.create(conclusion: false,
content: "You ate the bacon sandwich! Now you're even hungrier! Onwards march...",
preview: "Tired and hungry, this could be just what you need!",
option_a_id: option_e.id,
option_b_id: option_d.id)

# Current page / starting point
page = Page.create(starting_point: true,
content: "You wake up on a road. It's foggy and dampy. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?",
preview: "30 gold pieces would make you rich... But that bacon sandwich smells awfully good!",
option_a_id: option_a.id,
option_b_id: option_b.id)
9 changes: 4 additions & 5 deletions models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ def initialize(starting_page)
end

def input(input_string)
if input_string.chomp == "A"
@current_page = current_page.options.first
elsif input_string.chomp == "B"
@current_page = current_page.options.last
if input_string.chomp.capitalize == "A"
@current_page = Page.where(id: current_page.options.first).first
elsif input_string.chomp.capitalize == "B"
@current_page = Page.where(id: current_page.options.last).first
end
end

def complete_game?
current_page.conclusion?
end

end
3 changes: 1 addition & 2 deletions models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ def self.starting_point
end

def options
Page.where(:parent_id => id).limit(2)
[option_a_id, option_b_id]
end

end
12 changes: 9 additions & 3 deletions spec/book_spec.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
require_relative "spec_helper"

describe Book do
let!(:page) {Page.create(starting_point: true)}
let(:option_a) { Page.create }
let(:option_b) { Page.create }
let!(:page) {Page.create(starting_point: true, option_a_id: option_a.id, option_b_id: option_b.id)}
subject { Book.new(page) }

it "should have a page" do
subject.current_page.should eq(page)
end

describe "#input" do
let!(:option_a) { Page.create(parent_id: page.id)}
let!(:option_b) { Page.create(parent_id: page.id)}

it "should receive input and opens page" do
subject.input("A")
subject.current_page.should eq(option_a)
end

it "should receive input and opens page" do
subject.input("B")
subject.current_page.should eq(option_b)
end

it "should still work with lower case inputs" do
subject.input("a")
subject.current_page.should eq(option_a)
end

end

describe "#complete_game?" do
Expand Down
27 changes: 22 additions & 5 deletions spec/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Page.delete_all
end


it "should know if it's at the end of the road" do
page = Page.create(conclusion: true)
page.conclusion?.should be_true
Expand All @@ -16,14 +17,30 @@
Page.find(page.id).content.should eq("The fox and hound get along")
end

it "has a preview" do
page = Page.create(preview: "A light appears at the end of the road")
Page.find(page.id).preview.should eq("A light appears at the end of the road")
end

it "knows if it's a winning page" do
page = Page.create(winner: true)
page.winner.should be_true
end

it "is not a winner by default" do
Page.create.winner.should eq false
# Page.create.winner.should be_false => this passes when it shouldn't.
# What's the difference with eq false and be_false?
end

context "#options" do
subject {Page.create}
let(:option_a) {Page.create(parent_id: subject.id) }
let(:option_b) {Page.create(parent_id: subject.id) }
let(:option_c) {Page.create(parent_id: subject.id) }

let(:option_a) { Page.create }
let(:option_b) { Page.create }
let(:page) { Page.create(option_a_id: option_a.id, option_b_id: option_b.id) }

it "should have options for the next pages" do
subject.options.should eq([option_a, option_b])
expect(page.options).to eq [option_a.id, option_b.id]
end
end

Expand Down