Skip to content

Commit

Permalink
Finished main functionality. Next I'll polish the story and features.
Browse files Browse the repository at this point in the history
  • Loading branch information
ascot21 committed Oct 17, 2012
1 parent 7e393d3 commit 459e4e9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 100 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Overview
========
A Ruby command line game based on widely popular series of children's books. The game is from a second-person point of view, with the reader assuming the role of the protagonist and making choices that determine the main character's actions and the plot's outcome.

Story loosely based on "The Cave of Time" book. See a [graph of the paths](/doc/story_chart.png).
Story loosely based on "The Cave of Time" book. See doc/story_chart.png for an overview.


Features
Expand Down
5 changes: 3 additions & 2 deletions lib/fork.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
class Fork

attr_reader :id, :story, :path1, :path2, :finish
attr_reader :id, :story, :path1, :path2, :finish, :score

def initialize(id,story,path1,path2,finish)
def initialize(id,story,path1,path2,finish,score)
@id = id
@story = story
@path1 = path1
@path2 = path2
@finish = finish
@score = score
end

def end?
Expand Down
88 changes: 1 addition & 87 deletions lib/forkslist.csv
Original file line number Diff line number Diff line change
@@ -1,87 +1 @@
id,story,path1,path2,finish
1,"You are hiking in Snake Canyon when you find yourself lost in the strange, dimly lit Cave of Time. Gradually you can make out two passageways. One curves downward to the right; the other leads upward to the left. It occurs to you that the one leading down may go to the past and the one leading up may go to the future.

Which way will you choose?

Type '1' for the tunnel leading down
Type '2' for the tunnel leading up",4,5,false
4,"Door 8 or 10",8,10,false
5,"Door 6 or 16",6,16,false
6,"Door 22 or 114",22,114,false
8,"Door 17 or 18",17,18,false
10,"Door 20 or 21",20,21,false
11,"Door 13 or 12",13,12,false
12,"Door 66 or 78",66,78,false
13,"Door 14 or 15",14,15,false
14,"Merrie England (end)",nil,nil,true
15,"982 AD (end)",nil,nil,true
16,"Door 25 or 24",25,24,false
17,"Door 26 or 28",26,28,false
18,"Door 29 or 30",29,30,false
20,"Door 93 or 93",93,93,false
21,"Door 33 or 35",33,35,false
22,"Door 36 or 37",36,37,false
22,"Door 36 or 37",36,37,false
24,"Door 40 or 38",40,38,false
25,"Door 44 or 45",44,45,false
26,"Door 46 or 47",46,47,false
28,"Door 51 or 51",51,51,false
29,"Door 52 or 53",52,53,false
30,"Door 92 or 54",92,54,false
31,"Dying sun (end)",nil,nil,true
32,"Door 58 or 64",58,64,false
33,"Door 68 or 69",68,69,false
35,"Door 80 or 76",80,76,false
36,"Door 11 or 11",11,11,false
37,"Door 11 or 11",11,11,false
38,"Door 87 or 82",87,81,false
40,"Door 82 or 115",82,115,false
44,"Apocalypse (end)",nil,nil,true
45,"Return Home (end)",nil,nil,true
46,"Migrate (end)",nil,nil,true
47,"Door 50 or 49",50,49,false
49,"Door 25 or 25",25,25,false
50,"Door 81 or 81",81,81,false
51,"Return Home 11 years later",nil,nil,true
52,"Join Cavemen (end)",nil,nil,true
53,"Fall to Death",nil,nil,true
54,"Door 98 or 101",98,101,false
56,"Door 57 or 60",57,60,false
57,"Meta-CYOA (end)",nil,nil,true
58,"Door 63 or 62",63,62,true
60,"Monster (end)",nil,nil,true
61,"Door 104 or 106",36,37,false
62,"Surf's Up (end)",nil,nil,true
63,"Boa (end)",nil,nil,true
64,"Door 102 or 102",36,37,false
66,"Door 70 or 74",70,74,false
68,"Sinks (end)",nil,nil,true
70,"Door 72 or 103",72,103,false
72,"Return Home (end)",nil,nil,true
74,"Eaten Alive (end)",nil,nil,true
76,"Cadillac (end)",nil,nil,true
78,"Hanging (end)",nil,nil,false
79,"Door 88 or 86",88,86,false
80,"Red Sun (end)",nil,nil,true
81,"Return Home (end)",nil,nil,true
82,"Slave (end)",nil,nil,true
83,"Door 85 or 84",85,84,false
86,"London 1940 (end)",nil,nil,true
87,"Lost at Sea",nil,nil,true
88,"Door 91 or 90",91,90,false
90,"Return Home (end)",nil,nil,false
91,"T-Rex (end)",nil,nil,true
92,"Door 56 or 56",56,56,false
93,"Door 32 or 31",32,31,false
98,"Travel the world (end)",nil,nil,true
101,"Primordial Time",nil,nil,true
102,"Return Home (end)",nil,nil,true
103,"Return Home (end)",nil,nil,true
104,"Door 107 or 107",107,107,false
106,"Door 111 or 111",111,111,false
107,"Ship's Captain",nil,nil,true
111,"Return Home (end)",nil,nil,true
113,"18th Century (end)",nil,nil,true
114,"Door 61 or 61",61,61,false
115,"Return Home (end)",nil,nil,true

id,story,path1,path2,finish,score1,"You are hiking in Snake Canyon when you find yourself lost in the strange, dimly lit Cave of Time. Gradually you can make out two passageways. One curves downward to the right; the other leads upward to the left. It occurs to you that the one leading down may go to the past and the one leading up may go to the future.Which way will you choose?Type '1' for the tunnel leading downType '2' for the tunnel leading up",4,5,FALSE,1004,Door 8 or 10,8,10,FALSE,3005,Door 6 or 16,6,16,FALSE,1006,Door 22 or 114,22,114,FALSE,3008,Door 17 or 18,17,18,FALSE,30010,Door 20 or 21,20,21,FALSE,20011,Door 13 or 12,13,12,FALSE,50012,Door 66 or 78,66,78,FALSE,30013,Door 14 or 15,14,15,FALSE,20014,Merrie England (end),nil,nil,TRUE,50015,982 AD (end),nil,nil,TRUE,20016,Door 25 or 24,25,24,FALSE,30017,Door 26 or 28,26,28,FALSE,30018,Door 29 or 30,29,30,FALSE,10020,Door 93 or 93,93,93,FALSE,30021,Door 33 or 35,33,35,FALSE,30022,Door 36 or 37,36,37,FALSE,20024,Door 40 or 38,40,38,FALSE,50025,Door 44 or 45,44,45,FALSE,30026,Door 46 or 47,46,47,FALSE,20028,Door 51 or 51,51,51,FALSE,50029,Door 52 or 53,52,53,FALSE,20030,Door 92 or 54,92,54,FALSE,30031,Dying sun (end),nil,nil,TRUE,30032,Door 58 or 64,58,64,FALSE,10033,Door 68 or 69,68,79,FALSE,30035,Door 80 or 76,80,76,FALSE,30036,Door 11 or 11,11,11,FALSE,20037,Door 11 or 11,11,11,FALSE,50038,Door 87 or 82,87,81,FALSE,30040,Door 82 or 115,82,115,FALSE,20044,Apocalypse (end),nil,nil,TRUE,50045,Return Home (end),nil,nil,TRUE,20046,Migrate (end),nil,nil,TRUE,30047,Door 50 or 49,50,49,FALSE,30049,Door 25 or 25,25,25,FALSE,10050,Door 81 or 81,81,81,FALSE,30051,Return Home 11 years later,nil,nil,TRUE,30052,Join Cavemen (end),nil,nil,TRUE,20053,Fall to Death,nil,nil,TRUE,50054,Door 98 or 101,98,101,FALSE,30056,Door 57 or 60,57,60,FALSE,20057,Meta-CYOA (end),nil,nil,TRUE,50058,Door 63 or 62,63,62,FALSE,20060,Monster (end),nil,nil,TRUE,30061,Door 104 or 106,36,37,FALSE,30062,Surf's Up (end),nil,nil,TRUE,10063,Boa (end),nil,nil,TRUE,30064,Door 102 or 102,36,37,FALSE,30066,Door 70 or 74,70,74,FALSE,20068,Sinks (end),nil,nil,TRUE,50070,Door 72 or 103,72,103,FALSE,30072,Return Home (end),nil,nil,TRUE,20074,Eaten Alive (end),nil,nil,TRUE,50076,Cadillac (end),nil,nil,TRUE,20078,Hanging (end),nil,nil,TRUE,30079,Door 88 or 86,88,86,FALSE,30080,Red Sun (end),nil,nil,TRUE,10081,Return Home (end),nil,nil,TRUE,30082,Slave (end),nil,nil,TRUE,30083,Door 85 or 84,85,84,FALSE,20086,London 1940 (end),nil,nil,TRUE,50087,Lost at Sea,nil,nil,TRUE,30088,Door 91 or 90,91,90,FALSE,20090,Return Home (end),nil,nil,TRUE,50091,T-Rex (end),nil,nil,TRUE,20092,Door 56 or 56,56,56,FALSE,30093,Door 32 or 31,32,31,FALSE,30098,Travel the world (end),nil,nil,TRUE,100101,Primordial Time,nil,nil,TRUE,300102,Return Home (end),nil,nil,TRUE,300103,Return Home (end),nil,nil,TRUE,200104,Door 107 or 107,107,107,FALSE,500106,Door 111 or 111,111,111,FALSE,300107,Ship's Captain,nil,nil,TRUE,200111,Return Home (end),nil,nil,TRUE,500113,18th Century (end),nil,nil,TRUE,200114,Door 61 or 61,61,61,FALSE,300115,Return Home (end),nil,nil,TRUE,500
Expand Down
62 changes: 53 additions & 9 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
require 'colorize'
require_relative 'story'

class Game

attr_reader :score
attr_reader :name,:total_score,:top_scores

def initialize
@maingame = Story.new
@score = 0
@total_score = 0
end

def instructions
puts ""
puts "Welcome to the game of".center(80)
puts " _____ _ ____ __ _____ _

puts <<-CAVE_OF_TIME
_____ _ ____ __ _____ _
|_ _| |__ ___ / ___|__ ___ _____ ___ / _| |_ _(_)_ __ ___ ___
| | | '_ \\ / _ \\ | | / _` \\ \\ / / _ \\ / _ \\| |_ | | | | '_ ` _ \\ / _ \\
| | | | | | __/ | |___ (_| |\\ V / __/ | (_) | _| | | | | | | | | | __/
|_| |_| |_|\\___| \\____\\__,_| \\_/ \\___| \\___/|_| |_| |_|_| |_| |_|\\___|"
|_| |_| |_|\\___| \\____\\__,_| \\_/ \\___| \\___/|_| |_| |_|_| |_| |_|\\___|
CAVE_OF_TIME
puts ""
puts "~~~~ A Choose Your Own Adventure Game ~~~~".center(80)
puts "\nThis is a game where you choose what happens next in the story. It all depends on the choices you make. How does the story end? Only you can find out! And the best part is that you can start over and go ahead until you've had not one but many incredibly daring experiences!\n\n"
Expand All @@ -31,13 +35,13 @@ def prompt(*args)
end

def get_user_name
name = gets
puts "Hello #{name}"
@name = gets.chomp
@name = @name.capitalize
end

def load_next(id)
item = @maingame.get_fork(id)
while item.finish == "false"
while item.finish == "FALSE"
puts item.story.to_s
route = Integer(gets)
until route == 1 || route == 2
Expand All @@ -50,9 +54,11 @@ def load_next(id)
next_fork = item.path2
end
item = @maingame.get_fork(next_fork)
@score += 10
@total_score += item.score.to_i
end
game_over
add_to_high_scores
show_high_scores
end

def clear_screen
Expand All @@ -67,9 +73,47 @@ def game_over
| |_| | (_| | | | | | | __/ | |_| |\\ V / __/ | |_|
\\____|\\__,_|_| |_| |_|\\___| \\___/ \\_/ \\___|_| (_)"

puts "You scored #{@score} points!"
puts "You scored #{@total_score} points #{@name.chomp}!".upcase.colorize(:green).center(70)
end

def get_top_scores
#get all existing scores from csv file
@top_scores = {}
CSV.foreach("scores.csv", :headers => false) do |row|
@top_scores[row[0].to_i] = {:id => row[0].to_i,:score => row[1].to_i,:name => row[2],:date => row[3].to_s}
end
end

def add_to_high_scores
get_top_scores
if @top_scores.length > 0 #if there are 1 or more high scores
lowest_score = @top_scores.values.min_by {|v| v[:score]}
low_score = lowest_score[:score].to_i
else
low_score = 0 #if there are no high scores
end
if @total_score > low_score
if @top_scores.length < 10
@top_scores[@top_scores.length + 1] = {:id => @top_scores.length + 1,:score => @total_score,:name => @name,:date => Date.today.to_s}
else
@top_scores.delete(lowest_score[:id]) #delete lowest score
@top_scores[lowest_score[:id]] = {:id => lowest_score[:id],:score => @total_score,:name => @name,:date => Date.today.to_s} #update with new score
end
end
#rewrite the file
CSV.open("scores.csv","w") do |csv|
@top_scores.each do |k,v|
csv << [v[:id],v[:score],v[:name],v[:date]]
end
end
end

def show_high_scores
puts "\n~~~Here are the top scores!~~~\n".upcase.center(80).colorize(:red)
@top_scores = @top_scores.sort_by { |k, v| v[:score] }.reverse
@top_scores.each do |key,value|
puts "#{value[:name].colorize(:yellow)} scored #{value[:score].to_s.colorize(:yellow)} points on #{value[:date]}."
end
end

end
2 changes: 1 addition & 1 deletion lib/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Story
def initialize
@all_forks = {}
CSV.foreach("forkslist.csv", :headers => true) do |row|
@all_forks[row[0]] = Fork.new(row[0],row[1],row[2],row[3],row[4])
@all_forks[row[0]] = Fork.new(row[0],row[1],row[2],row[3],row[4],row[5])
end
end

Expand Down

0 comments on commit 459e4e9

Please sign in to comment.