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.



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

def initialize player = nil, attempts = 10
  @submit_code = nil
  @secret_code = ""
  @player = player
  @attempt = 1
  @hint_status = false
  @limit = attempts
  @last_result = nil
end

Instance Attribute Details

#attemptObject (readonly)

Returns the value of attribute attempt.



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

def attempt
  @attempt
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.



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

def player
  @player
end

Instance Method Details

#guess(submit_code) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dk_codebreaker/game.rb', line 29

def guess submit_code
  if @attempt <= @limit
    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



46
47
48
49
50
51
52
53
54
# File 'lib/dk_codebreaker/game.rb', line 46

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

#restartObject



21
22
23
24
25
26
27
# File 'lib/dk_codebreaker/game.rb', line 21

def restart
  @attempt = 1
  @submit_code = nil
  @secret_code = ""
  @hint_status = false
  start
end

#startObject



17
18
19
# File 'lib/dk_codebreaker/game.rb', line 17

def start
  1.upto(4) { @secret_code << "#{rand(1..6)}" }
end

#user_dataObject



56
57
58
# File 'lib/dk_codebreaker/game.rb', line 56

def user_data
  [@last_result, @player, @attempt, @limit, Time.new.strftime("%m/%d/%Y/%H/%M")]
end