codebreaker_karolina 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.fasterer.yml +20 -0
- data/.overcommit.yml +5 -0
- data/.rubocop.yml +14 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +85 -0
- data/README.md +17 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/codebreaker_karolina/version.rb +3 -0
- data/lib/codebreaker_karolina.rb +3 -0
- data/lib/constants.rb +13 -0
- data/lib/modules/db_yaml.rb +29 -0
- data/lib/modules/difficulty.rb +22 -0
- data/lib/modules/game.rb +69 -0
- data/lib/modules/user.rb +22 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a88d07653eb8fa47f4861847e0dd76e61805fda7f3b082a66c6be8ddc95d2e71
|
4
|
+
data.tar.gz: ce208a5f4ead3195cdbfe992d1c3154d201b74ba4c75900308b4d518698abb05
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b4ad47a6076255af4cfbb3003fc7c3fb3bebe80ae1ba4bc938f7230f4469cc428a192e6adafef23e992921d93329984f2e721feb6e7269505f50b73b71ab40b3
|
7
|
+
data.tar.gz: 98c8892ff95792e93d27ec7cc0e23e80c4302d7732066f310f2377087c34a1d8e130c2d49ac57ac30e138d107a9713626b2f9ab4067abd23bd7fb188fba91747
|
data/.fasterer.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
speedups:
|
2
|
+
rescue_vs_respond_to: true
|
3
|
+
module_eval: true
|
4
|
+
shuffle_first_vs_sample: true
|
5
|
+
for_loop_vs_each: true
|
6
|
+
each_with_index_vs_while: false
|
7
|
+
map_flatten_vs_flat_map: true
|
8
|
+
reverse_each_vs_reverse_each: true
|
9
|
+
select_first_vs_detect: true
|
10
|
+
sort_vs_sort_by: true
|
11
|
+
fetch_with_argument_vs_block: true
|
12
|
+
keys_each_vs_each_key: true
|
13
|
+
hash_merge_bang_vs_hash_brackets: true
|
14
|
+
block_vs_symbol_to_proc: true
|
15
|
+
proc_call_vs_yield: true
|
16
|
+
gsub_vs_tr: true
|
17
|
+
select_last_vs_reverse_detect: true
|
18
|
+
getter_vs_attr_reader: true
|
19
|
+
setter_vs_attr_writer: true
|
20
|
+
|
data/.overcommit.yml
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
codebreaker_karolina (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
childprocess (0.5.9)
|
11
|
+
ffi (~> 1.0, >= 1.0.11)
|
12
|
+
colorize (0.8.1)
|
13
|
+
diff-lcs (1.5.0)
|
14
|
+
docile (1.1.5)
|
15
|
+
fasterer (0.10.0)
|
16
|
+
colorize (~> 0.7)
|
17
|
+
ruby_parser (>= 3.19.1)
|
18
|
+
ffaker (2.21.0)
|
19
|
+
ffi (1.15.5)
|
20
|
+
iniparse (1.5.0)
|
21
|
+
json (2.6.2)
|
22
|
+
overcommit (0.32.0)
|
23
|
+
childprocess (~> 0.5.8)
|
24
|
+
iniparse (~> 1.4)
|
25
|
+
parallel (1.22.1)
|
26
|
+
parser (3.1.2.1)
|
27
|
+
ast (~> 2.4.1)
|
28
|
+
rainbow (3.1.1)
|
29
|
+
rake (13.0.6)
|
30
|
+
regexp_parser (2.5.0)
|
31
|
+
rexml (3.2.5)
|
32
|
+
rspec (3.11.0)
|
33
|
+
rspec-core (~> 3.11.0)
|
34
|
+
rspec-expectations (~> 3.11.0)
|
35
|
+
rspec-mocks (~> 3.11.0)
|
36
|
+
rspec-core (3.11.0)
|
37
|
+
rspec-support (~> 3.11.0)
|
38
|
+
rspec-expectations (3.11.0)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.11.0)
|
41
|
+
rspec-mocks (3.11.1)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.11.0)
|
44
|
+
rspec-support (3.11.0)
|
45
|
+
rubocop (1.36.0)
|
46
|
+
json (~> 2.3)
|
47
|
+
parallel (~> 1.10)
|
48
|
+
parser (>= 3.1.2.1)
|
49
|
+
rainbow (>= 2.2.2, < 4.0)
|
50
|
+
regexp_parser (>= 1.8, < 3.0)
|
51
|
+
rexml (>= 3.2.5, < 4.0)
|
52
|
+
rubocop-ast (>= 1.20.1, < 2.0)
|
53
|
+
ruby-progressbar (~> 1.7)
|
54
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
55
|
+
rubocop-ast (1.21.0)
|
56
|
+
parser (>= 3.1.1.0)
|
57
|
+
rubocop-rspec (2.12.1)
|
58
|
+
rubocop (~> 1.31)
|
59
|
+
ruby-progressbar (1.11.0)
|
60
|
+
ruby_parser (3.19.1)
|
61
|
+
sexp_processor (~> 4.16)
|
62
|
+
sexp_processor (4.16.1)
|
63
|
+
simplecov (0.12.0)
|
64
|
+
docile (~> 1.1.0)
|
65
|
+
json (>= 1.8, < 3)
|
66
|
+
simplecov-html (~> 0.10.0)
|
67
|
+
simplecov-html (0.10.2)
|
68
|
+
unicode-display_width (2.2.0)
|
69
|
+
|
70
|
+
PLATFORMS
|
71
|
+
x86_64-linux
|
72
|
+
|
73
|
+
DEPENDENCIES
|
74
|
+
codebreaker_karolina!
|
75
|
+
fasterer (~> 0.10.0)
|
76
|
+
ffaker
|
77
|
+
overcommit (~> 0.32.0)
|
78
|
+
rake (~> 13.0)
|
79
|
+
rspec (~> 3.11)
|
80
|
+
rubocop (~> 1.36)
|
81
|
+
rubocop-rspec
|
82
|
+
simplecov (~> 0.12.0)
|
83
|
+
|
84
|
+
BUNDLED WITH
|
85
|
+
2.2.29
|
data/README.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# CodebreakerKarolina
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'codebreaker_karolina'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install codebreaker_karolina
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'codebreaker_karolina'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/constants.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module CodebreakerConstants
|
2
|
+
PLUS = '+'.freeze
|
3
|
+
MINUS = '-'.freeze
|
4
|
+
FILE_NAME = 'score.yaml'.freeze
|
5
|
+
NUM_RANGE = (1..6).freeze
|
6
|
+
CODE_SIZE = 4
|
7
|
+
DIFFICULTY = {
|
8
|
+
easy: { attempts: 15, hints: 2 },
|
9
|
+
medium: { attempts: 10, hints: 1 },
|
10
|
+
hell: { attempts: 5, hints: 1 }
|
11
|
+
}.freeze
|
12
|
+
DIFFICULTY_SORT = { easy: 1, medium: 2, hell: 3 }.freeze
|
13
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative '../constants'
|
2
|
+
require_relative 'game'
|
3
|
+
require 'yaml'
|
4
|
+
module CodebreakerKarolina
|
5
|
+
class DBYaml
|
6
|
+
include CodebreakerConstants
|
7
|
+
|
8
|
+
def save(game)
|
9
|
+
File.open(FILE_NAME, 'a') do |filename|
|
10
|
+
YAML.dump(game, filename)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def statistic
|
15
|
+
games = load_file_stats
|
16
|
+
return if games.empty?
|
17
|
+
|
18
|
+
games.sort_by do |game|
|
19
|
+
[-DIFFICULTY_SORT[game.difficulty.name.to_sym], game.user.used_attempts, game.user.used_hints]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def load_file_stats
|
26
|
+
YAML.load_stream(File.open(FILE_NAME))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative '../constants'
|
2
|
+
class Difficulty
|
3
|
+
include CodebreakerConstants
|
4
|
+
|
5
|
+
attr_accessor :attempts, :hints, :name
|
6
|
+
|
7
|
+
def initialize(name)
|
8
|
+
return unless valid?(name)
|
9
|
+
|
10
|
+
@name = name
|
11
|
+
@hints = DIFFICULTY[name.to_sym][:hints]
|
12
|
+
@attempts = DIFFICULTY[name.to_sym][:attempts]
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def valid?(name)
|
18
|
+
return true if DIFFICULTY.include? name.to_sym
|
19
|
+
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
data/lib/modules/game.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require_relative '../constants'
|
2
|
+
require_relative 'user'
|
3
|
+
require_relative 'difficulty'
|
4
|
+
module CodebreakerKarolina
|
5
|
+
class Game
|
6
|
+
include CodebreakerConstants
|
7
|
+
attr_accessor :user, :difficulty
|
8
|
+
|
9
|
+
def start
|
10
|
+
@secret_code = generate_code.freeze
|
11
|
+
@hint = @secret_code.chars.uniq
|
12
|
+
end
|
13
|
+
|
14
|
+
def compare_code(guess)
|
15
|
+
return unless valid_code?(guess)
|
16
|
+
|
17
|
+
@user.used_attempts += 1
|
18
|
+
comparison_result(@secret_code.chars, guess.chars)
|
19
|
+
end
|
20
|
+
|
21
|
+
def win?(guess)
|
22
|
+
return true if @secret_code == guess
|
23
|
+
|
24
|
+
false
|
25
|
+
end
|
26
|
+
|
27
|
+
def lose?
|
28
|
+
return true if @user.used_attempts >= @difficulty.attempts
|
29
|
+
|
30
|
+
false
|
31
|
+
end
|
32
|
+
|
33
|
+
def hint
|
34
|
+
return unless hints_left?
|
35
|
+
|
36
|
+
@user.used_hints += 1
|
37
|
+
@hint.shuffle.pop
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def valid_code?(code)
|
43
|
+
return true if code =~ (/[1-6]{4}/)
|
44
|
+
|
45
|
+
false
|
46
|
+
end
|
47
|
+
|
48
|
+
def generate_code
|
49
|
+
Array.new(CODE_SIZE) { rand(NUM_RANGE) }.join
|
50
|
+
end
|
51
|
+
|
52
|
+
def comparison_result(secret_code, user_code)
|
53
|
+
result = ''
|
54
|
+
user_code.each_with_index do |num, index|
|
55
|
+
next unless secret_code.include? num
|
56
|
+
|
57
|
+
result += num == secret_code[index] ? PLUS : MINUS
|
58
|
+
secret_code[secret_code.index(num)] = nil
|
59
|
+
end
|
60
|
+
result.chars.sort.join
|
61
|
+
end
|
62
|
+
|
63
|
+
def hints_left?
|
64
|
+
return true if @difficulty.hints > @user.used_hints
|
65
|
+
|
66
|
+
false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/modules/user.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class User
|
2
|
+
attr_accessor :used_attempts, :used_hints
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@used_attempts = 0
|
6
|
+
@used_hints = 0
|
7
|
+
end
|
8
|
+
|
9
|
+
def name(name)
|
10
|
+
return unless valid?(name)
|
11
|
+
|
12
|
+
@name = name
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def valid?(name)
|
18
|
+
return true if name.match(/[A-Za-z0-9_]{3,20}/)
|
19
|
+
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codebreaker_karolina
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- drkarolina
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-09-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: fasterer
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.10.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.10.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ffaker
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: overcommit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.32.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.32.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.11'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.11'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.36'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.36'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.12.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.12.0
|
111
|
+
description: Codebreaker game
|
112
|
+
email:
|
113
|
+
- k.drahanchuk@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".fasterer.yml"
|
119
|
+
- ".overcommit.yml"
|
120
|
+
- ".rubocop.yml"
|
121
|
+
- Gemfile
|
122
|
+
- Gemfile.lock
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- lib/codebreaker_karolina.rb
|
128
|
+
- lib/codebreaker_karolina/version.rb
|
129
|
+
- lib/constants.rb
|
130
|
+
- lib/modules/db_yaml.rb
|
131
|
+
- lib/modules/difficulty.rb
|
132
|
+
- lib/modules/game.rb
|
133
|
+
- lib/modules/user.rb
|
134
|
+
homepage: https://github.com/drkarolina/codebreaker
|
135
|
+
licenses: []
|
136
|
+
metadata:
|
137
|
+
homepage_uri: https://github.com/drkarolina/codebreaker
|
138
|
+
source_code_uri: https://github.com/drkarolina/codebreaker
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: 2.6.0
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubygems_version: 3.2.22
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: Codebreaker game
|
158
|
+
test_files: []
|