Class: DkCodebreaker::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player = nil, attempts = 10) ⇒ Game

Returns a new instance of Game.



6
7
8
9
10
11
12
13
14
15
# File 'lib/dk_codebreaker/game.rb', line 6

def initialize(player = nil, attempts = 10)
  @submit_code = nil
  @secret_code = ''
  @player = player
  @attempt = 1
  @hint_status = false
  @attempts = attempts
  @last_result = nil
  generate_code
end

Instance Attribute Details

#attemptObject (readonly)

Returns the value of attribute attempt.



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

def attempt
  @attempt
end

#attemptsObject

Returns the value of attribute attempts.



4
5
6
# File 'lib/dk_codebreaker/game.rb', line 4

def attempts
  @attempts
end

#hint_statusObject (readonly)

Returns the value of attribute hint_status.



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

def hint_status
  @hint_status
end

#playerObject

Returns the value of attribute player.



4
5
6
# File 'lib/dk_codebreaker/game.rb', line 4

def player
  @player
end

Instance Method Details

#guess(submit_code) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dk_codebreaker/game.rb', line 25

def guess(submit_code)
  if @attempt < @attempts
    if submit_code.is_a? String
      return :less_then_four   if submit_code.size < 4
      submit_code = submit_code[0,4] if submit_code.size > 4
      submit_code.each_char { |x| return :guess_has_symbol if numeric?(x) != 0 }
      @submit_code = submit_code
      @attempt += 1
      @last_result = numbers_eq(position_eq) 
    else
      :code_not_string
    end
  else
    :you_lose
  end    
end

#hintObject



42
43
44
45
46
47
48
49
50
# File 'lib/dk_codebreaker/game.rb', line 42

def hint
  unless @hint_status
    send = '****'
    num = rand(0..3)
    send[num] = @secret_code[num]
    @hint_status = true
    send
  end
end

#restartObject



17
18
19
20
21
22
23
# File 'lib/dk_codebreaker/game.rb', line 17

def restart
  @attempt = 1
  @submit_code = nil
  @secret_code = ''
  @hint_status = false
  generate_code
end

#user_dataObject



52
53
54
55
56
57
58
59
60
# File 'lib/dk_codebreaker/game.rb', line 52

def user_data
  {
      player: @player,
      result: @last_result,
      attempt: @attempt,
      attempts: @attempts,
      time: Time.new.strftime('%m/%d/%Y/%H/%M')
  }
end