Class: Codeguessing::Game

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

Constant Summary collapse

MAX_HINT =
2
MAX_ATTEMPTS =
5
MAX_SIZE =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Game

Returns a new instance of Game.



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

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

#cur_gameObject



78
79
80
81
82
83
84
85
# File 'lib/codeguessing/game.rb', line 78

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



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

def cur_score(name = 'Anonim')
  hash = cur_game
  hash[:name] = name
  hash[:attempts] = MAX_SIZE - attempts
  hash[:hint_count] = MAX_HINT - hint_count
  hash[:date] = Time.now.to_i
  hash
end

#get_mark(code) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/codeguessing/game.rb', line 23

def get_mark(code)
  return false unless valid?(code)
  hash = {}
  res = ''
  secret_code.each_char.with_index do |char, i|
    if code[i] == char
      res += '+'
      code[i] = '_'
      hash.delete(char)
    elsif code.include?(char)
      hash[char] = '-'
    end
  end
  res += hash.values.join('')
end

#guess(code) ⇒ Object



17
18
19
20
21
# File 'lib/codeguessing/game.rb', line 17

def guess(code)
  @state = 'loose' unless check?(use_attempt)
  @state = 'win' if code == secret_code
  @answer = get_mark(code)
end

#hintObject



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

def hint
  return '' unless check?(hint_count)
  use_hint
  hint = '*' * MAX_SIZE
  index = rand(0...MAX_SIZE)
  code_char = secret_code[index]
  hint[index] = code_char
  hint
end

#use_attemptObject



63
64
65
# File 'lib/codeguessing/game.rb', line 63

def use_attempt
  @attempts -= 1
end

#use_hintObject



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

def use_hint
  @hint_count -= 1
end

#valid?(code) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#win?Boolean

Returns:

  • (Boolean)


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

def win?
  case @state
  when 'win' then true
  when 'loose' then false
  end
end