Class: RavCodebreaker::Game

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_leftObject (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

#levelObject (readonly)

Returns the value of attribute level.



6
7
8
# File 'lib/rav_codebreaker/game.rb', line 6

def level
  @level
end

#offerObject

Returns the value of attribute offer.



7
8
9
# File 'lib/rav_codebreaker/game.rb', line 7

def offer
  @offer
end

#secret_codeObject (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_leftObject (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_offerObject



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

Returns:

  • (Boolean)


50
51
52
# File 'lib/rav_codebreaker/game.rb', line 50

def exit?
  @offer =~ /^[qQ]$/
end

#format_error?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


54
55
56
# File 'lib/rav_codebreaker/game.rb', line 54

def game_over?
  @turns_left < 1
end

#get_hintObject



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_turnObject



38
39
40
# File 'lib/rav_codebreaker/game.rb', line 38

def next_turn
  @turns_left -= 1
end

#show_hint?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rav_codebreaker/game.rb', line 58

def show_hint?
  @offer =~ /^[hH]$/
end

#startObject



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

Returns:

  • (Boolean)


42
43
44
# File 'lib/rav_codebreaker/game.rb', line 42

def win?
  decode_offer == '++++'
end