lib/codeguessing/console.rb in codeguessing-0.2.0 vs lib/codeguessing/console.rb in codeguessing-0.3.0
- old
+ new
@@ -1,11 +1,11 @@
module Codeguessing
class Console
- def initialize(again = false)
+ def initialize(again = false, opt)
@path = File.join(File.dirname(__FILE__), 'scores.yml')
- @data = load(@path)
- @game = Game.new(@data)
+ @scores = load(@path)
+ @game = Game.new(opt)
return start if again
knowing
end
def knowing
@@ -43,11 +43,11 @@
def win
puts 'You win!'.green
puts 'Do you want save result? (Y/N)'
return puts 'Goodbie!' unless confirm?
puts 'Write your name'
- @game.save(@path, gets.chomp)
+ save(gets.chomp)
again?
end
def loose
puts 'You loose!'.red
@@ -58,11 +58,11 @@
puts 'Do you want start again? (Y/N)'
if confirm?
Console.new(true)
else
puts '-----------Scores----------'.yellow
- puts @game.scores
+ puts @scores
puts '---------------------------'.yellow
end
end
def confirm?(action = gets.chomp)
@@ -77,7 +77,18 @@
end
def load(path)
YAML.load(File.open(path)) if File.exist?(path)
end
+
+ def save(name = 'Anonim')
+ return if @game.state != true
+ @scores << @game.cur_score
+ File.new(@path, 'w') unless File.exist?(@path)
+ File.open(@path, "r+") do |f|
+ f.write(@scores.to_yaml)
+ end
+ @scores
+ end
+
end
end