codebreaker_gapdn 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6adf54f1e9ca95342913ab85d6725b53e3327051d08f1d188e0a2bcaeefc7fc3
4
- data.tar.gz: 302012d2c88d308647578a21af945dbb8d847c30ad7f473f83de7ce422ca1bf7
3
+ metadata.gz: 7d941db157786ec9ac283db477b06ab6c9e77a1466ce33d33c40a6430b04e5ab
4
+ data.tar.gz: 60f2244f3d0ea8b2f4bad32422e9e0351851d10f64394251da9acdae4804b675
5
5
  SHA512:
6
- metadata.gz: b08a2c7dedfffa62c694d0a21eeeea8b504ff524ee506bcc3e8ec1b90561c682c64f2267f0e58692237aed8da3775825b12632fcd580972f3c9fe67098f012bd
7
- data.tar.gz: 5f4f7d451c38e76ad034f6b8f9ff95dfd9fca01e6522741ab86ccbf82cd54f85bb0af02e4591c5d74ad80973edc6cfc812507bc006f6b02642602c59bdf24f6e
6
+ metadata.gz: bb9db105b27eda62578d4db3a8a03a5fd0e913399c3b7d1447dafd5b34a9ddd9af9729df953494a4a5e2c298ebb2a5298ee3c935c5b1cf703b3eb494442ca537
7
+ data.tar.gz: 3f9ac66a396a9a5c0c65c4f04df92d7c9393a69cae72b1099678e60e24ba7ec730ac665a82855ecf8095f2baa1fe36603c5349447093fee6f34a68b8a73131ac
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- codebreaker_gapdn (0.1.1)
4
+ codebreaker_gapdn (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -67,4 +67,4 @@ DEPENDENCIES
67
67
  simplecov (~> 0.17.1)
68
68
 
69
69
  BUNDLED WITH
70
- 2.0.2
70
+ 2.1.2
@@ -1,75 +1,69 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Game
4
- GUESS_PRESENCE = '-'
5
- GUESS_PLACE = '+'
3
+ module Codebreaker
4
+ class Game
5
+ GUESS_PRESENCE = '-'
6
+ GUESS_PLACE = '+'
6
7
 
7
- attr_reader :secret_code, :attempts_counter, :hinted_numbers, :current_difficulty, :win
8
+ attr_reader :secret_code, :attempts_counter, :hinted_numbers, :current_difficulty
8
9
 
9
- def initialize(difficulty)
10
- @secret_code = create_secret_code
11
- @guess = []
12
- @attempts_counter = 0
13
- difficulty(difficulty)
14
- end
10
+ def initialize(difficulty)
11
+ @secret_code = create_secret_code
12
+ @attempts_counter = 0
13
+ @current_difficulty = difficulty.level
14
+ @hinted_numbers = @secret_code.sample(@current_difficulty[:hints])
15
+ end
15
16
 
16
- def difficulty(difficulty)
17
- @current_difficulty = difficulty.level
18
- @hinted_numbers = @secret_code.sample(@current_difficulty[:hints])
19
- end
17
+ def check_number_match(guess)
18
+ @guess = guess
19
+ @attempts_counter += 1
20
+ return win_string if @guess == @secret_code
20
21
 
21
- def check_number_match(guess)
22
- @guess = guess
23
- @attempts_counter += 1
24
- if @guess == @secret_code
25
- @win = true
26
- return win_string
22
+ number_match(exact_match)
27
23
  end
28
24
 
29
- number_match(exact_match)
30
- end
31
-
32
- def take_hint
33
- @hinted_numbers.pop
34
- end
25
+ def take_hint
26
+ @hinted_numbers.pop
27
+ end
35
28
 
36
- def win?(string)
37
- string == win_string
38
- end
29
+ def win?(string)
30
+ string == win_string
31
+ end
39
32
 
40
- def lose?
41
- @attempts_counter >= @current_difficulty[:attempts]
42
- end
33
+ def lose?
34
+ @attempts_counter >= @current_difficulty[:attempts]
35
+ end
43
36
 
44
- private
37
+ private
45
38
 
46
- def win_string
47
- GUESS_PLACE * Guess::VALID_GUESS_LENGTH
48
- end
39
+ def win_string
40
+ GUESS_PLACE * Guess::VALID_GUESS_LENGTH
41
+ end
49
42
 
50
- def create_secret_code
51
- Array.new(4) { rand(0..6) }
52
- end
43
+ def create_secret_code
44
+ Array.new(4) { rand(0..6) }
45
+ end
53
46
 
54
- def exact_match
55
- array = @secret_code.dup
56
- array.map.with_index do |_char, index|
57
- next unless array[index] == @guess[index]
47
+ def exact_match
48
+ array = @secret_code.dup
49
+ array.map.with_index do |_char, index|
50
+ next unless array[index] == @guess[index]
58
51
 
59
- @guess[index] = nil
60
- GUESS_PLACE
61
- end.compact
62
- end
52
+ @guess[index] = nil
53
+ GUESS_PLACE
54
+ end.compact
55
+ end
63
56
 
64
- def number_match(pluses)
65
- s_code = @secret_code.map.with_index { |char, index| @guess[index].nil? ? nil : char }.compact
66
- @guess.compact!
67
- s_code.map.with_index do |_char, index|
68
- if s_code.include? @guess[index]
69
- s_code[s_code.index(@guess[index])] = nil
70
- pluses << GUESS_PRESENCE
57
+ def number_match(pluses)
58
+ code = @secret_code.map.with_index { |char, index| char if @guess[index] }.compact
59
+ @guess.compact!
60
+ code.map.with_index do |_char, index|
61
+ if code.include? @guess[index]
62
+ code[code.index(@guess[index])] = nil
63
+ pluses << GUESS_PRESENCE
64
+ end
71
65
  end
66
+ pluses.join
72
67
  end
73
- pluses.join
74
68
  end
75
69
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Codebreaker
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.3'
5
5
  end
@@ -1,52 +1,56 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Statistic
4
- include DataHandler
3
+ module Codebreaker
4
+ class Statistic
5
+ include DataHandler
5
6
 
6
- attr_reader :headers
7
+ attr_reader :headers
7
8
 
8
- def initialize(db = __dir__ + '/storage.yml')
9
- @db = db
10
- @headers = %w[ratign name difficulty attempts_total attempts_used hints_total hints_used]
11
- @items = load || []
12
- end
9
+ def initialize(db = __dir__ + '/storage.yml')
10
+ @db = db
11
+ @headers = %w[ratign name difficulty attempts_total attempts_used hints_total hints_used]
12
+ @items = load || []
13
+ end
13
14
 
14
- def statistic
15
- sort
16
- @items.each_with_index.map do |item, index|
17
- [index.next, item[:name], item[:difficulty], item[:attempts_total],
18
- item[:attempts_used], item[:hints_total], item[:hints_used]]
15
+ def statistic
16
+ sort
17
+ @items.each_with_index.map do |item, index|
18
+ [
19
+ index.next, item[:name], item[:difficulty], item[:attempts_total],
20
+ item[:attempts_used], item[:hints_total], item[:hints_used]
21
+ ]
22
+ end
19
23
  end
20
- end
21
24
 
22
- def add_item(game, username)
23
- @items << { name: username,
24
- difficulty: game.current_difficulty[:name],
25
- difficulty_id: game.current_difficulty[:id],
26
- attempts_total: game.current_difficulty[:attempts],
27
- attempts_used: game.attempts_counter,
28
- hints_total: game.current_difficulty[:hints],
29
- hints_used: hints_used(game) }
30
- save
31
- end
25
+ def add_item(game, username)
26
+ @items << { name: username,
27
+ difficulty: game.current_difficulty[:name],
28
+ difficulty_id: game.current_difficulty[:id],
29
+ attempts_total: game.current_difficulty[:attempts],
30
+ attempts_used: game.attempts_counter,
31
+ hints_total: game.current_difficulty[:hints],
32
+ hints_used: hints_used(game) }
33
+ save
34
+ end
32
35
 
33
- private
36
+ private
34
37
 
35
- def hints_used(game)
36
- game.current_difficulty[:hints] - game.hinted_numbers.size
37
- end
38
+ def hints_used(game)
39
+ game.current_difficulty[:hints] - game.hinted_numbers.size
40
+ end
38
41
 
39
- def load
40
- DataHandler.load(@db)
41
- end
42
+ def load
43
+ DataHandler.load(@db)
44
+ end
42
45
 
43
- def save
44
- DataHandler.save(@db, @items)
45
- end
46
+ def save
47
+ DataHandler.save(@db, @items)
48
+ end
46
49
 
47
- def sort
48
- @items.sort_by! do |item|
49
- [-item[:difficulty_id], item[:attempts_used], item[:hints_used]]
50
+ def sort
51
+ @items.sort_by! do |item|
52
+ [-item[:difficulty_id], item[:attempts_used], item[:hints_used]]
53
+ end
50
54
  end
51
55
  end
52
56
  end
@@ -1,16 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class BaseClass
4
- include Validator
3
+ module Codebreaker
4
+ class BaseClass
5
+ include Validator
5
6
 
6
- def valid?
7
- validate
8
- @errors.empty?
9
- end
7
+ def valid?
8
+ validate
9
+ @errors.empty?
10
+ end
10
11
 
11
- private
12
+ private
12
13
 
13
- def validate
14
- raise NotImplementedError
14
+ def validate
15
+ raise NotImplementedError
16
+ end
15
17
  end
16
18
  end
@@ -1,20 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Difficulty < BaseClass
4
- LEVELS = {
5
- easy: { attempts: 15, hints: 3, name: 'easy', id: 1 },
6
- medium: { attempts: 10, hints: 2, name: 'medium', id: 2 },
7
- hell: { attempts: 5, hints: 1, name: 'hell', id: 3 }
8
- }.freeze
3
+ module Codebreaker
4
+ class Difficulty < BaseClass
5
+ LEVELS = {
6
+ easy: { attempts: 15, hints: 3, name: 'easy', id: 1 },
7
+ medium: { attempts: 10, hints: 2, name: 'medium', id: 2 },
8
+ hell: { attempts: 5, hints: 1, name: 'hell', id: 3 }
9
+ }.freeze
9
10
 
10
- attr_reader :errors, :level
11
+ attr_reader :errors, :level
11
12
 
12
- def initialize(level = :easy)
13
- @level = LEVELS[level.to_sym]
14
- @errors = []
15
- end
13
+ def initialize(level = :easy)
14
+ @level = LEVELS[level.to_sym]
15
+ @errors = []
16
+ end
16
17
 
17
- def validate
18
- @errors << 'unexpected_comand' unless LEVELS.values.include? @level
18
+ def validate
19
+ @errors << 'unexpected_comand' unless LEVELS.values.include? @level
20
+ end
19
21
  end
20
22
  end
@@ -1,19 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Guess < BaseClass
4
- VALID_GUESS_LENGTH = 4
5
- VALID_GUESS_RANGE = ('0'..'6').freeze
3
+ module Codebreaker
4
+ class Guess < BaseClass
5
+ VALID_GUESS_LENGTH = 4
6
+ VALID_GUESS_RANGE = ('0'..'6').freeze
6
7
 
7
- attr_accessor :number
8
- attr_reader :errors
8
+ attr_accessor :number
9
+ attr_reader :errors
9
10
 
10
- def initialize(guess)
11
- @number = guess
12
- @errors = []
13
- end
11
+ def initialize(guess)
12
+ @number = guess
13
+ @errors = []
14
+ end
14
15
 
15
- def validate
16
- @errors << 'error_number_length' unless check_length?(@number, VALID_GUESS_LENGTH)
17
- @errors << 'error_number_digit' unless check_number_in_range?(@number, VALID_GUESS_RANGE)
16
+ def validate
17
+ @errors << 'error_number_length' unless check_length?(@number, VALID_GUESS_LENGTH)
18
+ @errors << 'error_number_digit' unless check_number_in_range?(@number, VALID_GUESS_RANGE)
19
+ end
18
20
  end
19
21
  end
@@ -1,19 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class User < BaseClass
4
- VALID_NAME_RANGE = (3..20).freeze
5
- VALID_SYMBOLS_RANGE = ('a'..'z').freeze
3
+ module Codebreaker
4
+ class User < BaseClass
5
+ VALID_NAME_RANGE = (3..20).freeze
6
+ VALID_SYMBOLS_RANGE = ('a'..'z').freeze
6
7
 
7
- attr_accessor :name
8
- attr_reader :errors
8
+ attr_accessor :name
9
+ attr_reader :errors
9
10
 
10
- def initialize(name = 'Codebreaker')
11
- @name = name
12
- @errors = []
13
- end
11
+ def initialize(name = 'Codebreaker')
12
+ @name = name
13
+ @errors = []
14
+ end
14
15
 
15
- def validate
16
- @errors << 'error_name_length' unless check_length_in_range?(@name, VALID_NAME_RANGE)
17
- @errors << 'error_name_chars' unless check_symbols_in_range?(@name, VALID_SYMBOLS_RANGE)
16
+ def validate
17
+ @errors << 'error_name_length' unless check_length_in_range?(@name, VALID_NAME_RANGE)
18
+ @errors << 'error_name_chars' unless check_symbols_in_range?(@name, VALID_SYMBOLS_RANGE)
19
+ end
18
20
  end
19
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreaker_gapdn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - gapdn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-19 00:00:00.000000000 Z
11
+ date: 2020-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler