Class: Patience::Card
- Extended by:
- Forwardable
- Defined in:
- lib/patience/card.rb,
lib/patience/rank.rb,
lib/patience/suit.rb
Overview
Patience::Card is a card creator class. Cards have ranks and suits. Every card has its own sprite. Sprite is a one big image, which contains every play card in the game. A sprite for a card is chosen by shift on the sprite. The shift is determined by integer values on X and Y axis repsectively.
card = Card.new(13, 3)
card.to_s #=> "Ace of Spades"
card.face_down
card.face_up? #=> false
Defined Under Namespace
Classes: DefunctRank, DefunctSuit, Rank, Suit
Instance Attribute Summary collapse
-
#rank ⇒ Object
readonly
Returns the value of attribute rank.
-
#sprite ⇒ Object
readonly
Returns the value of attribute sprite.
-
#suit ⇒ Object
readonly
Returns the value of attribute suit.
Instance Method Summary collapse
-
#draw_on(win) ⇒ Object
Draws the sprite of card in the window.
-
#eql?(other_card) ⇒ Boolean
Compares two cards with each other, considering their rank and suit equality.
-
#face_down ⇒ Object
Turns the card to its back (sets position of the sprite to zero values).
-
#face_down? ⇒ Boolean
Returns true if the card is turned to its back (it means, that the sprite of the card is set position of zero).
-
#face_up ⇒ Object
Turns the card to its face.
-
#face_up? ⇒ Boolean
The opposite of the Card#face_down? method.
-
#flip! ⇒ Object
Either turns the card to its face if it’s faced down or turns it to its back if it’s faced up.
-
#initialize(rank, suit) ⇒ Card
constructor
Creates new card object.
- #overlaps?(other_card) ⇒ Boolean
-
#to_s ⇒ Object
Prints human readable rank and suit of the card.
Constructor Details
#initialize(rank, suit) ⇒ Card
Creates new card object. Both arguments should be Fixnums in the valid ranges. Also, every card has its sprite, which is nothing but an instance of the Ray::Sprite class.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/patience/card.rb', line 20 def initialize(rank, suit) @rank = case rank when 1 then Rank::Ace.new when 2 then Rank::Two.new when 3 then Rank::Three.new when 4 then Rank::Four.new when 5 then Rank::Five.new when 6 then Rank::Six.new when 7 then Rank::Seven.new when 8 then Rank::Eight.new when 9 then Rank::Nine.new when 10 then Rank::Ten.new when 11 then Rank::Jack.new when 12 then Rank::Queen.new when 13 then Rank::King.new else raise DefunctRank, "Nonexistent rank: #{rank}" end @suit = case suit when 1 then Suit::Heart.new when 2 then Suit::Diamond.new when 3 then Suit::Spade.new when 4 then Suit::Club.new else raise DefunctSuit, "Nonexistent suit: #{suit}" end @sprite = Ray::Sprite.new path_of('patience/sprites/card_deck.png') # A sheet with 14 columns and 5 rows. First row and column # corresponds to the card back (their coordinats are equal to [0, 0]). @sprite.sheet_size = [14, 5] @sprite.sheet_pos = [rank, suit] end |
Instance Attribute Details
#rank ⇒ Object (readonly)
Returns the value of attribute rank.
15 16 17 |
# File 'lib/patience/card.rb', line 15 def rank @rank end |
#sprite ⇒ Object (readonly)
Returns the value of attribute sprite.
15 16 17 |
# File 'lib/patience/card.rb', line 15 def sprite @sprite end |
#suit ⇒ Object (readonly)
Returns the value of attribute suit.
15 16 17 |
# File 'lib/patience/card.rb', line 15 def suit @suit end |
Instance Method Details
#draw_on(win) ⇒ Object
Draws the sprite of card in the window.
87 88 89 |
# File 'lib/patience/card.rb', line 87 def draw_on(win) win.draw(sprite) end |
#eql?(other_card) ⇒ Boolean
Compares two cards with each other, considering their rank and suit equality.
93 94 95 |
# File 'lib/patience/card.rb', line 93 def eql?(other_card) (@rank == other_card.rank) and (@suit == other_card.suit) end |
#face_down ⇒ Object
Turns the card to its back (sets position of the sprite to zero values).
70 71 72 |
# File 'lib/patience/card.rb', line 70 def face_down sprite.sheet_pos = [0, 0] end |
#face_down? ⇒ Boolean
Returns true if the card is turned to its back (it means, that the sprite of the card is set position of zero). Otherwise, returns false.
76 77 78 |
# File 'lib/patience/card.rb', line 76 def face_down? sprite.sheet_pos == [0, 0] end |
#face_up ⇒ Object
Turns the card to its face.
59 60 61 |
# File 'lib/patience/card.rb', line 59 def face_up sprite.sheet_pos = [rank.to_i, suit.to_i] end |
#face_up? ⇒ Boolean
The opposite of the Card#face_down? method. Returns true if the card is turned to its face. Otherwise, returns false.
65 66 67 |
# File 'lib/patience/card.rb', line 65 def face_up? self.not.face_down? end |
#flip! ⇒ Object
Either turns the card to its face if it’s faced down or turns it to its back if it’s faced up.
82 83 84 |
# File 'lib/patience/card.rb', line 82 def flip! (face_up if face_down?) or (face_down if face_up?) end |
#overlaps?(other_card) ⇒ Boolean
97 98 99 |
# File 'lib/patience/card.rb', line 97 def overlaps?(other_card) sprite.collide?(other_card.sprite) and self.not.eql?(other_card) end |
#to_s ⇒ Object
Prints human readable rank and suit of the card.
54 55 56 |
# File 'lib/patience/card.rb', line 54 def to_s "#{rank} of #{suit}" end |