codebreakergem 0.1.6 → 0.1.8

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: 4037f592f943f162e0d39abbfabe74e5ba3e7a79019e4dfbd875601cb6310293
4
- data.tar.gz: 671252a913e1d5a691dd0ecda6c374f611a6d496a60c19f611b7e3f00e7a544c
3
+ metadata.gz: 24dbfc89209109274edcddc6ce15602d3ec4ac625efcf6140d4d858c27de7ac2
4
+ data.tar.gz: 88acb590b21360401e212b2ba04a7b2c4eddb54b94988a8da5057458107ccec7
5
5
  SHA512:
6
- metadata.gz: f690c6ae82685c759cd505675937d69738e3caa7fe4367fb748488dd906d3ce131bb8de4af73629c2d7fcf0e6b47110a21e386a9aad91f512f9d46659ef78090
7
- data.tar.gz: af5513cf119e5acf09df2b4f7a6601e84fae9a8d66111dc74f336a39a5abe4dfc8323ca0d23c255cf2621798cc3c56e6ad33a1adf1fd6f5c7dfb4682c9f457de
6
+ metadata.gz: cdb2e709dd642d8b2499cd1974e238d341ba64ae94caa17f3e9fc46857eb9337b40281c111ce381be5a0d8e9ae36b041717354b40860d8655a2f1d1ff31cd07d
7
+ data.tar.gz: a3fcaf3a88816769aa7903e395910c5af6225d6cfbce7f758610896584775bc615211470714881732f18ad6c10b24529b29beaf2cb496ec4a20cfb40d927f458
data/.rubocop.yml CHANGED
@@ -10,4 +10,3 @@ Metrics/LineLength:
10
10
  AllCops:
11
11
  Exclude:
12
12
  - 'main.rb'
13
- - 'spec/spec_helper.rb'
@@ -2,25 +2,19 @@
2
2
 
3
3
  module Codebreakergem
4
4
  class FileWorker
5
+ PERMITTED_CLASSES = [Statistic, Difficulty]
6
+
5
7
  class << self
6
8
  def read_from_file(filename)
7
- permitted_class = [Statistic, Difficulty]
8
- unless File.file?(Game::FILE)
9
- puts 'File doesn\'t exist'
10
- return
11
- end
12
- YAML.safe_load(File.read(filename), permitted_class, [], true)
9
+ return nil unless File.file?(Game::FILE)
10
+
11
+ YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true)
13
12
  end
14
13
 
15
14
  def add_to_file(filename, data)
16
- permitted_class = [Statistic, Difficulty]
17
- data_in_file = YAML.safe_load(File.read(filename), permitted_class, [], true)
18
- if data_in_file.is_a? Array
19
- data_in_file << data
20
- File.open(filename, 'w') { |file| file.write(data_in_file.to_yaml) }
21
- else
22
- File.open(filename, 'w') { |file| file.write([data_in_file, data].to_yaml) }
23
- end
15
+ data_in_file = Array(YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true))
16
+ data_in_file << data
17
+ write_to_file(filename, data_in_file)
24
18
  end
25
19
 
26
20
  def write_to_file(filename, data)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Codebreakergem
4
4
  class Statistic
5
- attr_accessor :name, :difficulty, :attempts_total,
5
+ attr_reader :name, :difficulty, :attempts_total,
6
6
  :attempts_used, :hints_total, :hints_used
7
7
 
8
8
  def initialize(name:, difficulty:, attempts_total:, hints_total:)
@@ -3,129 +3,14 @@
3
3
  require 'yaml'
4
4
  require 'i18n'
5
5
  require 'pry'
6
- require 'modules/check_module.rb'
7
- require 'classes/difficulty.rb'
8
- require 'classes/user.rb'
9
- require 'classes/statistic.rb'
10
- require 'classes/file_worker.rb'
11
- require 'config.rb'
6
+ require './lib/modules/check_module.rb'
7
+ require './lib/classes/difficulty.rb'
8
+ require './lib/classes/user.rb'
9
+ require './lib/classes/game.rb'
10
+ require './lib/classes/statistic.rb'
11
+ require './lib/classes/file_worker.rb'
12
+ require './lib/config.rb'
12
13
  require 'codebreakergem/version.rb'
13
14
 
14
15
  module Codebreakergem
15
- class Game
16
- include Checker
17
-
18
- DIGIT_NUMBER = 4
19
- DIFFICULTIES = [Difficulty.new(name: 'Easy', attempts: 15, hints: 2, order: 3),
20
- Difficulty.new(name: 'Medium', attempts: 10, hints: 1, order: 2),
21
- Difficulty.new(name: 'Hell', attempts: 5, hints: 1, order: 1)].freeze
22
- COMMANDS = %w[hint exit].freeze
23
- GUESS_PLACE = '+'
24
- GUESS_PRESENCE = '-'
25
- FILE = File.expand_path(__dir__ + '/data/data.yml')
26
-
27
- attr_reader :secret_code, :user
28
- attr_accessor :difficulty, :statistics
29
-
30
- def initialize(user)
31
- @user = user
32
- @secret_code = generate_code
33
- end
34
-
35
- def show_hint
36
- if difficulty.hints.positive?
37
- difficulty.hints -= 1
38
- secret_code[difficulty.hints]
39
- else
40
- I18n.t(:no_hints)
41
- end
42
- end
43
-
44
- def try_guess(guess)
45
- if guess == secret_code
46
- save
47
- difficulty.attempts = 0
48
- return I18n.t(:victory_message)
49
- end
50
-
51
- difficulty.attempts -= 1
52
- return I18n.t(:out_of_attempts) unless difficulty.attempts.positive?
53
-
54
- make_guess(guess)
55
- end
56
-
57
- def validate_guess(guess)
58
- return false if guess.empty?
59
-
60
- guess.match?(/\D/) ? COMMANDS.include?(guess) : true
61
- end
62
-
63
- def save
64
- source_difficulty = DIFFICULTIES.find { |item| item.name == difficulty.name }
65
- statistic = Statistic.new(name: user.name, difficulty: difficulty, attempts_total: source_difficulty.attempts,
66
- hints_total: source_difficulty.hints)
67
- File.file?(FILE) ? FileWorker.add_to_file(FILE, statistic) : FileWorker.write_to_file(FILE, statistic)
68
- end
69
-
70
- class << self
71
- def rules
72
- I18n.t(:rules)
73
- end
74
-
75
- def stats
76
- index = 0
77
- result = I18n.t(:stats_header)
78
- data = Array(FileWorker.read_from_file(FILE))
79
- return result unless data
80
-
81
- sorted_data = data.sort_by { |item| [item.difficulty.order, item.attempts_used, item.hints_used] }
82
- sorted_data.each { |item| result += stats_constructor(index += 1, item) }
83
- result
84
- end
85
-
86
- private
87
-
88
- def stats_constructor(index, item)
89
- "#{index}\t #{item.name}\t #{item.difficulty.name}\t #{item.attempts_total}\t " \
90
- "#{item.attempts_used}\t #{item.hints_total}\t #{item.hints_used}\n"
91
- end
92
- end
93
-
94
- private
95
-
96
- def generate_code
97
- DIGIT_NUMBER.times.map { rand(1..6) }.join
98
- end
99
-
100
- def find_place(guess, code)
101
- result = ''
102
- (0...guess.length).each do |index|
103
- next unless guess[index] == code[index]
104
-
105
- result += GUESS_PLACE
106
- code[index] = ' '
107
- guess[index] = '_'
108
- end
109
- result
110
- end
111
-
112
- def find_presence(guess, code)
113
- result = ''
114
- (0...guess.length).each do |index|
115
- position = code.index(guess[index])
116
- if position
117
- result += GUESS_PRESENCE
118
- code[position] = ' '
119
- end
120
- end
121
- result
122
- end
123
-
124
- def make_guess(guess)
125
- result = ''
126
- code_copy = secret_code.dup
127
- result += find_place(guess, code_copy)
128
- result + find_presence(guess, code_copy)
129
- end
130
- end
131
16
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Codebreakergem
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.8'
5
5
  end
data/lib/locales/en.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  en:
2
2
  length_error: 'Length of the string is not in allowed range'
3
3
  no_hints: 'You have no more hints'
4
+ no_file: 'File does not exist'
4
5
  stats_header: "Rating Name Difficulty Attempts Total Attempts Used Hints Total Hints Used\n"
5
6
  victory_message: 'Congratulations!!! YOU WON!!!'
6
7
  out_of_attempts: 'Unfortunately you have no more attempts'
@@ -37,3 +38,14 @@ en:
37
38
  | * Codebreaker also has some number of hints(depends on chosen difficulty). If a user |\n
38
39
  | takes a hint - he receives back a separate digit of the secret code. |\n
39
40
  |____________________________________________________________________________________________|"
41
+ hello_message: 'Hello player! Welcome to codebreaker'
42
+ menu: "Chose action:\n
43
+ 1) Start\n
44
+ 2) Rules\n
45
+ 3) Stats\n
46
+ 4) Exit\n"
47
+ victory_message: 'Congratulations!!! YOU WON!!!'
48
+ goodbye_message: "Thank you for playing\nSee you next time"
49
+ input_name: "Input player's name:"
50
+ difficulty: 'Chose difficulty'
51
+ invalid_command: 'You have passed unexpected command. Please choose one from listed commands'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreakergem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sasha
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-27 00:00:00.000000000 Z
11
+ date: 2019-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler