Class: NeuroGammon::Dice
- Inherits:
-
Object
- Object
- NeuroGammon::Dice
- Defined in:
- lib/neuro_gammon/dice.rb
Instance Method Summary collapse
- #[](x) ⇒ Object
- #check_state ⇒ Object
- #double? ⇒ Boolean
- #each ⇒ Object
-
#initialize ⇒ Dice
constructor
A new instance of Dice.
- #roll ⇒ Object
- #to_a ⇒ Object
- #use!(value) ⇒ Object
Constructor Details
#initialize ⇒ Dice
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_state ⇒ Object
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
32 33 34 |
# File 'lib/neuro_gammon/dice.rb', line 32 def double? @double end |
#each ⇒ Object
44 45 46 |
# File 'lib/neuro_gammon/dice.rb', line 44 def each to_a.each {|obj| yield(obj)} end |
#roll ⇒ Object
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_a ⇒ Object
40 41 42 |
# File 'lib/neuro_gammon/dice.rb', line 40 def to_a @values end |
#use!(value) ⇒ Object
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 |