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
50 changes: 33 additions & 17 deletions lib/ruby_warrior/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,31 @@ def play_current_level
continue = true
current_level.load_player
UI.puts "Starting Level #{current_level.number}"
current_level.play
if current_level.passed?
if next_level.exists?
UI.puts "Success! You have found the stairs."
if !compared_files?
current_level.play
if current_level.passed?
if next_level.exists?
UI.puts "Success! You have found the stairs."
else
UI.puts "CONGRATULATIONS! You have climbed to the top of the tower and rescued the fair maiden Ruby."
continue = false
end
current_level.tally_points
if profile.epic?
UI.puts final_report if final_report && !continue
else
request_next_level
end
else
UI.puts "CONGRATULATIONS! You have climbed to the top of the tower and rescued the fair maiden Ruby."
continue = false
UI.puts "Sorry, you failed level #{current_level.number}. Change your script and try again."
if !Config.skip_input? && current_level.clue && UI.ask("Would you like to read the additional clues for this level?")
UI.puts current_level.clue.hard_wrap
end
end
current_level.tally_points
if profile.epic?
UI.puts final_report if final_report && !continue
else
request_next_level
end
else
continue = false
UI.puts "Sorry, you failed level #{current_level.number}. Change your script and try again."
if !Config.skip_input? && current_level.clue && UI.ask("Would you like to read the additional clues for this level?")
UI.puts current_level.clue.hard_wrap
end
else
UI.puts "You haven't changed the initial rubywarrior/#{profile.directory_name}/player.rb file!"
continue=false
end
continue
end
Expand Down Expand Up @@ -160,6 +165,17 @@ def tower_paths
end


#templates

def templates_path
File.expand_path("../../../templates", __FILE__)
end

def compared_files?
FileUtils.compare_file(current_level.player_path+'/player.rb',templates_path+'/player.rb')
end


# levels

def current_level
Expand Down
39 changes: 38 additions & 1 deletion lib/ruby_warrior/runner.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'optparse'
require 'fileutils'

module RubyWarrior
class Runner
Expand All @@ -16,14 +17,50 @@ def run
parse_options
@game.start
end


def reset
if ! File.directory?('rubywarrior/')
# If the directory doesn't exist, we can't reset the progress.
print "No progress to reset.\n"
else
directories = Dir["rubywarrior/*"]
if directories.length>0
puts inputProfile = UI.choose('directory', directories)
if inputProfile != nil
if File.directory?('%{IN}'% {IN:inputProfile})
# Prompt the user to avoid accidental an reset.
print "Are you sure you want to reset your progress? [yn]\n"
input = gets.chomp
if input == 'y'
FileUtils.rm_rf('%{IN}'% {IN:inputProfile} )
print "Progress reset.\n"
elsif input == 'n'
print "Progress not reset.\n"
else
print "Not a valid option (y or n). Please try again.\n"
reset
end
else
print "No such profile.\n"
end
else
print "No profile name provided.\n"
end
else
print "No profile to reset."
end
end
exit
end

private

def parse_options
options = OptionParser.new
options.banner = "Usage: rubywarrior [options]"
options.on('-d', '--directory DIR', "Run under given directory") { |dir| Config.path_prefix = dir }
options.on('-l', '--level LEVEL', "Practice level on epic") { |level| Config.practice_level = level.to_i }
options.on('-r', '--reset', "Reset all Progress ") { reset }
options.on('-s', '--skip', "Skip user input") { Config.skip_input = true }
options.on('-t', '--time SECONDS', "Delay each turn by seconds") { |seconds| Config.delay = seconds.to_f }
options.on('-h', '--help', "Show this message") { puts(options); exit }
Expand Down
19 changes: 13 additions & 6 deletions lib/ruby_warrior/ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,22 @@ def choose(item, options)
if options.length == 1
response = options.first
else
options.each_with_index do |option, i|
if option.kind_of? Array
puts "[#{i+1}] #{option.last}"
loop do
options.each_with_index do |option, i|
if option.kind_of? Array
puts "[#{i+1}] #{option.last}"
else
puts "[#{i+1}] #{option}"
end
end
choice = request("Choose #{item} by typing the number: ")
if choice.to_i>options.length
print "You haven't provided a correct input. Try again!\n\n"
else
puts "[#{i+1}] #{option}"
response = options[choice.to_i-1]
end
break if choice.to_i<=options.length
end
choice = request("Choose #{item} by typing the number: ")
response = options[choice.to_i-1]
end
if response.kind_of? Array
response.first
Expand Down