codeguessing 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb1b864076f30ae5faf38e18964a9876dfb9eb32
4
- data.tar.gz: 38135af1470f0702b26bfe2453bdcfc54a63de15
3
+ metadata.gz: d5a2a58a150dba74e9f58963a14232efe0dee761
4
+ data.tar.gz: 951e826fa29482f88ec28cb346bebd7f7484eaeb
5
5
  SHA512:
6
- metadata.gz: 239379048e30540ab24d6472e3ec95d79c892d0ca0b9d20106fa57565aeb649df123cc4c7bda2a4c95a81cde1bb9df40b806e0fd9b09e3d2ea3cd1f7e3e7c822
7
- data.tar.gz: 4c47bbbaa9cd20ea012dec34bc86c859e1196898531d268d4d0d5b5638fb01ffac8a80dd99fc04291c070d71c4fdd57e3a58444ef7e174bf9c5b5d74a7fc8344
6
+ metadata.gz: 18651820487e51cab59a3e4214a86812057c41dc78d215ab35aa65b5054bc412200449b9881374af7ee61a9eeb8b9b3bdc90375fa047310419b210d9ddd51bed
7
+ data.tar.gz: 1f31add28d73efdf5b8e5ef81595597ed0220ebb75baaadd0e5e492cb551a9c57a164d352b55109ee6f78fa9ac72ce1664cb5852090e92d69919e9b3eb5dc11a
@@ -1,9 +1,9 @@
1
1
  module Codeguessing
2
2
  class Console
3
- def initialize(again = false)
3
+ def initialize(again = false, opt)
4
4
  @path = File.join(File.dirname(__FILE__), 'scores.yml')
5
- @data = load(@path)
6
- @game = Game.new(@data)
5
+ @scores = load(@path)
6
+ @game = Game.new(opt)
7
7
  return start if again
8
8
  knowing
9
9
  end
@@ -45,7 +45,7 @@ module Codeguessing
45
45
  puts 'Do you want save result? (Y/N)'
46
46
  return puts 'Goodbie!' unless confirm?
47
47
  puts 'Write your name'
48
- @game.save(@path, gets.chomp)
48
+ save(gets.chomp)
49
49
  again?
50
50
  end
51
51
 
@@ -60,7 +60,7 @@ module Codeguessing
60
60
  Console.new(true)
61
61
  else
62
62
  puts '-----------Scores----------'.yellow
63
- puts @game.scores
63
+ puts @scores
64
64
  puts '---------------------------'.yellow
65
65
  end
66
66
  end
@@ -79,5 +79,16 @@ module Codeguessing
79
79
  def load(path)
80
80
  YAML.load(File.open(path)) if File.exist?(path)
81
81
  end
82
+
83
+ def save(name = 'Anonim')
84
+ return if @game.state != true
85
+ @scores << @game.cur_score
86
+ File.new(@path, 'w') unless File.exist?(@path)
87
+ File.open(@path, "r+") do |f|
88
+ f.write(@scores.to_yaml)
89
+ end
90
+ @scores
91
+ end
92
+
82
93
  end
83
94
  end
@@ -1,18 +1,16 @@
1
1
  module Codeguessing
2
2
  class Game
3
- attr_reader :result, :attempts, :hint_count, :state, :scores
3
+ attr_reader :result, :attempts, :hint_count, :state
4
4
  attr_accessor :secret_code
5
5
 
6
6
  MAX_HINT = 2
7
7
  MAX_ATTEMPTS = 5
8
8
 
9
- def initialize(data = [])
10
- @secret_code = ''
11
- 4.times { @secret_code += rand(1..6).to_s }
12
- @attempts = MAX_ATTEMPTS
13
- @hint_count = MAX_HINT
9
+ def initialize(opt = {})
10
+ @secret_code = opt[:secret_code] || random
11
+ @attempts = opt[:attempts] || MAX_ATTEMPTS
12
+ @hint_count = opt[:hint_count] || MAX_HINT
14
13
  @state = ''
15
- @scores = data || []
16
14
  end
17
15
 
18
16
  def guess(code)
@@ -44,20 +42,9 @@ module Codeguessing
44
42
  res
45
43
  end
46
44
 
47
- def save(path, name = 'Anonim')
48
- return false if state != true
49
- score = cur_score
50
- score[:name] = name
51
- @scores << score
52
- File.new(path, 'w') unless File.exist?(path)
53
- File.open(path, "r+") do |f|
54
- f.write(@scores.to_yaml)
55
- end
56
- @scores
57
- end
58
-
59
- def cur_score
45
+ def cur_score(name = 'Anonim')
60
46
  hash = {}
47
+ hash[:name] = name
61
48
  self.instance_variables.each do |k, v|
62
49
  new_k = k.to_s.gsub('@','').to_sym
63
50
  hash[new_k] = self.instance_variable_get(k)
@@ -94,5 +81,11 @@ module Codeguessing
94
81
  @state = false
95
82
  end
96
83
 
84
+ def random
85
+ code = ''
86
+ 4.times { code += rand(1..6).to_s }
87
+ code
88
+ end
89
+
97
90
  end
98
91
  end
@@ -19,3 +19,24 @@
19
19
  :attempts: 0
20
20
  :hint_count: 0
21
21
  :name: Yaroslav
22
+ - :secret_code: '1234'
23
+ :attempts: 4
24
+ :hint_count: 2
25
+ :name: dasdad
26
+ - :secret_code: '1234'
27
+ :attempts: 4
28
+ :hint_count: 2
29
+ :name: Y
30
+ - Anonim
31
+ - :secret_code: '1234'
32
+ :attempts: 4
33
+ :hint_count: 2
34
+ :name: Anonim
35
+ - :name: Anonim
36
+ :secret_code: '1234'
37
+ :attempts: 4
38
+ :hint_count: 2
39
+ - :name: Anonim
40
+ :secret_code: '1234'
41
+ :attempts: 2
42
+ :hint_count: 4
@@ -1,3 +1,3 @@
1
1
  module Codeguessing
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codeguessing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - bezrukavyi