Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. class Card
  2. attr_reader :rank, :suit
  3.  
  4. def initialize(rank, suit)
  5. @rank = rank
  6. @suit = suit
  7. end
  8.  
  9. def output_card
  10. puts "#{self.rank} of #{self.suit}"
  11. end
  12. end
  13.  
  14. class Deck
  15.  
  16. def initialize
  17. @deck = []
  18. suits = ["Spades", "Diamonds", "Hearts", "Clubs"]
  19. ranks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
  20.  
  21. suits.each do |suit|
  22. ranks.each do |rank|
  23. card = Card.new(rank, suit)
  24. @deck << card
  25. end
  26. end
  27. end
  28.  
  29. def output_deck
  30. @deck.each do |card|
  31. card.output_card
  32. end
  33. end
  34.  
  35. def shuffle
  36. @deck.shuffle!
  37. end
  38.  
  39. def deal
  40. if @deck.empty?
  41. nil
  42. else
  43. @deck.shift
  44. end
  45. end
  46. end
  47.  
  48.  
  49.  
  50. deck = Deck.new
  51. deck.shuffle
  52. nextcard = "y"
  53.  
  54. while nextcard == "y"
  55. card = deck.deal
  56. card.output_card
  57. puts "Would you like a card from the deck? y/n: "
  58. nextcard = gets.chomp
  59.  
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement