codebraker_ov 0.0.8 → 0.0.10
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/lib/codebraker_ov.rb +7 -3
- metadata +1 -4
- data/lib/entities/game.rb +0 -100
- data/lib/entities/player.rb +0 -38
- data/lib/entities/stats.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33a5c696d4383bb9cbfbc0e27d8e0cc85a9c63abde5067dbf07ada2c47a9e584
|
4
|
+
data.tar.gz: 713d9852509266c7187be385beb28a6726c87d34019081aee0bb30f54736b1d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74d8b91a5ddffec7d8a4de530c507d4b5729fb7fd8b2cf091d6b89ef411f478306b09594486e75d042d58a1bb9114cf386e1736dd37280f3ab9b2b4cadff523c
|
7
|
+
data.tar.gz: 2402881c80cf3f8dba5a023d804223fc88fa7cd81e682f9f7b4ec83e64df0fce3e40bcc0917869b69b56a8e03acb08bdce998269dcd79e46e6d0b2a80d6a967d
|
data/lib/codebraker_ov.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
require 'i18n'
|
3
3
|
require 'pry'
|
4
|
-
|
5
|
-
require_relative './
|
6
|
-
require_relative './
|
4
|
+
|
5
|
+
require_relative './codebreaker_ov/utils/errors'
|
6
|
+
require_relative './codebreaker_ov/utils/validation'
|
7
|
+
|
8
|
+
require_relative './codebreaker_ov/entities/game.rb'
|
9
|
+
require_relative './codebreaker_ov/entities/stats.rb'
|
10
|
+
require_relative './codebreaker_ov/entities/player.rb'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codebraker_ov
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleh Volynets
|
@@ -17,9 +17,6 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- lib/codebraker_ov.rb
|
20
|
-
- lib/entities/game.rb
|
21
|
-
- lib/entities/player.rb
|
22
|
-
- lib/entities/stats.rb
|
23
20
|
homepage: https://github.com/olehvolynets/codebraker.git
|
24
21
|
licenses:
|
25
22
|
- MIT
|
data/lib/entities/game.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
module Codebreaker
|
2
|
-
class Game
|
3
|
-
attr_reader(
|
4
|
-
:secret,
|
5
|
-
:attempts_total,
|
6
|
-
:attempts,
|
7
|
-
:hints_available,
|
8
|
-
:hints_used,
|
9
|
-
:hints_given,
|
10
|
-
:result,
|
11
|
-
:level_name
|
12
|
-
)
|
13
|
-
|
14
|
-
DIFFICULTY = {
|
15
|
-
low: { attempts: 15, hints: 2 },
|
16
|
-
medium: { attempts: 10, hints: 2 },
|
17
|
-
hell: { attempts: 5, hints: 1 }
|
18
|
-
}.freeze
|
19
|
-
|
20
|
-
def initialize(level)
|
21
|
-
@secret = ''
|
22
|
-
@level_name = level.to_s
|
23
|
-
@attempts_total = DIFFICULTY[level][:attempts]
|
24
|
-
@attempts = DIFFICULTY[level][:attempts]
|
25
|
-
@hints_available = DIFFICULTY[level][:hints]
|
26
|
-
@hints_used = 0
|
27
|
-
@hints_given = []
|
28
|
-
@result = nil
|
29
|
-
|
30
|
-
generate_number
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
def generate_number
|
36
|
-
4.times { @secret += Random.rand(1..6).to_s }
|
37
|
-
end
|
38
|
-
|
39
|
-
def check_full_matches(guess, secret_copy)
|
40
|
-
result = ''
|
41
|
-
|
42
|
-
(0..3).each do |i|
|
43
|
-
if secret_copy[i] == guess[i]
|
44
|
-
result += '+'
|
45
|
-
secret_copy[i] = ' '
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
[result, secret_copy]
|
50
|
-
end
|
51
|
-
|
52
|
-
def check_partial_matches(guess, secret_copy)
|
53
|
-
result = ''
|
54
|
-
|
55
|
-
(0..3).each do |i|
|
56
|
-
index = secret_copy.index(guess[i])
|
57
|
-
if index
|
58
|
-
result += '-'
|
59
|
-
secret_copy[index] = ' '
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
[result, secret_copy]
|
64
|
-
end
|
65
|
-
|
66
|
-
public
|
67
|
-
|
68
|
-
def compare_with(guess)
|
69
|
-
secret_copy = @secret.slice(0..-1)
|
70
|
-
|
71
|
-
full_matches, secret_copy = check_full_matches(guess, secret_copy)
|
72
|
-
partial_matches, _secret_copy = check_partial_matches(guess, secret_copy)
|
73
|
-
result = full_matches + partial_matches
|
74
|
-
@attempts -= 1
|
75
|
-
|
76
|
-
@result = result.chars.sort.join
|
77
|
-
@result
|
78
|
-
end
|
79
|
-
|
80
|
-
# rubocop:disable Naming/AccessorMethodName
|
81
|
-
def get_hint
|
82
|
-
return unless @hints_available.positive?
|
83
|
-
|
84
|
-
number = @secret[Random.rand(0..3)]
|
85
|
-
|
86
|
-
number = @secret[Random.rand(0..3)] until @hints_given.index(number).nil?
|
87
|
-
|
88
|
-
@hints_available -= 1
|
89
|
-
@hints_used += 1
|
90
|
-
@hints_given.push(number)
|
91
|
-
|
92
|
-
number
|
93
|
-
end
|
94
|
-
# rubocop:enable Naming/AccessorMethodName
|
95
|
-
|
96
|
-
def self.validate_answer(guess)
|
97
|
-
guess.instance_of?(String) && guess.length == 4
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
data/lib/entities/player.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
module Codebreaker
|
2
|
-
class Player
|
3
|
-
attr_accessor(
|
4
|
-
:rating,
|
5
|
-
:name,
|
6
|
-
:difficulty,
|
7
|
-
:attempts_ttl,
|
8
|
-
:attempts_used,
|
9
|
-
:hints_ttl,
|
10
|
-
:hints_used,
|
11
|
-
:date
|
12
|
-
)
|
13
|
-
|
14
|
-
def initialize(name, difficulty, game)
|
15
|
-
@name = name
|
16
|
-
@difficulty = difficulty
|
17
|
-
@attempts_ttl = game.attempts_total
|
18
|
-
@attempts_used = nil
|
19
|
-
@hints_ttl = game.hints_available
|
20
|
-
@hints_used = nil
|
21
|
-
@rating = nil
|
22
|
-
@date = nil
|
23
|
-
end
|
24
|
-
|
25
|
-
def <=>(other)
|
26
|
-
[other.hints_used, other.attempts_used] <=> [@hints_used, @attempts_used]
|
27
|
-
end
|
28
|
-
|
29
|
-
def on_win(attempts:, hints:)
|
30
|
-
@attempts_used = @attempts_ttl - attempts
|
31
|
-
@hints_used = hints
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.valid?(name)
|
35
|
-
name.instance_of?(String) && name.length > 2 && name.length < 21
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
data/lib/entities/stats.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
# :nocov:
|
2
|
-
module Codebreaker
|
3
|
-
class Stats
|
4
|
-
STORE = 'db/stats.yml'.freeze
|
5
|
-
|
6
|
-
class << self
|
7
|
-
def write(player)
|
8
|
-
data = load
|
9
|
-
data.push(player)
|
10
|
-
|
11
|
-
sorted = data.sort
|
12
|
-
|
13
|
-
if sorted.length >= 1
|
14
|
-
(0..(sorted.length - 1)).each do |i|
|
15
|
-
sorted[i].rating = i + 1
|
16
|
-
end
|
17
|
-
else
|
18
|
-
sorted[0].rating = 1
|
19
|
-
end
|
20
|
-
|
21
|
-
File.write(STORE, sorted.to_yaml)
|
22
|
-
end
|
23
|
-
|
24
|
-
def load
|
25
|
-
File.exist?(STORE) ? YAML.load_file(STORE) : []
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
# :nocov:
|