lib/femis_hangman/router.rb in femis_hangman-0.1.2 vs lib/femis_hangman/router.rb in femis_hangman-0.1.3

- old
+ new

@@ -11,59 +11,59 @@ elsif @status == 'feedback' then choose_feedback(input) elsif @status == 'start' then begin_game(input) elsif @status == 'play' then play_game(input) elsif @status == 'finish' then restart_game(input) elsif @status == 'load' then load_game(input) - elsif @status == 'quit' || 'save' then quit_game(input) + elsif @status == 'quit' then quit_game(input) else welcome(input) end end def welcome(input) case input when 'p', 'play' then start_game when 'q', 'quit' then quit_game when 'l', 'load' then show_saved_games - when 'i', 'instructions' then instructions_prompt - else invalid_prompt + when 'i', 'instructions' then puts instructions_prompt + else puts invalid_prompt end end def start_game @status = 'feedback' - welcome_prompt - feedback_prompt + puts welcome_prompt + puts feedback_prompt end def choose_feedback(input) if input.to_i < 1 || input.to_i > 2 - invalid_prompt + puts invalid_prompt false else - level_prompt + puts level_prompt @feedback = input.to_i @status = 'start' end end def begin_game(input) if input.to_i < 1 || input.to_i > 3 - invalid_prompt + puts invalid_prompt else @difficulty = input.to_i @status = 'play' create_game end end def create_game - begin_prompt + puts begin_prompt @game = Game.new(@difficulty, @feedback) - size_prompt(@game.word.size) - turns_prompt(@game.turns) - game_instruction_prompt - print_text(@game.show_word) + puts size_prompt(@game.word.size) + puts turns_prompt(@game.turns) + puts game_instruction_prompt + puts (@game.show_word) end def play_game(input) if @game.status == 'restart' restart_game(input) @@ -73,58 +73,67 @@ end end def restart_game(input) if input == 'r' || input == 'restart' - level_prompt + puts level_prompt @status = 'start' elsif input == 'q' || input == 'quit' then quit_game else invalid_prompt end end - def save_game - File.open('./saved_games.yaml', 'a'){|f| f.write(YAML.dump(@game))} + def save_game(file='./saved_games.yaml') + File.open(file, 'a'){|f| f.write(YAML.dump(@game))} @status = 'quit' end def show_saved_games @status = 'load' - load_prompt - i = 0 + puts load_prompt + counter = 0 YAML.load_stream(File.open('./saved_games.yaml', 'r')).each do |game| - print "#{i += 1}: " - print game.show_word - print "(#{game.turns} turns left)" - print "\n" + counter += 1 + puts saved_games_list(counter, game) end end + def saved_games_list(counter, game) + "#{counter}: " << game.show_word << "(#{game.turns} turns left)" + end + def load_game(input) game_id = 0 YAML.load_stream(File.open('./saved_games.yaml', 'r')).each do |game| game_id += 1 - @game = game + if game_id == input.to_i then resume_game(game) + else + puts invalid_game_prompt + show_saved_games + end end + end + + def resume_game(game) + @game = game @status = 'play' @game.status = 'play' @game.check_game end def quit_game(input=nil) - if input.nil? then @status = 'quit' + if input.nil? || input == 'q' || 'quit' then @status = 'quit' elsif input == 's' || 'save' then save_game - elsif input == 'q' || 'quit' then quit_game - else invalid_prompt + else puts invalid_prompt end end def loop repl = lambda do |prompt| print prompt - process(gets.chomp!) + process(STDIN.gets.chomp.downcase) end repl['% Hangman-0.1.0: '] while @status != 'quit' - thanks_prompt + puts thanks_prompt end end end \ No newline at end of file