Class: Play

Inherits:
Message show all
Defined in:
lib/game_codebreaker/play.rb

Instance Attribute Summary collapse

Attributes inherited from Message

#number, #user

Instance Method Summary collapse

Methods inherited from Message

#mess, #print_info, #question, #user_data

Constructor Details

#initializePlay

Returns a new instance of Play.



8
9
10
11
12
13
# File 'lib/game_codebreaker/play.rb', line 8

def initialize
  @code = ""
  @turns = 0
  @history = []
  @level = 15
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



6
7
8
# File 'lib/game_codebreaker/play.rb', line 6

def code
  @code
end

#historyObject

Returns the value of attribute history.



6
7
8
# File 'lib/game_codebreaker/play.rb', line 6

def history
  @history
end

#levelObject

Returns the value of attribute level.



6
7
8
# File 'lib/game_codebreaker/play.rb', line 6

def level
  @level
end

#turnsObject

Returns the value of attribute turns.



6
7
8
# File 'lib/game_codebreaker/play.rb', line 6

def turns
  @turns
end

#winObject

Returns the value of attribute win.



6
7
8
# File 'lib/game_codebreaker/play.rb', line 6

def win
  @win
end

Instance Method Details

#diceObject



15
16
17
18
# File 'lib/game_codebreaker/play.rb', line 15

def dice
  4.times{ @code << rand(1..6).to_s }
  @code.to_i
end

#game_over?(string) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/game_codebreaker/play.rb', line 53

def game_over?( string )
  return true if string.count("+") == 4
end

#processObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/game_codebreaker/play.rb', line 26

def process
  @level.times do
    skynet = Array.new(@code.to_s.split(//))
    human = question( mess 1 ).split(//)
    list = [@code, human.join("")]
    p result = to_think( skynet, human )
    @turns += 1; @history << ( list << result )
    p @code
    return @win = true if game_over?( result )
  end
  @win = false
end

#startObject



20
21
22
23
24
# File 'lib/game_codebreaker/play.rb', line 20

def start
  dice
  process
  Game.new( @code, @turns, @history, @win, @level )
end

#to_think(skynet, human) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/game_codebreaker/play.rb', line 39

def to_think( skynet, human )
  string = ""
  (0..3).each do |i|
    if skynet[i] == human[i]
      string += "+"
      skynet[i] = nil
      human[i] = nil
    end
  end
  skynet.compact!; human.compact!;
  human.each { |val| string += "-" if skynet.include?( val ) }
  string
end