Chris Braddock on 21 Nov 2005 21:26:19 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PhillyOnRails] a little hold 'em code ... help please


A Ruby/OO noob could use a little help here.

I saw the Hold 'Em trainer project on the community wiki. Thought I might kick it around a bit.

I think I have some basic code to get a deck shuffled but there is one place where I'm a little confused (see comments) and I'd love to get some code critiques as I'm just fumbling my way through this right now.

#holdem.rb
class Card
 attr_reader :suit, :rank
 def initialize(suit, rank)
   @suit = suit
   @rank = rank
 end
end

class Deck
 attr_reader :deck
 SUITS = ["C","D","H","S"]
 RANKS = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
 def initialize
   @deck = Array.new
   SUITS.each do |suit|
     RANKS.each do |rank|
       @deck[@deck.length] = Card.new(suit, rank)
     end
   end
   self
 end
 def shuffle
   deck_shuffled = Array.new
   deck_length_orig = @deck.length
   while deck_shuffled.length < deck_length_orig
     rand_card = rand(@deck.length)
     deck_shuffled << @deck[rand_card]
     @deck.delete_at(rand_card)
   end
   @deck.replace(deck_shuffled)
 end
end

deck = Deck.new
deck.shuffle
# why do I need to use deck.deck if I'm returning self on initialize? have pointer to self object and that's <> the array?
deck.deck.each do |card|
print(card.rank, card.suit)
print "," unless deck.deck.index(card) == deck.deck.length - 1
end
puts
print("total cards: ", deck.deck.length)
puts



_______________________________________________ talk mailing list talk@phillyonrails.org http://lists.phillyonrails.org/mailman/listinfo/talk