Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- GAME_SETTINGS =
{ easy: { attempts: 10, symbols_count: 3, symbols_range: 6, hints: 3 }, medium: { attempts: 10, symbols_count: 4, symbols_range: 6, hints: 1 }, hard: { attempts: 20, symbols_count: 6, symbols_range: 6, hints: 1 }, very_hard: { attempts: 25, symbols_count: 4, symbols_range: 15, hints: 1 }, gosu: { attempts: 35, symbols_count: 6, symbols_range: 15, hints: 0 }, mlg: { attempts: 1, symbols_count: 4, symbols_range: 6, hints: 0 } }
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#elements_revealed ⇒ Object
readonly
Returns the value of attribute elements_revealed.
-
#hints_left ⇒ Object
readonly
Returns the value of attribute hints_left.
-
#max_attempts ⇒ Object
readonly
Returns the value of attribute max_attempts.
-
#max_hints ⇒ Object
readonly
Returns the value of attribute max_hints.
-
#score ⇒ Object
readonly
Returns the value of attribute score.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#symbols_count ⇒ Object
readonly
Returns the value of attribute symbols_count.
-
#symbols_range ⇒ Object
readonly
Returns the value of attribute symbols_range.
Instance Method Summary collapse
- #attempts_taken ⇒ Object
- #guess(code) ⇒ Object
- #hint ⇒ Object
- #hint? ⇒ Boolean
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #lose ⇒ Object
- #lose? ⇒ Boolean (also: #lost?)
- #restart ⇒ Object
- #start(diff = :medium) ⇒ Object
- #win ⇒ Object
- #win? ⇒ Boolean (also: #won?)
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
14 15 16 |
# File 'lib/codebreaker/game.rb', line 14 def initialize @state = :initialized end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def attempts @attempts end |
#elements_revealed ⇒ Object (readonly)
Returns the value of attribute elements_revealed.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def elements_revealed @elements_revealed end |
#hints_left ⇒ Object (readonly)
Returns the value of attribute hints_left.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def hints_left @hints_left end |
#max_attempts ⇒ Object (readonly)
Returns the value of attribute max_attempts.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def max_attempts @max_attempts end |
#max_hints ⇒ Object (readonly)
Returns the value of attribute max_hints.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def max_hints @max_hints end |
#score ⇒ Object (readonly)
Returns the value of attribute score.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def score @score end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def secret @secret end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def settings @settings end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def state @state end |
#symbols_count ⇒ Object (readonly)
Returns the value of attribute symbols_count.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def symbols_count @symbols_count end |
#symbols_range ⇒ Object (readonly)
Returns the value of attribute symbols_range.
3 4 5 |
# File 'lib/codebreaker/game.rb', line 3 def symbols_range @symbols_range end |
Instance Method Details
#attempts_taken ⇒ Object
74 75 76 |
# File 'lib/codebreaker/game.rb', line 74 def attempts_taken @max_attempts - @attempts end |
#guess(code) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/codebreaker/game.rb', line 36 def guess(code) fail IndexError if code.length != @secret.length fail ArgumentError unless code.all? { |x| (1..@symbols_range).include?(x) } if code == @secret win else @attempts -= 1 lose if @attempts.zero? end secret = @secret.dup [extract_exact_matches(secret, code), extract_close_matches(secret, code)] end |
#hint ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/codebreaker/game.rb', line 78 def hint if @hints_left.zero? fail 'No more hints available' else random_symbol_index = 0 loop do random_symbol_index = rand(@secret.size) break unless @elements_revealed.include? random_symbol_index end @elements_revealed << random_symbol_index @hints_left -= 1 hint = ([nil]*@secret.size) hint[random_symbol_index] = @secret[random_symbol_index] hint end end |
#hint? ⇒ Boolean
70 71 72 |
# File 'lib/codebreaker/game.rb', line 70 def hint? @hints_left > 0 end |
#lose ⇒ Object
65 66 67 68 |
# File 'lib/codebreaker/game.rb', line 65 def lose @score = 0 @state = :lost end |
#lose? ⇒ Boolean Also known as: lost?
55 56 57 |
# File 'lib/codebreaker/game.rb', line 55 def lose? @state == :lost end |
#restart ⇒ Object
32 33 34 |
# File 'lib/codebreaker/game.rb', line 32 def restart start(@settings) end |
#start(diff = :medium) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/codebreaker/game.rb', line 18 def start(diff = :medium) @settings = diff settings = GAME_SETTINGS[diff] @max_attempts = settings[:attempts] @attempts = @max_attempts @symbols_count = settings[:symbols_count] @symbols_range = settings[:symbols_range] @secret = generate_code @max_hints = settings[:hints] @hints_left = @max_hints @elements_revealed = [] @state = :playing end |
#win ⇒ Object
60 61 62 63 |
# File 'lib/codebreaker/game.rb', line 60 def win @score = @attempts * @symbols_count * @symbols_range + @hints_left*20 @state = :won end |
#win? ⇒ Boolean Also known as: won?
50 51 52 |
# File 'lib/codebreaker/game.rb', line 50 def win? @state == :won end |