codeguessing 0.3.3 → 0.4
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 +4 -4
- data/.gitignore +0 -0
- data/.rspec +0 -0
- data/.travis.yml +0 -0
- data/CODE_OF_CONDUCT.md +0 -0
- data/Gemfile +0 -0
- data/LICENSE.txt +0 -0
- data/README.md +0 -0
- data/Rakefile +0 -0
- data/bin/console +0 -0
- data/bin/setup +0 -0
- data/codeguessing.gemspec +0 -0
- data/lib/codeguessing/console.rb +85 -72
- data/lib/codeguessing/game.rb +9 -7
- data/lib/codeguessing/scores.yml +18 -19
- data/lib/codeguessing/version.rb +1 -1
- data/lib/codeguessing.rb +0 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2be5270a620c86f26d398e9c6af49b43aa5c8f4
|
4
|
+
data.tar.gz: 1327eb3f6a111ee3581cdc8593d350f1283ce728
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c6db8055afff298cf5d5c4abc09d7f88f32679c425bf08bb42a1a31c8a4913f3c20f2b53820d6bec01fa725634eda7bd34cee8e8ec09dc2173c9b7e781e26fe
|
7
|
+
data.tar.gz: fbc530de9fe44ed38e626068f01643c5dc41ac34cb84384082728a5be3c6fd9b29bb3d5a90b10c5539a23a575011e05c21ab8b74a7f9ae90f78ddf4631e335bc
|
data/.gitignore
CHANGED
File without changes
|
data/.rspec
CHANGED
File without changes
|
data/.travis.yml
CHANGED
File without changes
|
data/CODE_OF_CONDUCT.md
CHANGED
File without changes
|
data/Gemfile
CHANGED
File without changes
|
data/LICENSE.txt
CHANGED
File without changes
|
data/README.md
CHANGED
File without changes
|
data/Rakefile
CHANGED
File without changes
|
data/bin/console
CHANGED
File without changes
|
data/bin/setup
CHANGED
File without changes
|
data/codeguessing.gemspec
CHANGED
File without changes
|
data/lib/codeguessing/console.rb
CHANGED
@@ -1,94 +1,107 @@
|
|
1
1
|
module Codeguessing
|
2
|
-
class Console
|
3
|
-
|
2
|
+
class Console
|
3
|
+
attr_reader :game, :scores, :path
|
4
|
+
|
5
|
+
def initialize(opt = {})
|
4
6
|
@path = File.join(File.dirname(__FILE__), 'scores.yml')
|
5
7
|
@scores = load(@path)
|
6
|
-
@game = Game.new(opt)
|
7
|
-
return start if again
|
8
|
-
knowing
|
9
|
-
end
|
10
|
-
|
11
|
-
def knowing
|
12
|
-
puts "Are you ready broke code? (Y/N)"
|
13
|
-
if confirm?
|
14
|
-
puts '-----------------Rules!----------------------'.yellow
|
15
|
-
puts "You need guess secret code. This four-digit number with symbols from 1 to 6".yellow
|
16
|
-
puts "You have #{@game.attempts} attempt(s) and #{@game.hint_count} hint(s)".yellow
|
17
|
-
puts "If you want get hint write 'hint'".yellow
|
18
|
-
puts '---------------------------------------------'.yellow
|
19
|
-
return start
|
20
|
-
else
|
21
|
-
puts 'Goodbie!'
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def start
|
26
|
-
puts "Attempt(s): #{@game.attempts} | Hint(s): #{@game.hint_count}"
|
27
|
-
return loose if @game.state == 'false'
|
28
|
-
return win if @game.state == 'true'
|
29
|
-
|
30
|
-
action = gets.chomp
|
31
|
-
if action == 'hint'
|
32
|
-
puts @game.hint
|
33
|
-
return start
|
34
|
-
end
|
35
|
-
unless @game.valid?(action)
|
36
|
-
puts 'Invalid data'.red
|
37
|
-
return start
|
38
|
-
end
|
39
|
-
puts coloring(@game.guess(action))
|
40
|
-
start
|
41
|
-
end
|
42
|
-
|
43
|
-
def win
|
44
|
-
puts 'You win!'.green
|
45
|
-
puts 'Do you want save result? (Y/N)'
|
46
|
-
return puts 'Goodbie!' unless confirm?
|
47
|
-
puts 'Write your name'
|
48
|
-
save(gets.chomp)
|
49
|
-
again?
|
50
|
-
end
|
51
|
-
|
52
|
-
def loose
|
53
|
-
puts 'You loose!'.red
|
54
|
-
again?
|
55
|
-
end
|
56
|
-
|
57
|
-
def again?
|
58
|
-
puts 'Do you want start again? (Y/N)'
|
59
|
-
if confirm?
|
60
|
-
Console.new(true)
|
61
|
-
else
|
62
|
-
puts '-----------Scores----------'.yellow
|
63
|
-
puts @scores
|
64
|
-
puts '---------------------------'.yellow
|
65
|
-
end
|
8
|
+
@game = Game.new(opt)
|
66
9
|
end
|
10
|
+
|
11
|
+
def go(know = true)
|
12
|
+
rules if know
|
13
|
+
puts "Attempt(s): #{@game.attempts} | Hint(s): #{@game.hint_count}"
|
14
|
+
case @game.state
|
15
|
+
when 'true'
|
16
|
+
win
|
17
|
+
when 'false'
|
18
|
+
loose
|
19
|
+
else
|
20
|
+
gaming
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def rules
|
25
|
+
puts "Do you know rules? (Y/N)"
|
26
|
+
rules = [
|
27
|
+
'-----------------Rules!----------------------',
|
28
|
+
"You need guess secret code. This four-digit number with symbols from 1 to 6",
|
29
|
+
"You have #{@game.attempts} attempt(s) and #{@game.hint_count} hint(s)",
|
30
|
+
"If you want get hint write 'hint'",
|
31
|
+
'---------------------------------------------'
|
32
|
+
]
|
33
|
+
unless confirm?
|
34
|
+
puts rules.join("\n")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def gaming
|
39
|
+
action = gets.chomp
|
40
|
+
if action == 'hint'
|
41
|
+
puts @game.hint
|
42
|
+
return go(false)
|
43
|
+
end
|
44
|
+
if @game.valid?(action)
|
45
|
+
puts @game.guess(action)
|
46
|
+
else
|
47
|
+
puts 'Invalid data'
|
48
|
+
end
|
49
|
+
go(false)
|
50
|
+
end
|
67
51
|
|
68
52
|
def confirm?(action = gets.chomp)
|
69
|
-
return true if action == '
|
53
|
+
return true if action.downcase == 'y'
|
70
54
|
false
|
71
55
|
end
|
72
56
|
|
73
|
-
def coloring(string)
|
74
|
-
color_s = ''
|
75
|
-
string.chars { |w| color_s += w == '+' ? '+'.green : '-'.red }
|
76
|
-
color_s
|
77
|
-
end
|
78
|
-
|
79
57
|
def load(path)
|
80
58
|
YAML.load(File.open(path)) if File.exist?(path)
|
81
59
|
end
|
82
60
|
|
83
|
-
def save(name = 'Anonim')
|
84
|
-
|
85
|
-
|
61
|
+
def save!(name = 'Anonim')
|
62
|
+
if @game.state != 'true'
|
63
|
+
return puts 'You cant save game'
|
64
|
+
end
|
65
|
+
name.chomp!
|
66
|
+
@scores << @game.cur_score(name)
|
86
67
|
File.new(@path, 'w') unless File.exist?(@path)
|
87
68
|
File.open(@path, "r+") do |f|
|
88
69
|
f.write(@scores.to_yaml)
|
89
70
|
end
|
90
71
|
@scores
|
91
72
|
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def win
|
77
|
+
puts 'You win!'
|
78
|
+
save?
|
79
|
+
again?
|
80
|
+
end
|
92
81
|
|
82
|
+
def loose
|
83
|
+
puts 'You loose!'
|
84
|
+
again?
|
85
|
+
end
|
86
|
+
|
87
|
+
def save?
|
88
|
+
puts 'Do you want save result? (Y/N)'
|
89
|
+
return puts 'Goodbie!' unless confirm?
|
90
|
+
puts 'Write your name'
|
91
|
+
save!(gets)
|
92
|
+
end
|
93
|
+
|
94
|
+
def again?
|
95
|
+
puts 'Do you want start again? (Y/N)'
|
96
|
+
if confirm?
|
97
|
+
@game = Game.new
|
98
|
+
return go(false)
|
99
|
+
else
|
100
|
+
puts '-----------Scores----------'
|
101
|
+
p @scores
|
102
|
+
puts '---------------------------'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
93
106
|
end
|
94
107
|
end
|
data/lib/codeguessing/game.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Codeguessing
|
2
2
|
class Game
|
3
|
-
|
4
|
-
attr_accessor :secret_code
|
3
|
+
attr_accessor :attempts, :hint_count, :state, :answer, :secret_code
|
5
4
|
|
6
5
|
MAX_HINT = 2
|
7
6
|
MAX_ATTEMPTS = 5
|
@@ -16,14 +15,17 @@ module Codeguessing
|
|
16
15
|
|
17
16
|
def guess(code)
|
18
17
|
loose unless check?(use_attempt)
|
18
|
+
hash = {}
|
19
19
|
res = ''
|
20
20
|
code.each_char.with_index do |char, i|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
case
|
22
|
+
when char == secret_code[i]
|
23
|
+
res += '+'
|
24
|
+
when secret_code.count(char) == 1
|
25
|
+
hash[char] = '-'
|
26
|
+
end
|
26
27
|
end
|
28
|
+
res += hash.values.join('')
|
27
29
|
win if res == '++++'
|
28
30
|
@answer = res
|
29
31
|
end
|
data/lib/codeguessing/scores.yml
CHANGED
@@ -1,22 +1,21 @@
|
|
1
1
|
---
|
2
|
-
- :
|
3
|
-
:
|
4
|
-
:attempts: 4
|
5
|
-
:hint_count: 2
|
6
|
-
:state: true
|
7
|
-
- :name: '1234435'
|
8
|
-
:secret_code: '1234'
|
9
|
-
:attempts: 4
|
2
|
+
- :secret_code: '2222'
|
3
|
+
:attempts: 5
|
10
4
|
:hint_count: 2
|
11
|
-
:
|
12
|
-
|
13
|
-
|
14
|
-
:
|
5
|
+
:name: Yaroslav
|
6
|
+
- :secret_code: '1222'
|
7
|
+
:attempts: 3
|
8
|
+
:hint_count: 1
|
9
|
+
:name: 'Yaroslav
|
10
|
+
|
11
|
+
'
|
12
|
+
- :secret_code: '1351'
|
13
|
+
:attempts: 1
|
14
|
+
:hint_count: 0
|
15
|
+
:name: 'Yaroslav
|
16
|
+
|
17
|
+
'
|
18
|
+
- :secret_code: '1111'
|
15
19
|
:attempts: 4
|
16
|
-
:hint_count:
|
17
|
-
:
|
18
|
-
:answer: "++++"
|
19
|
-
- :secret_code: '1234'
|
20
|
-
:attempts: 4
|
21
|
-
:hint_count: 2
|
22
|
-
:name: Yayryary
|
20
|
+
:hint_count: 0
|
21
|
+
:name: Ivan
|
data/lib/codeguessing/version.rb
CHANGED
data/lib/codeguessing.rb
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codeguessing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.4'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bezrukavyi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
109
|
version: '0'
|
110
110
|
requirements: []
|
111
111
|
rubyforge_project:
|
112
|
-
rubygems_version: 2.
|
112
|
+
rubygems_version: 2.5.1
|
113
113
|
signing_key:
|
114
114
|
specification_version: 4
|
115
115
|
summary: Codeguessing
|