Class: Codeguessing::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/codeguessing/game.rb

Constant Summary collapse

MAX_HINT =
2
MAX_ATTEMPTS =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Game

Returns a new instance of Game.



8
9
10
11
12
13
14
# File 'lib/codeguessing/game.rb', line 8

def initialize(opt = {})
  @secret_code = opt[:secret_code] || random
  @attempts = opt[:attempts] || MAX_ATTEMPTS
  @hint_count = opt[:hint_count] || MAX_HINT
  @state = opt[:state] || ''
  @answer = opt[:answer] || ''
end

Instance Attribute Details

#answerObject

Returns the value of attribute answer.



3
4
5
# File 'lib/codeguessing/game.rb', line 3

def answer
  @answer
end

#attemptsObject

Returns the value of attribute attempts.



3
4
5
# File 'lib/codeguessing/game.rb', line 3

def attempts
  @attempts
end

#hint_countObject

Returns the value of attribute hint_count.



3
4
5
# File 'lib/codeguessing/game.rb', line 3

def hint_count
  @hint_count
end

#secret_codeObject

Returns the value of attribute secret_code.



3
4
5
# File 'lib/codeguessing/game.rb', line 3

def secret_code
  @secret_code
end

#stateObject

Returns the value of attribute state.



3
4
5
# File 'lib/codeguessing/game.rb', line 3

def state
  @state
end

Instance Method Details

#check?(varible) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/codeguessing/game.rb', line 71

def check?(varible)
  return false if varible == 0
  true
end

#cur_gameObject



49
50
51
52
53
54
55
56
# File 'lib/codeguessing/game.rb', line 49

def cur_game
  hash = {}
  self.instance_variables.each do |k, v|
    new_k = k.to_s.gsub('@','').to_sym
    hash[new_k] = self.instance_variable_get(k)
  end
  hash
end

#cur_score(name = 'Anonim') ⇒ Object



58
59
60
61
62
63
64
# File 'lib/codeguessing/game.rb', line 58

def cur_score(name = 'Anonim')
  hash = cur_game
  hash[:name] = name
  hash.delete(:answer)
  hash.delete(:state)
  hash
end

#guess(code) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/codeguessing/game.rb', line 16

def guess(code)
  loose unless check?(use_attempt)
  hash = {}
  res = ''
  code.each_char.with_index do |char, i|
    case
      when char == secret_code[i]
        res += '+'
      when secret_code.count(char) == 1 &&
           code.count(char) == 1
        hash[char] = '-'
      end
  end
  res += hash.values.join('')
  win if res == '++++'
  @answer = res
end

#hintObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/codeguessing/game.rb', line 34

def hint
  res = ''
  need_index = rand(0...4)
  secret_code.each_char.with_index do |char, index|
    if index == need_index
      res += char
    else
      res += '*'
    end
  end
  return '' unless check?(hint_count)
  use_hint
  res
end

#looseObject



88
89
90
# File 'lib/codeguessing/game.rb', line 88

def loose
  @state = 'false'
end

#randomObject



92
93
94
95
96
# File 'lib/codeguessing/game.rb', line 92

def random
  code = ''
  4.times { code += rand(1..6).to_s }
  code
end

#use_attemptObject



76
77
78
# File 'lib/codeguessing/game.rb', line 76

def use_attempt
  @attempts -= 1
end

#use_hintObject



80
81
82
# File 'lib/codeguessing/game.rb', line 80

def use_hint
  @hint_count -= 1
end

#valid?(code) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/codeguessing/game.rb', line 66

def valid?(code)
  return true if code =~ /^[1-6]{4}$/s
  false
end

#winObject



84
85
86
# File 'lib/codeguessing/game.rb', line 84

def win
  @state = 'true'
end