Class: NeuroGammon::Dice

Inherits:
Object
  • Object
show all
Defined in:
lib/neuro_gammon/dice.rb

Instance Method Summary collapse

Constructor Details

#initializeDice

Returns a new instance of Dice.



20
21
22
23
# File 'lib/neuro_gammon/dice.rb', line 20

def initialize
  @values=[]
  roll
end

Instance Method Details

#[](x) ⇒ Object



36
37
38
# File 'lib/neuro_gammon/dice.rb', line 36

def [] x
  return @values[x]
end

#check_stateObject



55
56
57
58
# File 'lib/neuro_gammon/dice.rb', line 55

def check_state
  @double=(@values[0]==@values[1])    
  @values = double? ? Array.new(4,self[0]) : @values
end

#double?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/neuro_gammon/dice.rb', line 32

def double?
  @double
end

#eachObject



44
45
46
# File 'lib/neuro_gammon/dice.rb', line 44

def each
  to_a.each {|obj| yield(obj)}
end

#rollObject



25
26
27
28
29
30
# File 'lib/neuro_gammon/dice.rb', line 25

def roll
  @values=[]
  @values << rand(6)+1
  @values << rand(6)+1
  check_state
end

#to_aObject



40
41
42
# File 'lib/neuro_gammon/dice.rb', line 40

def to_a
  @values
end

#use!(value) ⇒ Object

Raises:

  • (Exception)


48
49
50
51
52
53
# File 'lib/neuro_gammon/dice.rb', line 48

def use! value
  #TODO is there a method on Array to delete one value only?
  i=@values.index(value)
  raise Exception.new("No value " << value << " in dice " << self.to_a.to_s) if i==nil
  @values.delete_at(i)
end