codebreaker_kirill 0.2.12 → 0.2.15
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/codebreaker_kirill/code_generator.rb +9 -0
- data/lib/codebreaker_kirill/game.rb +14 -22
- data/lib/codebreaker_kirill/guess_handler.rb +13 -7
- data/lib/codebreaker_kirill/settings.rb +11 -0
- data/lib/codebreaker_kirill/stats.rb +13 -0
- data/lib/codebreaker_kirill/stats.yml +34 -0
- data/lib/codebreaker_kirill/user.rb +12 -2
- data/lib/codebreaker_kirill/validations.rb +19 -0
- data/lib/codebreaker_kirill/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d4ad4a1d6b0e6708f1f0f1c46c8f1bfcd13102bf91a6b5de1f850276ea53d27
|
4
|
+
data.tar.gz: de693c9580df81f56f28b96ae4adb751a7642ec2927825c96a42643d7183b359
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df0df3cafb0cd72e06b8e41107d1853800575f9a55bfc0441054ff949f7a3009a64aca5a5f3acc61fdf35e1d2b989962afc8a915cfc487266e0b2d5b44a232bf
|
7
|
+
data.tar.gz: cfa8a264bf9458306436f01a6b6ec3f6c940326d03c6ba5cb324a295fd05e12d95b29c1c5f09ebbab3ceca675f80c9c1f50dcd31c8f37659eef7536aad62c68c
|
@@ -1,37 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'yaml'
|
4
|
+
require_relative 'validations'
|
3
5
|
require_relative 'guess_handler'
|
4
6
|
require_relative 'user'
|
7
|
+
require_relative 'settings'
|
8
|
+
require_relative 'code_generator'
|
9
|
+
require_relative 'stats'
|
5
10
|
|
6
11
|
class Game
|
7
12
|
include GuessHandler
|
13
|
+
include Validations
|
14
|
+
include CodeGenerator
|
15
|
+
include Settings
|
8
16
|
|
9
|
-
attr_reader :secret_code, :
|
17
|
+
attr_reader :secret_code, :user
|
10
18
|
|
11
|
-
def initialize(user
|
12
|
-
@secret_code =
|
13
|
-
@
|
14
|
-
@attempts = { all: difficulty[:attempts], used: 0 }
|
15
|
-
@hints = { all: difficulty[:hints], used: 0 }
|
16
|
-
@name = user.name
|
19
|
+
def initialize(user)
|
20
|
+
@secret_code = CodeGenerator.call
|
21
|
+
@user = user
|
17
22
|
end
|
18
23
|
|
19
24
|
def give_a_hint
|
20
|
-
return 0 if @hints[:
|
21
|
-
|
22
|
-
@hints[:all] -= 1
|
23
|
-
@hints[:used] += 1
|
25
|
+
return 0 if @user.hints[:used] >= @user.hints[:all]
|
26
|
+
@user.hints[:used] += 1
|
24
27
|
@secret_code[@hints[:used]]
|
25
28
|
end
|
26
29
|
end
|
27
|
-
|
28
|
-
game = Game.new(User.new("kirill"), { attempts: 10, hints: 2 })
|
29
|
-
print game.respond_to_guess(game, "2222", [1,2,3,4]) # +
|
30
|
-
print game.respond_to_guess(game, "2222", [2,2,2,4]) # +++
|
31
|
-
print game.respond_to_guess(game, "2211", [1,2,3,4]) # +-
|
32
|
-
print game.respond_to_guess(game, "2221", [1,2,3,2]) # +--
|
33
|
-
print game.respond_to_guess(game, "1212", [2,1,2,1]) # ----
|
34
|
-
print game.respond_to_guess(game, "6666", [1,1,1,1]) # []
|
35
|
-
print game.respond_to_guess(game, "6666", [6,6,6,6]) # win
|
36
|
-
print game.respond_to_guess(game, "1234", [5,3,6,3]) # -
|
37
|
-
print game.respond_to_guess(game, "5363", [1,2,3,4]) # -
|
@@ -2,23 +2,29 @@
|
|
2
2
|
|
3
3
|
module GuessHandler
|
4
4
|
def respond_to_guess(game, input, code)
|
5
|
+
validate_guess(input)
|
5
6
|
@result = []
|
6
7
|
@code_clone = code.clone
|
7
8
|
@numbers = input.split('').map(&:to_i)
|
8
9
|
return 'win' if @numbers == code
|
9
10
|
|
10
11
|
assess_guess(@code_clone, @numbers)
|
11
|
-
|
12
|
-
game.attempts[:used]
|
12
|
+
|
13
|
+
game.user.attempts[:used] += 1
|
14
|
+
game.user.attempts[:used] >= game.attempts[:all] ? 'loss' : @result.delete_if { |value| (1..6).include?(value) }.sort
|
13
15
|
end
|
14
16
|
|
15
17
|
def assess_guess(code, input)
|
16
|
-
|
18
|
+
@numbers.each_with_index do |element, index|
|
19
|
+
if element == code[index]
|
20
|
+
code[index] = '+'
|
21
|
+
input[@numbers.index(element)] = '' end
|
22
|
+
end
|
23
|
+
|
17
24
|
@result = code.each_with_index do |el, index|
|
18
|
-
if
|
19
|
-
|
20
|
-
input.slice!(@numbers.index(el))
|
21
|
-
end
|
25
|
+
if @numbers.include?(el)
|
26
|
+
code[index] = '-'
|
27
|
+
input.slice!(@numbers.index(el)) end
|
22
28
|
end
|
23
29
|
end
|
24
30
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Stats
|
2
|
+
def self.save_game(user)
|
3
|
+
@data = File.exist?("stats.yml") ? YAML.load_file("stats.yml") : []
|
4
|
+
@data << user
|
5
|
+
File.new("stats.yml", 'w') unless File.exist?("stats.yml")
|
6
|
+
File.open("stats.yml", 'w') { |file| file.write(@data.to_yaml) }
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.show_stats
|
10
|
+
@data = File.exist?("stats.yml") ? YAML.load_file("stats.yml") : []
|
11
|
+
print @data
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
---
|
2
|
+
- !ruby/object:User
|
3
|
+
name: dima
|
4
|
+
difficulty:
|
5
|
+
:attempts: 10
|
6
|
+
:hints: 1
|
7
|
+
attempts:
|
8
|
+
:all: 10
|
9
|
+
:used: 0
|
10
|
+
hints:
|
11
|
+
:all: 1
|
12
|
+
:used: 0
|
13
|
+
- !ruby/object:User
|
14
|
+
name: stacy
|
15
|
+
difficulty:
|
16
|
+
:attempts: 5
|
17
|
+
:hints: 1
|
18
|
+
attempts:
|
19
|
+
:all: 5
|
20
|
+
:used: 0
|
21
|
+
hints:
|
22
|
+
:all: 1
|
23
|
+
:used: 0
|
24
|
+
- !ruby/object:User
|
25
|
+
name: kirill
|
26
|
+
difficulty:
|
27
|
+
:attempts: 5
|
28
|
+
:hints: 1
|
29
|
+
attempts:
|
30
|
+
:all: 5
|
31
|
+
:used: 0
|
32
|
+
hints:
|
33
|
+
:all: 1
|
34
|
+
:used: 0
|
@@ -1,9 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
require_relative "validations"
|
3
|
+
require_relative "settings"
|
2
4
|
|
3
5
|
class User
|
4
|
-
|
6
|
+
include Validations
|
7
|
+
include Settings
|
5
8
|
|
6
|
-
|
9
|
+
attr_reader :name, :difficulty, :attempts, :hints
|
10
|
+
|
11
|
+
def initialize(name, difficulty)
|
12
|
+
validate_name(name)
|
13
|
+
validate_difficulty(difficulty)
|
7
14
|
@name = name
|
15
|
+
@difficulty = Settings::LEVEL[difficulty]
|
16
|
+
@attempts = { all: @difficulty[:attempts], used: 0 }
|
17
|
+
@hints = { all: @difficulty[:hints], used: 0 }
|
8
18
|
end
|
9
19
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Validations
|
2
|
+
def validate_name(name)
|
3
|
+
raise "Name shouldn't be empty" if name.empty?
|
4
|
+
raise 'Name should be string' if name.class != String
|
5
|
+
raise 'Name should be at least 3 characters long' if name.length < 3
|
6
|
+
raise "Name shouldn't be more than 20 characters long" if name.length > 20
|
7
|
+
end
|
8
|
+
|
9
|
+
def validate_guess(guess)
|
10
|
+
raise "Name shouldn't be empty" if guess.empty?
|
11
|
+
raise 'Name should be at least 3 characters long' if guess.length < 4
|
12
|
+
raise 'Name shouldn\'t be more than 4 characters long' if guess.length > 4
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_difficulty(difficulty)
|
16
|
+
raise "Input shouldn't be empty" if difficulty.empty?
|
17
|
+
raise 'You should enter one of the following options: easy, medium, hell' unless difficulty.match?(/easy|medium|hell/)
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codebreaker_kirill
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kirill Dudchenko
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-08-
|
11
|
+
date: 2022-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -27,9 +27,14 @@ files:
|
|
27
27
|
- Rakefile
|
28
28
|
- lib/codebreaker_kirill.rb
|
29
29
|
- lib/codebreaker_kirill/autoload.rb
|
30
|
+
- lib/codebreaker_kirill/code_generator.rb
|
30
31
|
- lib/codebreaker_kirill/game.rb
|
31
32
|
- lib/codebreaker_kirill/guess_handler.rb
|
33
|
+
- lib/codebreaker_kirill/settings.rb
|
34
|
+
- lib/codebreaker_kirill/stats.rb
|
35
|
+
- lib/codebreaker_kirill/stats.yml
|
32
36
|
- lib/codebreaker_kirill/user.rb
|
37
|
+
- lib/codebreaker_kirill/validations.rb
|
33
38
|
- lib/codebreaker_kirill/version.rb
|
34
39
|
- sig/codebreaker_kirill.rbs
|
35
40
|
homepage:
|