codebreaker_rg 0.1.0

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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.rspec_status +168 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +85 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +43 -0
  9. data/Rakefile +6 -0
  10. data/autoload.rb +15 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/codebreaker_rg.gemspec +46 -0
  14. data/coverage/.last_run.json +5 -0
  15. data/coverage/.resultset.json +617 -0
  16. data/coverage/.resultset.json.lock +0 -0
  17. data/coverage/assets/0.10.2/application.css +799 -0
  18. data/coverage/assets/0.10.2/application.js +1707 -0
  19. data/coverage/assets/0.10.2/colorbox/border.png +0 -0
  20. data/coverage/assets/0.10.2/colorbox/controls.png +0 -0
  21. data/coverage/assets/0.10.2/colorbox/loading.gif +0 -0
  22. data/coverage/assets/0.10.2/colorbox/loading_background.png +0 -0
  23. data/coverage/assets/0.10.2/favicon_green.png +0 -0
  24. data/coverage/assets/0.10.2/favicon_red.png +0 -0
  25. data/coverage/assets/0.10.2/favicon_yellow.png +0 -0
  26. data/coverage/assets/0.10.2/loading.gif +0 -0
  27. data/coverage/assets/0.10.2/magnify.png +0 -0
  28. data/coverage/assets/0.10.2/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  29. data/coverage/assets/0.10.2/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  30. data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  31. data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  32. data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  33. data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  34. data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  35. data/coverage/assets/0.10.2/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  36. data/coverage/assets/0.10.2/smoothness/images/ui-icons_222222_256x240.png +0 -0
  37. data/coverage/assets/0.10.2/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  38. data/coverage/assets/0.10.2/smoothness/images/ui-icons_454545_256x240.png +0 -0
  39. data/coverage/assets/0.10.2/smoothness/images/ui-icons_888888_256x240.png +0 -0
  40. data/coverage/assets/0.10.2/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  41. data/coverage/index.html +3868 -0
  42. data/database/.gitignore +4 -0
  43. data/lib/app/entities/data_storage.rb +27 -0
  44. data/lib/app/entities/game.rb +70 -0
  45. data/lib/app/entities/menu.rb +158 -0
  46. data/lib/app/entities/processor.rb +34 -0
  47. data/lib/app/entities/renderer.rb +63 -0
  48. data/lib/app/entities/statistics.rb +20 -0
  49. data/lib/app/i18n_config.rb +4 -0
  50. data/lib/app/locales/en.yml +39 -0
  51. data/lib/app/modules/validator.rb +17 -0
  52. data/lib/codebreaker_rg/version.rb +3 -0
  53. data/lib/codebreaker_rg.rb +7 -0
  54. data/pkg/codebreaker_rg-0.1.0.gem +0 -0
  55. metadata +238 -0
@@ -0,0 +1,4 @@
1
+ # Ignore everything in this directory
2
+ *
3
+ # Except this file
4
+ !.gitignore
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DataStorage
4
+ FILE_NAME = 'database/data.yml'
5
+
6
+ def create
7
+ File.new(FILE_NAME, 'w')
8
+ File.write(FILE_NAME, [].to_yaml)
9
+ end
10
+
11
+ def load
12
+ YAML.load(File.open(FILE_NAME), [Menu]) if storage_exist?
13
+ end
14
+
15
+ def save(object)
16
+ File.open(FILE_NAME, 'w') { |file| file.write(YAML.dump(object)) }
17
+ end
18
+
19
+ def storage_exist?
20
+ File.exist?(FILE_NAME)
21
+ end
22
+
23
+ def save_game_result(object)
24
+ create unless storage_exist?
25
+ save(load.push(object))
26
+ end
27
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Game
4
+ DIGITS_COUNT = 4
5
+ DIFFICULTIES = {
6
+ easy: {
7
+ attempts: 15,
8
+ hints: 2
9
+ },
10
+ medium: {
11
+ attempts: 10,
12
+ hints: 1
13
+ },
14
+ hell: {
15
+ attempts: 5,
16
+ hints: 1
17
+ }
18
+ }.freeze
19
+ RANGE = (1..6).freeze
20
+
21
+ attr_reader :attempts, :hints, :code
22
+
23
+ def initialize
24
+ @process = Processor.new
25
+ end
26
+
27
+ def generate(difficulty)
28
+ @difficulty = difficulty
29
+ @code = generate_secret_code
30
+ @hints = @code.sample(difficulty[:hints])
31
+ @attempts = difficulty[:attempts]
32
+ end
33
+
34
+ def start_process(command)
35
+ @process.secret_code_proc(code.join, command)
36
+ end
37
+
38
+ def win?(guess)
39
+ code.join == guess
40
+ end
41
+
42
+ def decrease_attempts!
43
+ @attempts -= 1
44
+ end
45
+
46
+ def to_h(name)
47
+ {
48
+ name: name,
49
+ difficulty: DIFFICULTIES.key(@difficulty),
50
+ all_attempts: @difficulty[:attempts],
51
+ all_hints: @difficulty[:hints],
52
+ attempts_used: @difficulty[:attempts] - @attempts,
53
+ hints_used: @difficulty[:hints] - @hints.length
54
+ }
55
+ end
56
+
57
+ def hints_spent?
58
+ hints.empty?
59
+ end
60
+
61
+ def take_a_hint!
62
+ hints.pop
63
+ end
64
+
65
+ private
66
+
67
+ def generate_secret_code
68
+ Array.new(DIGITS_COUNT) { rand(RANGE) }
69
+ end
70
+ end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Menu
4
+ include Validator
5
+ attr_reader :storage, :renderer, :game, :guess
6
+
7
+ COMMANDS = {
8
+ start: 'start',
9
+ exit: 'exit',
10
+ rules: 'rules',
11
+ stats: 'stats'
12
+ }.freeze
13
+ CHOOSE_COMMANDS = {
14
+ yes: 'yes'
15
+ }.freeze
16
+ HINT_COMMAND = 'hint'
17
+ MIN_SIZE_VALUE = 3
18
+ MAX_SIZE_VALUE = 20
19
+
20
+ def initialize
21
+ @storage = DataStorage.new
22
+ @renderer = Renderer.new
23
+ @game = Game.new
24
+ @statistics = Statistics.new
25
+ end
26
+
27
+ def game_menu
28
+ renderer.start_message
29
+ choice_menu_process(ask(:choice_options, commands: COMMANDS.keys.join(' | ')))
30
+ end
31
+
32
+ private
33
+
34
+ def rules
35
+ renderer.rules
36
+ game_menu
37
+ end
38
+
39
+ def start
40
+ @name = registrate_user
41
+ level_choice
42
+ game_process
43
+ end
44
+
45
+ def stats
46
+ scores = storage.load
47
+ render_stats(@statistics.stats(scores)) if scores
48
+ game_menu
49
+ end
50
+
51
+ def ask(phrase_key = nil, options = {})
52
+ renderer.message(phrase_key, options) if phrase_key
53
+ gets.chomp
54
+ end
55
+
56
+ def save_result
57
+ storage.save_game_result(game.to_h(@name)) if ask(:save_results_message) == CHOOSE_COMMANDS[:yes]
58
+ end
59
+
60
+ def registrate_user
61
+ loop do
62
+ name = ask(:registration)
63
+
64
+ return name if name_valid?(name)
65
+
66
+ renderer.registration_name_length_error
67
+ end
68
+ end
69
+
70
+ def name_valid?(name)
71
+ !check_emptyness(name) && check_length(name, MIN_SIZE_VALUE, MAX_SIZE_VALUE)
72
+ end
73
+
74
+ def level_choice
75
+ loop do
76
+ level = ask(:hard_level, levels: Game::DIFFICULTIES.keys.join(' | '))
77
+
78
+ return generate_game(Game::DIFFICULTIES[level.to_sym]) if Game::DIFFICULTIES[level.to_sym]
79
+ return game_menu if level == COMMANDS[:exit]
80
+
81
+ renderer.command_error
82
+ end
83
+ end
84
+
85
+ def generate_game(difficulty)
86
+ game.generate(difficulty)
87
+ renderer.message(:difficulty,
88
+ hints: difficulty[:hints],
89
+ attempts: difficulty[:attempts])
90
+ end
91
+
92
+ def game_process
93
+ while game.attempts.positive?
94
+ @guess = ask
95
+ return handle_win if game.win?(guess)
96
+
97
+ choice_code_process
98
+ end
99
+ handle_lose
100
+ end
101
+
102
+ def choice_code_process
103
+ case guess
104
+ when HINT_COMMAND then hint_process
105
+ when COMMANDS[:exit] then game_menu
106
+ else handle_command
107
+ end
108
+ end
109
+
110
+ def handle_command
111
+ return renderer.command_error unless check_command_range(guess)
112
+
113
+ p game.start_process(guess)
114
+ renderer.round_message
115
+ game.decrease_attempts!
116
+ end
117
+
118
+ def handle_win
119
+ renderer.win_game_message
120
+ save_result
121
+ game_menu
122
+ end
123
+
124
+ def handle_lose
125
+ renderer.lost_game_message(game.code)
126
+ game_menu
127
+ end
128
+
129
+ def hint_process
130
+ return renderer.no_hints_message? if game.hints_spent?
131
+
132
+ renderer.print_hint_number(game.take_a_hint!)
133
+ end
134
+
135
+ def exit_from_game
136
+ renderer.goodbye_message
137
+ exit
138
+ end
139
+
140
+ def choice_menu_process(command_name)
141
+ case command_name
142
+ when COMMANDS[:start] then start
143
+ when COMMANDS[:exit] then exit_from_game
144
+ when COMMANDS[:rules] then rules
145
+ when COMMANDS[:stats] then stats
146
+ else
147
+ renderer.command_error
148
+ game_menu
149
+ end
150
+ end
151
+
152
+ def render_stats(list)
153
+ list.each_with_index do |key, index|
154
+ puts "#{index + 1}: "
155
+ key.each { |param, value| puts "#{param}:#{value}" }
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Processor
4
+ MATCHED_DIGIT_CHAR = '+'
5
+ UNMATCHED_DIGIT_CHAR = '-'
6
+
7
+ attr_reader :guess, :code, :result
8
+
9
+ def secret_code_proc(code, guess)
10
+ @code = code.split('')
11
+ @guess = guess.split('')
12
+ handle_matched_digits.join + handle_matched_digits_with_wrong_position.join
13
+ end
14
+
15
+ private
16
+
17
+ def handle_matched_digits
18
+ code.map.with_index do |_, index|
19
+ next unless code[index] == guess[index]
20
+
21
+ @guess[index], @code[index] = nil
22
+ MATCHED_DIGIT_CHAR
23
+ end
24
+ end
25
+
26
+ def handle_matched_digits_with_wrong_position
27
+ guess.compact.map do |number|
28
+ next unless @code.include?(number)
29
+
30
+ @code.delete_at(code.index(number))
31
+ UNMATCHED_DIGIT_CHAR
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Renderer
4
+ def message(msg_name, hashee = {})
5
+ puts I18n.t(msg_name, hashee)
6
+ end
7
+
8
+ def start_message
9
+ message(:start_message)
10
+ end
11
+
12
+ def rules
13
+ message(:rules)
14
+ end
15
+
16
+ def goodbye_message
17
+ message(:goodbye_message)
18
+ end
19
+
20
+ def save_results_message
21
+ message(:save_results_message)
22
+ end
23
+
24
+ def win_game_message
25
+ message(:win_game_message)
26
+ end
27
+
28
+ def round_message
29
+ message(:round_message)
30
+ end
31
+
32
+ def lost_game_message(code)
33
+ message(:lost_game_message, code: code)
34
+ end
35
+
36
+ def no_hints_message?
37
+ message(:have_no_hints_message)
38
+ end
39
+
40
+ def print_hint_number(code)
41
+ message(:print_hint_number, code: code)
42
+ end
43
+
44
+ def registration_name_emptyness_error
45
+ message(:registration_name_emptyness_error)
46
+ end
47
+
48
+ def registration_name_length_error
49
+ message(:registration_name_length_error)
50
+ end
51
+
52
+ def command_error
53
+ message(:command_error)
54
+ end
55
+
56
+ def command_length_error
57
+ message(:command_length_error)
58
+ end
59
+
60
+ def command_int_error
61
+ message(:command_int_error)
62
+ end
63
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Statistics
4
+ def stats(list)
5
+ difficulties = list.group_by { |score| score[:difficulty] }
6
+ %w[hell medium easy].reduce([]) do |sorted_difficulties, difficulty_name|
7
+ if difficulties[difficulty_name]
8
+ sorted_difficulties + stats_sort(difficulties[difficulty_name])
9
+ else
10
+ sorted_difficulties
11
+ end
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def stats_sort(scores)
18
+ scores.sort_by! { |score| [score[:attempts_used], score[:hints_used]] }.reverse
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ I18n.load_path << Dir[File.expand_path('lib/app/locales/') + '/*.yml']
4
+ I18n.config.available_locales = :en
@@ -0,0 +1,39 @@
1
+ en:
2
+ start_message: "Welcome to CodeBreaker"
3
+ choice_options: "Choose option:
4
+ %{commands}"
5
+ hard_level: "Which level you do you want to play?
6
+ %{levels}"
7
+ goodbye_message: "We are waiting for you. Come back!"
8
+ command_error: "You have passed unexpected command. Please choose one from listed commands"
9
+ rules: "Codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker. The codemaker, which will be played by the application we’re going to write, creates a secret code of four numbers between 1 and 6.
10
+ The codebreaker gets some number of chances to break the code (depends on chosen difficulty). In each turn, the codebreaker makes a guess of 4 numbers. The codemaker then marks the guess with up to 4 signs - + or - or empty spaces.
11
+
12
+ A + indicates an exact match: one of the numbers in the guess is the same as one of the numbers in the secret code and in the same position. For example:
13
+ Secret number - 1234
14
+ Input number - 6264
15
+ Number of pluses - 2 (second and fourth position)
16
+
17
+ A - indicates a number match: one of the numbers in the guess is the same as one of the numbers in the secret code but in a different position. For example:
18
+ Secret number - 1234
19
+ Input number - 6462
20
+ Number of minuses - 2 (second and fourth position)
21
+
22
+ An empty space indicates that there is not a current digit in a secret number.
23
+
24
+ If codebreaker inputs the exact number as a secret number - codebreaker wins the game. If all attempts are spent - codebreaker loses.
25
+
26
+ Codebreaker also has some number of hints(depends on chosen difficulty). If a user takes a hint - he receives back a separate digit of the secret code.
27
+ "
28
+ registration: "Before start, enter your name, please "
29
+ difficulty: "You had to %{hints} hints and %{attempts} attempts"
30
+ round_message: "1. Enter your secret code
31
+ 2. hint
32
+ 3. exit "
33
+ guess: "Opps, secret code must to be from four digits in range from 1 to 6"
34
+ lost_game_message: "Oh, your attempts ended... Your code was: %{code}"
35
+ win_game_message: "Yayy, u won the game! Congrats!"
36
+ save_results_message: "Do you want to save result? 1. yes 2. no"
37
+ have_no_hints_message: "Oh, your hints ended"
38
+ print_hint_number: "Hint number: %{code}"
39
+ registration_name_length_error: "It must be more than 3 and less than 20 symbols."
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Validator
4
+ VALUE_FORMAT = /^[1-6]{4}$/.freeze
5
+
6
+ def check_emptyness(value)
7
+ value.empty?
8
+ end
9
+
10
+ def check_length(value, min_size, max_size)
11
+ value.size.between?(min_size, max_size)
12
+ end
13
+
14
+ def check_command_range(command)
15
+ command =~ VALUE_FORMAT
16
+ end
17
+ end