codebraker_ov 0.0.2
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 +7 -0
- data/lib/entities/game.rb +98 -0
- data/lib/entities/player.rb +32 -0
- data/lib/entities/stats.rb +37 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 99bc32131c4c2c5ef95a09f24a6b1bd8bf557f15aa2895b504d15d8984a71f55
|
4
|
+
data.tar.gz: 27445ad1a365dbe98d60a2b2a0edba3cf4005c9bf15c24417f6d613461244738
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e211b17edefe3f52313b20d4a240de11965c8b61e2fd95b6c827b8a5c119fc4f9951ebf8d360a0fb4b01726de72b744c8e80d9e82f8e3a6dd283fac8f9d9707
|
7
|
+
data.tar.gz: af04e03202d1fc87091b492bcfcb2e8bcaa15ecdd88d271e126c930728100745b66d4fdd13c969e94e63073f079c5138af77cc5181d8c91e3470114e0bbb050d
|
@@ -0,0 +1,98 @@
|
|
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
|
+
include Validation
|
21
|
+
|
22
|
+
def initialize(level)
|
23
|
+
@secret = ''
|
24
|
+
@level_name = level.to_s
|
25
|
+
@attempts_total = DIFFICULTY[level][:attempts]
|
26
|
+
@attempts = DIFFICULTY[level][:attempts]
|
27
|
+
@hints_available = DIFFICULTY[level][:hints]
|
28
|
+
@hints_used = 0
|
29
|
+
@hints_given = []
|
30
|
+
|
31
|
+
generate_number
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def generate_number
|
37
|
+
4.times { @secret += Random.rand(1..6).to_s }
|
38
|
+
end
|
39
|
+
|
40
|
+
def check_full_matches(guess, secret_copy)
|
41
|
+
result = ''
|
42
|
+
|
43
|
+
(0..3).each do |i|
|
44
|
+
if secret_copy[i] == guess[i]
|
45
|
+
result += '+'
|
46
|
+
secret_copy[i] = ' '
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
[result, secret_copy]
|
51
|
+
end
|
52
|
+
|
53
|
+
def check_partial_matches(guess, secret_copy)
|
54
|
+
result = ''
|
55
|
+
|
56
|
+
(0..3).each do |i|
|
57
|
+
index = secret_copy.index(guess[i])
|
58
|
+
if index
|
59
|
+
result += '-'
|
60
|
+
secret_copy[index] = ' '
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
[result, secret_copy]
|
65
|
+
end
|
66
|
+
|
67
|
+
public
|
68
|
+
|
69
|
+
def compare_with(guess)
|
70
|
+
return unless validate_answer(guess)
|
71
|
+
|
72
|
+
secret_copy = @secret.slice(0..-1)
|
73
|
+
|
74
|
+
full_matches, secret_copy = check_full_matches(guess, secret_copy)
|
75
|
+
partial_matches, _secret_copy = check_partial_matches(guess, secret_copy)
|
76
|
+
result = full_matches + partial_matches
|
77
|
+
@attempts -= 1
|
78
|
+
|
79
|
+
result.chars.sort.join
|
80
|
+
end
|
81
|
+
|
82
|
+
# rubocop:disable Naming/AccessorMethodName
|
83
|
+
def get_hint
|
84
|
+
return unless @hints_available.positive?
|
85
|
+
|
86
|
+
number = @secret[Random.rand(0..3)]
|
87
|
+
|
88
|
+
number = @secret[Random.rand(0..3)] until @hints_given.index(number).nil?
|
89
|
+
|
90
|
+
@hints_available -= 1
|
91
|
+
@hints_used += 1
|
92
|
+
@hints_given.push(number)
|
93
|
+
|
94
|
+
number
|
95
|
+
end
|
96
|
+
# rubocop:enable Naming/AccessorMethodName
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,32 @@
|
|
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
|
+
)
|
12
|
+
|
13
|
+
def initialize(name, difficulty, game)
|
14
|
+
@name = name
|
15
|
+
@difficulty = difficulty
|
16
|
+
@attempts_ttl = game.attempts_total
|
17
|
+
@attempts_used = nil
|
18
|
+
@hints_ttl = hints_ttl
|
19
|
+
@hints_used = nil
|
20
|
+
@rating = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def <=>(other)
|
24
|
+
[other.hints_used, other.attempts_used] <=> [@hints_used, @attempts_used]
|
25
|
+
end
|
26
|
+
|
27
|
+
def on_win(attempts:, hints:)
|
28
|
+
@attempts_used = @attempts_ttl - attempts
|
29
|
+
@hints_used = hints
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# :nocov:
|
2
|
+
module Codebreaker
|
3
|
+
class Stats
|
4
|
+
STORE = 'db/stats.yml'.freeze
|
5
|
+
|
6
|
+
include Table
|
7
|
+
|
8
|
+
alias table_show show
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def write(player)
|
12
|
+
@data.push(player)
|
13
|
+
|
14
|
+
sorted = @data.sort
|
15
|
+
|
16
|
+
if sorted.length >= 1
|
17
|
+
(0..(sorted.length - 1)).each do |i|
|
18
|
+
sorted[i].rating = i + 1
|
19
|
+
end
|
20
|
+
else
|
21
|
+
sorted[0].rating = 1
|
22
|
+
end
|
23
|
+
|
24
|
+
File.write(@path, sorted.to_yaml)
|
25
|
+
end
|
26
|
+
|
27
|
+
def show
|
28
|
+
table_show(load)
|
29
|
+
end
|
30
|
+
|
31
|
+
def load
|
32
|
+
File.exist?(STORE) ? YAML.load_file(STORE) : []
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
# :nocov:
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codebraker_ov
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Oleh Volynets
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A codebraker gem for RubyGarage ruby classes
|
14
|
+
email: saggot91@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/entities/game.rb
|
20
|
+
- lib/entities/player.rb
|
21
|
+
- lib/entities/stats.rb
|
22
|
+
homepage: https://github.com/olehvolynets/codebraker.git
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubygems_version: 3.0.4
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: nil
|
45
|
+
test_files: []
|