codebreaker_first 0.3.0 → 0.4.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 +5 -5
- data/Gemfile.lock +3 -2
- data/codebreaker_first-0.3.0.gem +0 -0
- data/lib/codebreaker_first.rb +94 -89
- data/lib/codebreaker_first/version.rb +3 -3
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b9f8d8ad4df694e3b5dd3f758a95a793ef25dca6
|
4
|
+
data.tar.gz: 54855051f38b22534551e60777666ddfc017c6df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5669e3a4efff7d7879ad77203974b7b591580e165c666a5c3078a20f032c50c43df9efdd614986bee02014bccfc1c5dc5f33ff0beb6a624380b1159622bcb843
|
7
|
+
data.tar.gz: d7f5a4e99c4a97815b06d425395b33cd122f9bb9801b223aaa655035c579b63804fcac9567bb00a736346d41dce9cd254f91b5509a5b6ae84f85b0ed3fa4cef1
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
codebreaker_first (0.
|
4
|
+
codebreaker_first (0.4.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -23,6 +23,7 @@ GEM
|
|
23
23
|
rspec-support (3.8.0)
|
24
24
|
|
25
25
|
PLATFORMS
|
26
|
+
ruby
|
26
27
|
x64-mingw32
|
27
28
|
|
28
29
|
DEPENDENCIES
|
@@ -32,4 +33,4 @@ DEPENDENCIES
|
|
32
33
|
rspec (~> 3.0)
|
33
34
|
|
34
35
|
BUNDLED WITH
|
35
|
-
1.16.
|
36
|
+
1.16.6
|
Binary file
|
data/lib/codebreaker_first.rb
CHANGED
@@ -1,89 +1,94 @@
|
|
1
|
-
require 'json'
|
2
|
-
require_relative 'codebreaker_first/version'
|
3
|
-
|
4
|
-
module Codebreaker
|
5
|
-
MAX_TRIES = 6
|
6
|
-
|
7
|
-
# Rubocop
|
8
|
-
class Game
|
9
|
-
attr_accessor :code, :user_tries, :game_result
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
@code =
|
13
|
-
@user_tries =
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
1
|
+
require 'json'
|
2
|
+
require_relative 'codebreaker_first/version'
|
3
|
+
|
4
|
+
module Codebreaker
|
5
|
+
MAX_TRIES = 6
|
6
|
+
|
7
|
+
# Rubocop
|
8
|
+
class Game
|
9
|
+
attr_accessor :code, :user_tries, :game_result
|
10
|
+
|
11
|
+
def initialize(code = '', user_tries = 0, game_result = nil)
|
12
|
+
@code = code
|
13
|
+
@user_tries = user_tries
|
14
|
+
@game_result = game_result
|
15
|
+
end
|
16
|
+
|
17
|
+
def from_hash(data)
|
18
|
+
Game.new data['code'], data['user_tries'], data['game_result']
|
19
|
+
end
|
20
|
+
|
21
|
+
def start
|
22
|
+
4.times do
|
23
|
+
@code << rand(1...6).to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def check_code(input_code)
|
28
|
+
@user_tries += 1
|
29
|
+
return loose if @user_tries >= MAX_TRIES
|
30
|
+
return win if input_code == @code
|
31
|
+
|
32
|
+
validate_code input_code
|
33
|
+
|
34
|
+
result = ''
|
35
|
+
|
36
|
+
@code.chars.each.with_index do |char, index|
|
37
|
+
result << '+' if char == input_code[index]
|
38
|
+
|
39
|
+
result << '-' unless char == input_code[index]
|
40
|
+
end
|
41
|
+
|
42
|
+
result
|
43
|
+
end
|
44
|
+
|
45
|
+
def win
|
46
|
+
@game_result = true
|
47
|
+
end
|
48
|
+
|
49
|
+
def loose
|
50
|
+
@game_result = false
|
51
|
+
end
|
52
|
+
|
53
|
+
def save_result(name)
|
54
|
+
raise Exception, 'To save, you must finish the game!' if @game_result.nil?
|
55
|
+
|
56
|
+
Result.save name, @user_tries, @game_result
|
57
|
+
end
|
58
|
+
|
59
|
+
def results
|
60
|
+
Result.results
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def validate_code(code)
|
66
|
+
raise Exception unless code.is_a?(Numeric) || code.is_a?(String)
|
67
|
+
|
68
|
+
code = code.to_s
|
69
|
+
|
70
|
+
raise Exception, 'Code must contains 4 numbers from 1 to 6' unless code =~ /^[1-6]{4}$/
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Rubocop
|
75
|
+
class Result
|
76
|
+
SCORES_FILE = File.expand_path('scores.json', __dir__)
|
77
|
+
|
78
|
+
def self.save(name, tries, result)
|
79
|
+
scores = results.to_a
|
80
|
+
|
81
|
+
scores << { name: name, tries: tries, result: result }
|
82
|
+
|
83
|
+
File.write SCORES_FILE, scores.to_json
|
84
|
+
|
85
|
+
scores
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.results
|
89
|
+
file = File.read SCORES_FILE
|
90
|
+
|
91
|
+
JSON.parse file
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module Codebreaker
|
2
|
-
VERSION = '0.
|
3
|
-
end
|
1
|
+
module Codebreaker
|
2
|
+
VERSION = '0.4.0'.freeze
|
3
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codebreaker_first
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- shindakioku
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- codebreaker_first-0.2.0.gem
|
73
|
+
- codebreaker_first-0.3.0.gem
|
73
74
|
- codebreaker_first.gemspec
|
74
75
|
- lib/codebreaker_first.rb
|
75
76
|
- lib/codebreaker_first/version.rb
|
@@ -95,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
96
|
version: '0'
|
96
97
|
requirements: []
|
97
98
|
rubyforge_project:
|
98
|
-
rubygems_version: 2.
|
99
|
+
rubygems_version: 2.5.2.1
|
99
100
|
signing_key:
|
100
101
|
specification_version: 4
|
101
102
|
summary: Write a short summary, because RubyGems requires one.
|