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

- old
+ new

@@ -9,16 +9,14 @@ def process(input) if @status == 'begin' then welcome(input) 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 finish_game(input) - elsif @status == 'save' then save_game + elsif @status == 'finish' then restart_game(input) elsif @status == 'load' then load_game(input) - elsif @status == 'quit' || 'end' then quit_game - else - welcome(input) + elsif @status == 'quit' || 'save' then quit_game(input) + else welcome(input) end end def welcome(input) case input @@ -28,28 +26,35 @@ when 'i', 'instructions' then instructions_prompt else invalid_prompt end end + def start_game + @status = 'feedback' + welcome_prompt + feedback_prompt + end + def choose_feedback(input) - @feedback = input.to_i - if @feedback < 1 || @feedback > 2 - feedback_prompt - return + if input.to_i < 1 || input.to_i > 2 + invalid_prompt + false + else + level_prompt + @feedback = input.to_i + @status = 'start' end - level_prompt - @status = 'start' end def begin_game(input) - @difficulty = input.to_i - if @difficulty < 1 || @difficulty > 3 + if input.to_i < 1 || input.to_i > 3 invalid_prompt - return + else + @difficulty = input.to_i + @status = 'play' + create_game end - @status = 'play' - create_game end def create_game begin_prompt @game = Game.new(@difficulty, @feedback) @@ -59,95 +64,67 @@ print_text(@game.show_word) end def play_game(input) if @game.status == 'restart' - if input == 'r' || input == 'restart' - level_prompt - @status = 'start' - elsif input == 'q' || input == 'quit' - quit_game - else invalid_prompt - end + restart_game(input) elsif @game.status == 'quit' - if input == 's' || 'save' then save_game - elsif input == 'q' || 'quit' then quit_game - else - invalid_prompt - end + quit_game(input) else @game.control(input) end end - def start_game - @status = 'feedback' - welcome_prompt - feedback_prompt - end - - def finish_game(input) - if input == 'r' || 'restart' + def restart_game(input) + if input == 'r' || input == 'restart' + level_prompt @status = 'start' - begin_game(input) - elsif input == 'q' || 'quit' - save_prompt - @status = 'save' - else - invalid_prompt + 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))} - thanks_prompt - @status = 'end' + @status = 'quit' end def show_saved_games @status = 'load' load_prompt i = 0 YAML.load_stream(File.open('./saved_games.yaml', 'r')).each do |game| print "#{i += 1}: " - word = game.word.split('') - history = game.history - word.each do |letter| - if history.include?(letter) - print "#{letter} " - else - print '_ ' - end - end + print game.show_word print "(#{game.turns} turns left)" print "\n" end end def load_game(input) - game_id = input.to_i - i = 0 + game_id = 0 YAML.load_stream(File.open('./saved_games.yaml', 'r')).each do |game| - i += 1 - if i == game_id - @status = 'play' - @game = game - @game.status = 'play' - @game.check_game - end + game_id += 1 + @game = game end + @status = 'play' + @game.status = 'play' + @game.check_game end - def quit_game - @status = 'end' - thanks_prompt + def quit_game(input=nil) + if input.nil? then @status = 'quit' + elsif input == 's' || 'save' then save_game + elsif input == 'q' || 'quit' then quit_game + else invalid_prompt + end end def loop repl = lambda do |prompt| print prompt process(gets.chomp!) end - - repl["% Hangman-#{FemisHangman::VERSION}: "] while @status != 'end' + repl['% Hangman-0.1.0: '] while @status != 'quit' + thanks_prompt end end end \ No newline at end of file