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 +4 -4
- data/.rubocop.yml +0 -1
- data/lib/classes/file_worker.rb +8 -14
- data/lib/classes/statistic.rb +1 -1
- data/lib/codebreakergem.rb +7 -122
- data/lib/codebreakergem/version.rb +1 -1
- data/lib/locales/en.yml +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24dbfc89209109274edcddc6ce15602d3ec4ac625efcf6140d4d858c27de7ac2
|
4
|
+
data.tar.gz: 88acb590b21360401e212b2ba04a7b2c4eddb54b94988a8da5057458107ccec7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdb2e709dd642d8b2499cd1974e238d341ba64ae94caa17f3e9fc46857eb9337b40281c111ce381be5a0d8e9ae36b041717354b40860d8655a2f1d1ff31cd07d
|
7
|
+
data.tar.gz: a3fcaf3a88816769aa7903e395910c5af6225d6cfbce7f758610896584775bc615211470714881732f18ad6c10b24529b29beaf2cb496ec4a20cfb40d927f458
|
data/.rubocop.yml
CHANGED
data/lib/classes/file_worker.rb
CHANGED
@@ -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
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
17
|
-
data_in_file
|
18
|
-
|
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)
|
data/lib/classes/statistic.rb
CHANGED
data/lib/codebreakergem.rb
CHANGED
@@ -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/
|
10
|
-
require 'classes/
|
11
|
-
require '
|
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
|
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.
|
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-
|
11
|
+
date: 2019-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|