lib/codeguessing/console.rb in codeguessing-0.4.7 vs lib/codeguessing/console.rb in codeguessing-0.4.8

- old
+ new

@@ -1,109 +1,94 @@ module Codeguessing class Console - attr_reader :game, :scores, :path + attr_reader :game, :scores - RULES = [ - '-----------------Rules!----------------------', - "You need guess secret code. This four-digit number with symbols from 1 to 6", - "You have #{Game::MAX_ATTEMPTS} attempt(s) and #{Game::MAX_HINT} hint(s)", - "If you want get hint write 'hint'", - '---------------------------------------------' - ] + MESSAGE = YAML.load_file(File.absolute_path('data/messages.yml')) - def initialize(opt = {}) - @path = File.join(File.dirname(__FILE__), 'scores.yml') + def initialize + @path = File.absolute_path('data/scores.yml') @scores = load(@path) - @game = Game.new(opt) + @game = Game.new end - def go(knowed = true) - rules if knowed - puts "Attempt(s): #{@game.attempts} | Hint(s): #{@game.hint_count}" - case @game.win? - when true - win - when false - loose - else - gaming - end + def rules + puts MESSAGE['rules?'] + puts MESSAGE['rules'].join("\n") unless confirm? end - def rules - puts "Do you know rules? (Y/N)" - unless confirm? - puts RULES.join("\n") + def go(knowed = false) + rules unless knowed + puts "Attempt(s): #{game.attempts} | Hint(s): #{game.hint_count}" + case game.win? + when true then win + when false then loose + else gaming end end def gaming action = gets.chomp if action == 'hint' - puts @game.hint - return go(false) + puts game.hint + return go(true) end - if @game.valid?(action) - puts @game.guess(action) + if game.valid?(action) + puts game.guess(action) else - puts 'Invalid data' + puts MESSAGE['invalid_data'] end - go(false) + go(true) end - - def confirm?(action = gets.chomp) - return true if action.downcase == 'y' - false - end - - def load(path) - YAML.load(File.open(path)) if File.exist?(path) - end - - def save!(name = 'Anonim') - unless @game.win? - return puts 'You cant save game' - end - name.chomp! - @scores << @game.cur_score(name) - File.new(@path, 'w') unless File.exist?(@path) - File.open(@path, "r+") do |f| - f.write(@scores.to_yaml) - end - @scores - end private + def confirm?(action = gets.chomp) + return true if action.downcase == 'y' + false + end + + def load(path) + YAML.load_file(path) if File.exist?(path) + end + + def save(name: 'Anonim', path: @path) + return puts MESSAGE['cant_save'] unless game.win? + @scores << game.cur_score(name.chomp) + File.new(path, 'w') unless File.exist?(path) + File.open(path, "r+") do |f| + f.write(scores.to_yaml) + end + scores + end + def win - puts 'You win!' + puts MESSAGE['win'] save? - again? + again end def loose - puts 'You loose!' - puts "Secret code was #{@game.secret_code}" - again? + puts MESSAGE['loose'] + puts "#{MESSAGE['loose_code']} #{game.secret_code}" + again end def save? - puts 'Do you want save result? (Y/N)' - return puts 'Goodbie!' unless confirm? - puts 'Write your name' - save!(gets) + puts MESSAGE['save?'] + return puts MESSAGE['not_saved'] unless confirm? + puts MESSAGE['set_name'] + save(gets) end - def again? - puts 'Do you want start again? (Y/N)' + def again + puts MESSAGE['again?'] if confirm? @game = Game.new - return go(false) + return go(true) else - puts '-----------Scores----------' - p @scores - puts '---------------------------' + puts MESSAGE['scores_line']['start'] + p scores + puts MESSAGE['scores_line']['end'] end end - end end