Articles tagged ruby

Simple code for playing card
Dorren_mii_thumb by dorren, 09/15/2007
Simple code to implement playing card. In case you want to build an online casino with Ruby on Rails, you got a head start. :)

class Deck COLOR = %w(S H D C) # Spade Heart Diamond Club POINT = %w(2 3 4 5 6 7 8 9 10 J Q K A) attr_accessor :cards def initialize @cards = [] COLOR.each{|c| POINT.each{|p| @cards << Card.new(COLOR.index(c), POINT.index(p)) } } self end def shuffle 52.times{|i| r = rand(52); @cards[i], @cards[r] = @cards[r], @cards[i]} end def sort @cards.sort!{|x, y| x <=> y} end def to_s @cards.collect{|c| "#{COLOR[c.color]} #{POINT[c.point]}"}.join("\n") end end class Card attr_accessor :color, :point def initialize(color, point) @color, @point = color, point end def <=>(obj) color == obj.color ? obj.point <=> point : color <=> obj.color end end deck = Deck.new deck.shuffle puts "\n----shuffle-----\n#{deck}" deck.sort puts "\n----sort-----\n#{deck}"
Views: 1365   Replies: 0   Tags: ruby
 




login or sign up to participate.
Money_dollar moneywill