codebreakergem 0.1.8 → 0.1.9
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/codebreakergem-0.1.6.gem +0 -0
- data/codebreakergem-0.1.7.gem +0 -0
- data/codebreakergem-0.1.8.gem +0 -0
- data/lib/classes/game.rb +118 -0
- data/lib/codebreakergem/version.rb +1 -1
- data/lib/data/data.yml +23 -0
- data/main.rb +70 -0
- data/test.rb +7 -0
- metadata +8 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd9872a708d03902c8443257387f82370bdd1d1713738360267ab5e06fe6a560
|
4
|
+
data.tar.gz: ad61ebe48ceaece6c526d408033b0abe1d644dd0b5cdfd0d92816122a267b137
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbe5b11c708a0bbc958c94ea2f2b6f7db606a39f38100b357a5d18905a71b0301bfa1440b32316181e2ab7743bf31ae237bc2dee832c38b954d9e69a96304c65
|
7
|
+
data.tar.gz: 22b9563dfa2f796b37ad766b33b5762b852cd9fbd3128b21bb867d2ed37d57ad460fb14ff7c87d19bf7ac92008145d3dc9aedcadedcc9ddb0f6ed09004786971
|
Binary file
|
Binary file
|
Binary file
|
data/lib/classes/game.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreakergem
|
4
|
+
class Game
|
5
|
+
include Checker
|
6
|
+
|
7
|
+
DIGIT_NUMBER = 4
|
8
|
+
DIFFICULTIES = [Difficulty.new(name: 'Easy', attempts: 15, hints: 2, order: 3),
|
9
|
+
Difficulty.new(name: 'Medium', attempts: 10, hints: 1, order: 2),
|
10
|
+
Difficulty.new(name: 'Hell', attempts: 5, hints: 1, order: 1)].freeze
|
11
|
+
COMMANDS = %w[hint exit].freeze
|
12
|
+
GUESS_PLACE = '+'
|
13
|
+
GUESS_PRESENCE = '-'
|
14
|
+
FILE = File.expand_path(__dir__ + '/../data/data.yml')
|
15
|
+
|
16
|
+
attr_reader :secret_code, :user
|
17
|
+
attr_accessor :difficulty, :statistics
|
18
|
+
|
19
|
+
def initialize(user)
|
20
|
+
@user = user
|
21
|
+
@secret_code = generate_code
|
22
|
+
end
|
23
|
+
|
24
|
+
def show_hint
|
25
|
+
return I18n.t(:no_hints) unless difficulty.hints.positive?
|
26
|
+
|
27
|
+
difficulty.hints -= 1
|
28
|
+
secret_code[difficulty.hints]
|
29
|
+
end
|
30
|
+
|
31
|
+
def try_guess(guess)
|
32
|
+
if guess == secret_code
|
33
|
+
save
|
34
|
+
difficulty.attempts = 0
|
35
|
+
return I18n.t(:victory_message)
|
36
|
+
end
|
37
|
+
|
38
|
+
difficulty.attempts -= 1
|
39
|
+
return I18n.t(:out_of_attempts) unless difficulty.attempts.positive?
|
40
|
+
|
41
|
+
make_guess(guess)
|
42
|
+
end
|
43
|
+
|
44
|
+
def validate_guess(guess)
|
45
|
+
return false if guess.empty?
|
46
|
+
|
47
|
+
guess.match?(/\D/) ? COMMANDS.include?(guess) : true
|
48
|
+
end
|
49
|
+
|
50
|
+
def save
|
51
|
+
source_difficulty = DIFFICULTIES.find { |item| item.name == difficulty.name }
|
52
|
+
statistic = Statistic.new(name: user.name, difficulty: difficulty, attempts_total: source_difficulty.attempts,
|
53
|
+
hints_total: source_difficulty.hints)
|
54
|
+
File.file?(FILE) ? FileWorker.add_to_file(FILE, statistic) : FileWorker.write_to_file(FILE, statistic)
|
55
|
+
end
|
56
|
+
|
57
|
+
class << self
|
58
|
+
def rules
|
59
|
+
I18n.t(:rules)
|
60
|
+
end
|
61
|
+
|
62
|
+
def stats
|
63
|
+
index = 0
|
64
|
+
result = I18n.t(:stats_header)
|
65
|
+
data = Array(FileWorker.read_from_file(FILE))
|
66
|
+
return I18n.t(:no_file) if data.empty?
|
67
|
+
|
68
|
+
sorted_data = data.sort_by { |item| [item.difficulty.order, item.attempts_used, item.hints_used] }
|
69
|
+
sorted_data.each { |item| result += stats_constructor(index += 1, item) }
|
70
|
+
result
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def stats_constructor(index, item)
|
76
|
+
"#{index}\t #{item.name}\t #{item.difficulty.name}\t #{item.attempts_total}\t " \
|
77
|
+
"#{item.attempts_used}\t #{item.hints_total}\t #{item.hints_used}\n"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def generate_code
|
84
|
+
DIGIT_NUMBER.times.map { rand(1..6) }.join
|
85
|
+
end
|
86
|
+
|
87
|
+
def find_place(guess, code)
|
88
|
+
result = ''
|
89
|
+
(0...guess.length).each do |index|
|
90
|
+
next unless guess[index] == code[index]
|
91
|
+
|
92
|
+
result += GUESS_PLACE
|
93
|
+
code[index] = ' '
|
94
|
+
guess[index] = '_'
|
95
|
+
end
|
96
|
+
result
|
97
|
+
end
|
98
|
+
|
99
|
+
def find_presence(guess, code)
|
100
|
+
result = ''
|
101
|
+
(0...guess.length).each do |index|
|
102
|
+
position = code.index(guess[index])
|
103
|
+
if position
|
104
|
+
result += GUESS_PRESENCE
|
105
|
+
code[position] = ' '
|
106
|
+
end
|
107
|
+
end
|
108
|
+
result
|
109
|
+
end
|
110
|
+
|
111
|
+
def make_guess(guess)
|
112
|
+
result = ''
|
113
|
+
code_copy = secret_code.dup
|
114
|
+
result += find_place(guess, code_copy)
|
115
|
+
result + find_presence(guess, code_copy)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
data/lib/data/data.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
---
|
2
|
+
- !ruby/object:Codebreakergem::Statistic
|
3
|
+
name: Alex
|
4
|
+
difficulty: !ruby/object:Codebreakergem::Difficulty
|
5
|
+
name: Easy
|
6
|
+
attempts: 8
|
7
|
+
hints: 1
|
8
|
+
order: 3
|
9
|
+
attempts_total: 15
|
10
|
+
attempts_used: 7
|
11
|
+
hints_total: 2
|
12
|
+
hints_used: 1
|
13
|
+
- !ruby/object:Codebreakergem::Statistic
|
14
|
+
name: Alex
|
15
|
+
difficulty: !ruby/object:Codebreakergem::Difficulty
|
16
|
+
name: Medium
|
17
|
+
attempts: 5
|
18
|
+
hints: 1
|
19
|
+
order: 2
|
20
|
+
attempts_total: 10
|
21
|
+
attempts_used: 5
|
22
|
+
hints_total: 1
|
23
|
+
hints_used: 0
|
data/main.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require './lib/codebreakergem.rb'
|
4
|
+
include Codebreakergem
|
5
|
+
|
6
|
+
def hello_message
|
7
|
+
puts 'Hello'
|
8
|
+
end
|
9
|
+
|
10
|
+
def menu
|
11
|
+
puts "Chose action:\n 1) Start\n 2) Rules\n 3) Stats\n 4) Exit\n"
|
12
|
+
end
|
13
|
+
|
14
|
+
def goodbye_message
|
15
|
+
puts "Thank you for playing\nSee you next time"
|
16
|
+
end
|
17
|
+
|
18
|
+
def play_game
|
19
|
+
user = nil
|
20
|
+
user_input = ''
|
21
|
+
puts "Input player's name:"
|
22
|
+
|
23
|
+
loop do
|
24
|
+
user = User.new(gets.chomp)
|
25
|
+
break if user.validate?
|
26
|
+
end
|
27
|
+
game = Game.new(user)
|
28
|
+
|
29
|
+
difficulties = Game::DIFFICULTIES.map(&:name)
|
30
|
+
|
31
|
+
loop do
|
32
|
+
puts "Chose difficulty"
|
33
|
+
difficulties.each {|key| puts " -#{key}"}
|
34
|
+
user_input = gets.chomp
|
35
|
+
break if difficulties.include? user_input
|
36
|
+
puts "Input one of proposed options"
|
37
|
+
end
|
38
|
+
game.difficulty = Game::DIFFICULTIES.find{|item| item.name == user_input}.dup
|
39
|
+
|
40
|
+
|
41
|
+
while game.difficulty.attempts.positive? do
|
42
|
+
puts 'Input your guess:'
|
43
|
+
user_input = gets.chomp
|
44
|
+
next unless game.validate_guess(user_input)
|
45
|
+
if user_input == "hint"
|
46
|
+
puts game.show_hint
|
47
|
+
elsif user_input == "exit"
|
48
|
+
break
|
49
|
+
else
|
50
|
+
puts game.try_guess(user_input)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def game_process
|
56
|
+
hello_message
|
57
|
+
loop do
|
58
|
+
menu
|
59
|
+
case gets.chomp
|
60
|
+
when "Start" then play_game
|
61
|
+
when "Rules" then puts Game.rules
|
62
|
+
when "Stats" then puts Game.stats
|
63
|
+
when "Exit" then break
|
64
|
+
else puts "You have passed unexpected command. Please choose one from listed commands"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
goodbye_message
|
68
|
+
end
|
69
|
+
|
70
|
+
game_process
|
data/test.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sasha
|
@@ -73,18 +73,25 @@ files:
|
|
73
73
|
- Rakefile
|
74
74
|
- bin/console
|
75
75
|
- bin/setup
|
76
|
+
- codebreakergem-0.1.6.gem
|
77
|
+
- codebreakergem-0.1.7.gem
|
78
|
+
- codebreakergem-0.1.8.gem
|
76
79
|
- codebreakergem.gemspec
|
77
80
|
- lib/classes/difficulty.rb
|
78
81
|
- lib/classes/file_worker.rb
|
82
|
+
- lib/classes/game.rb
|
79
83
|
- lib/classes/statistic.rb
|
80
84
|
- lib/classes/user.rb
|
81
85
|
- lib/codebreakergem.rb
|
82
86
|
- lib/codebreakergem/version.rb
|
83
87
|
- lib/config.rb
|
84
88
|
- lib/data/.gitkeep
|
89
|
+
- lib/data/data.yml
|
85
90
|
- lib/locales/en.yml
|
86
91
|
- lib/locales/ru.yml
|
87
92
|
- lib/modules/check_module.rb
|
93
|
+
- main.rb
|
94
|
+
- test.rb
|
88
95
|
homepage: https://github.com/SashaZhuravskiy/CodebreakerGemRepo/
|
89
96
|
licenses:
|
90
97
|
- MIT
|