Beginner Needs Help

I am new to Ruby and OOP although I have written programs in Forth;
another
world. As a learning project, I decided to duplicate something I had
written
in Forth; namely a bridge hand dealer. I originally stored the hands in
a
3-dimensional binary array (hand, suit, cards). In cards, a set bit
represented
a particular card. I like the binary representation because it makes
things
like searching for pairs or sequences, etc. easy with bit logic.

Here is what I have written so far:

class Carddeck
attr_writer :deck
def initialize
@deck=Array.new
end

def build_deck
0.upto(51) {|i| @deck.push(i.divmod(13))}

deck consists of 52 2-member arrays, [suit, rank],

each representing a playing card. In use, the first

13 members is hand[0] and the next 13 are hand[1], etc.

hand will be defined in a subclass.

return @deck
end

def shuffle_deck
@[email protected]_by{rand}
end
end

class Bridgehands < Carddeck
attr_writer :hands
attr_reader :deck
def initialize
super
@hands = Array.new(4) {|idx| Array.new(4)}

hands is a 4 by 4 array for player,suit

end

def build_hands
self.shuffle_deck
0.upto(51) {|i| @hands[i/13][@deck[i][0]] += 0b1 << @deck[i][1]}

The left side addresses a particular cell in hands.

The right side converts the card rank to a bit shifted

to the position represented by the rank of the card.

We cycle through the entire deck and let i/13 = the hand.

return @hands
end
end

Carddeck works just fine but Bridgehands returns an error when I enter:

bh = Bridgehands.new followed by bh.build_deck and then bh.build_hands.
to
which it responds:

NoMethodError: undefined method +' for nil:NilClass from (irb):92:inbuild_hands’
from (irb):92:in `build_hands’
from (irb):103
from :0

I’m going nuts.

Charles G. – Phoenix, AZ … skype and google-talk user name:
tcykgreis

From: [email protected]

def build_deck
0.upto(51) {|i| @deck.push(i.divmod(13))}

deck consists of 52 2-member arrays, [suit, rank],

each representing a playing card. In use, the first

13 members is hand[0] and the next 13 are hand[1], etc.

hand will be defined in a subclass.

return @deck
end
[…]
def initialize
super
@hands = Array.new(4) {|idx| Array.new(4)}

hands is a 4 by 4 array for player,suit

end
[…]
def build_hands
self.shuffle_deck
0.upto(51) {|i| @hands[i/13][@deck[i][0]] += 0b1 << @deck[i][1]}

IRB shows @hands looks like:

irb(main):024:0> hands = Array.new(4) {|idx| Array.new(4)}
=> [[nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, nil],
[nil, nil, nil, nil]]

So,

@hands[0][@deck[0][0]]

is:

@hands[0][0]

which is fetching the first ‘nil’ shown above in IRB.

Regards,

Bill

P.S. FORTH LOVE IF 7 EMIT THEN