codeguessing 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/codeguessing/console.rb +16 -5
- data/lib/codeguessing/game.rb +13 -20
- data/lib/codeguessing/scores.yml +21 -0
- data/lib/codeguessing/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5a2a58a150dba74e9f58963a14232efe0dee761
|
4
|
+
data.tar.gz: 951e826fa29482f88ec28cb346bebd7f7484eaeb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18651820487e51cab59a3e4214a86812057c41dc78d215ab35aa65b5054bc412200449b9881374af7ee61a9eeb8b9b3bdc90375fa047310419b210d9ddd51bed
|
7
|
+
data.tar.gz: 1f31add28d73efdf5b8e5ef81595597ed0220ebb75baaadd0e5e492cb551a9c57a164d352b55109ee6f78fa9ac72ce1664cb5852090e92d69919e9b3eb5dc11a
|
data/lib/codeguessing/console.rb
CHANGED
@@ -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
|
-
@
|
6
|
-
@game = Game.new(
|
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
|
-
|
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 @
|
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
|
data/lib/codeguessing/game.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
1
|
module Codeguessing
|
2
2
|
class Game
|
3
|
-
attr_reader :result, :attempts, :hint_count, :state
|
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(
|
10
|
-
@secret_code =
|
11
|
-
|
12
|
-
@
|
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
|
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
|
data/lib/codeguessing/scores.yml
CHANGED
@@ -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
|
data/lib/codeguessing/version.rb
CHANGED