diff --git a/lib/ruby_warrior/game.rb b/lib/ruby_warrior/game.rb index 59f00c47..6c719f65 100644 --- a/lib/ruby_warrior/game.rb +++ b/lib/ruby_warrior/game.rb @@ -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 @@ -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 diff --git a/lib/ruby_warrior/runner.rb b/lib/ruby_warrior/runner.rb index d2cb6c68..f4b780b9 100644 --- a/lib/ruby_warrior/runner.rb +++ b/lib/ruby_warrior/runner.rb @@ -1,4 +1,5 @@ require 'optparse' +require 'fileutils' module RubyWarrior class Runner @@ -16,7 +17,42 @@ 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 @@ -24,6 +60,7 @@ def parse_options 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 } diff --git a/lib/ruby_warrior/ui.rb b/lib/ruby_warrior/ui.rb index a616d54c..e97aea15 100644 --- a/lib/ruby_warrior/ui.rb +++ b/lib/ruby_warrior/ui.rb @@ -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