Class: CodebreakerAp::Game
- Inherits:
-
Object
- Object
- CodebreakerAp::Game
- Defined in:
- lib/codebreaker_ap/game.rb
Constant Summary collapse
- LOCALES_FOLDER =
File.('storage_files', __dir__)
- CODE_LENGTH =
4
- CODE_LENGTH_RANGE =
(1..4).freeze
- SECRET_CODE_NUMBERS_RANGE =
(1..6).freeze
- IN_POSITION =
'+'.freeze
- NOT_IN_POSITION =
'-'.freeze
Constants included from Statistic
Instance Attribute Summary collapse
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
Instance Method Summary collapse
- #check_answer(answer) ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #show_rules ⇒ Object
- #take_hint ⇒ Object
- #win? ⇒ Boolean
Methods included from Statistic
#load_statistic, #save_stats, #show_stats
Methods included from Storage
#read_from_file, #save_to_file
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
15 16 17 18 19 20 |
# File 'lib/codebreaker_ap/game.rb', line 15 def initialize @difficulty = CodebreakerAp::Difficulty.new @secret_code = generate_secret_code @hints_code = secret_code.to_a.dup.shuffle @game_win = false end |
Instance Attribute Details
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
13 14 15 |
# File 'lib/codebreaker_ap/game.rb', line 13 def difficulty @difficulty end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
13 14 15 |
# File 'lib/codebreaker_ap/game.rb', line 13 def secret_code @secret_code end |
Instance Method Details
#check_answer(answer) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/codebreaker_ap/game.rb', line 22 def check_answer(answer) answer = answer.to_s @game_win = true if @secret_code.join == answer @difficulty.attempts -= 1 compare_answer_with_secret(answer.split(''), secret_code.join.split('')) end |
#show_rules ⇒ Object
39 40 41 42 43 44 |
# File 'lib/codebreaker_ap/game.rb', line 39 def show_rules rules = read_from_file("#{LOCALES_FOLDER}/#{Message.new.file_rules}") return unless rules rules end |
#take_hint ⇒ Object
35 36 37 |
# File 'lib/codebreaker_ap/game.rb', line 35 def take_hint difficulty.hint(@hints_code) end |
#win? ⇒ Boolean
31 32 33 |
# File 'lib/codebreaker_ap/game.rb', line 31 def win? @game_win end |