Class: RavCodebreaker::Game
- Inherits:
-
Object
- Object
- RavCodebreaker::Game
- Defined in:
- lib/rav_codebreaker/game.rb
Constant Summary collapse
- TURNS_COUNT =
{expert: 10, master: 15, beginner: 20}
- HINTS_COUNT =
{expert: 0, master: 1, beginner: 2}
Instance Attribute Summary collapse
-
#hints_left ⇒ Object
readonly
Returns the value of attribute hints_left.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#offer ⇒ Object
Returns the value of attribute offer.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
-
#turns_left ⇒ Object
readonly
Returns the value of attribute turns_left.
Instance Method Summary collapse
- #decode_offer ⇒ Object
- #exit? ⇒ Boolean
- #format_error? ⇒ Boolean
- #game_over? ⇒ Boolean
- #get_hint ⇒ Object
-
#initialize(level = :expert) ⇒ Game
constructor
A new instance of Game.
- #next_turn ⇒ Object
- #show_hint? ⇒ Boolean
- #start ⇒ Object
- #win? ⇒ Boolean
Constructor Details
#initialize(level = :expert) ⇒ Game
Returns a new instance of Game.
9 10 11 12 13 |
# File 'lib/rav_codebreaker/game.rb', line 9 def initialize(level = :expert) @secret_code = '' @turns_left = 0 TURNS_COUNT.keys.include?(level) ? @level = level : @level = :expert end |
Instance Attribute Details
#hints_left ⇒ Object (readonly)
Returns the value of attribute hints_left.
6 7 8 |
# File 'lib/rav_codebreaker/game.rb', line 6 def hints_left @hints_left end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
6 7 8 |
# File 'lib/rav_codebreaker/game.rb', line 6 def level @level end |
#offer ⇒ Object
Returns the value of attribute offer.
7 8 9 |
# File 'lib/rav_codebreaker/game.rb', line 7 def offer @offer end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
6 7 8 |
# File 'lib/rav_codebreaker/game.rb', line 6 def secret_code @secret_code end |
#turns_left ⇒ Object (readonly)
Returns the value of attribute turns_left.
6 7 8 |
# File 'lib/rav_codebreaker/game.rb', line 6 def turns_left @turns_left end |
Instance Method Details
#decode_offer ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/rav_codebreaker/game.rb', line 22 def decode_offer code = @secret_code.split('') offer = @offer.split('') 4.times{|i| code[i] = offer[i] = nil if @secret_code[i] == @offer[i]} offer.compact! offer.each{|num| code[code.index(num)] = '-' if code.include? num} '+' * code.count(nil) + '-' * code.count('-') end |
#exit? ⇒ Boolean
50 51 52 |
# File 'lib/rav_codebreaker/game.rb', line 50 def exit? @offer =~ /^[qQ]$/ end |
#format_error? ⇒ Boolean
46 47 48 |
# File 'lib/rav_codebreaker/game.rb', line 46 def format_error? @offer !~ /^[1-6]{4}$/ && @offer !~ /^[hH]$/ end |
#game_over? ⇒ Boolean
54 55 56 |
# File 'lib/rav_codebreaker/game.rb', line 54 def game_over? @turns_left < 1 end |
#get_hint ⇒ Object
31 32 33 34 35 36 |
# File 'lib/rav_codebreaker/game.rb', line 31 def get_hint return nil if @hints_left < 1 @hints_left -= 1 pos = rand(4) [@secret_code[pos], pos] end |
#next_turn ⇒ Object
38 39 40 |
# File 'lib/rav_codebreaker/game.rb', line 38 def next_turn @turns_left -= 1 end |
#show_hint? ⇒ Boolean
58 59 60 |
# File 'lib/rav_codebreaker/game.rb', line 58 def show_hint? @offer =~ /^[hH]$/ end |
#start ⇒ Object
15 16 17 18 19 20 |
# File 'lib/rav_codebreaker/game.rb', line 15 def start @secret_code = Array.new(4){rand(1..6)}*'' @turns_left = TURNS_COUNT[@level] @hints_left = HINTS_COUNT[@level] @offer = '6666' end |
#win? ⇒ Boolean
42 43 44 |
# File 'lib/rav_codebreaker/game.rb', line 42 def win? decode_offer == '++++' end |