Skip to content

Instantly share code, notes, and snippets.

@luckyruby
Last active December 29, 2015 23:19
Show Gist options
  • Select an option

  • Save luckyruby/7742023 to your computer and use it in GitHub Desktop.

Select an option

Save luckyruby/7742023 to your computer and use it in GitHub Desktop.
Simple choose your own adventure game to help my 12 year old nephew learn programming.
player_balance = 1000
player_skill_ids = [] #store course id for skills learned
COURSES = [
{ id: 1, name: 'Earth', price: 200, instructor: "Kwame", days: 8 },
{ id: 2, name: 'Fire', price: 300, instructor: "Wheeler", days: 5 },
{ id: 3, name: 'Wind', price: 150, instructor: "Linka", days: 7 },
{ id: 4, name: 'Water', price: 225, instructor: "Gi", days: 6 },
{ id: 5, name: 'Heart', price: 250, instructor: "Ma-Ti", days: 8 }
]
def player_skills(ids)
COURSES.select{|i| ids.include?(i[:id])}.map{|i| i[:name]}.join(", ")
end
def valid_course?(selection)
COURSES.map{|i| i[:id]}.include? selection.to_i
end
puts "What is your name?"
player_name = gets.chomp
puts "\nWelcome to the School of Planeteers, #{player_name}! Please select a class to attend. (Player Balance: $#{player_balance})"
COURSES.each do |c|
puts "#{c[:id]}) #{c[:name]} ($#{c[:price]})"
end
course_selection = gets.chomp.to_i
until valid_course?(course_selection)
puts "I don't recognize that course number. Please try again."
course_selection = gets.chomp.to_i
end
course = COURSES.detect{|i| i[:id] == course_selection}
player_balance -= course[:price]
player_skill_ids << course[:id]
puts "\nYou have enrolled in #{course[:name]} which is being taught by #{course[:instructor]}. (Player Balance: $#{player_balance})"
(1 .. course[:days]).each do |day|
puts "Day #{day} ..."
sleep 1
end
puts "Congratulations! After #{course[:days]} days of hard work and dedication, you have now mastered the skill of #{course[:name]}. (Player Skills: #{player_skills(player_skill_ids)})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment