fork download
  1. import random
  2.  
  3.  
  4. class Card():
  5. def __init__(self, suit, rank, suit_symbol, rank_symbol):
  6. self.rank = rank#ランク番号
  7. self.suit = suit#スート番号
  8. self.rank_symbol = rank_symbol#ランクシンボル
  9. self.suit_symbol = suit_symbol#スートシンボル
  10. self.expression = suit_symbol + rank_symbol#出力する時のカードの見映え・表層・表現
  11.  
  12. def __str__(self):
  13. return self.expression
  14.  
  15.  
  16. class Deck():
  17. def __init__(self):
  18. self.this = []#デッキ自身・デッキそのもの
  19.  
  20. def discard(self, index=None):#カードを出す
  21. if index is None:
  22. return self.this.pop()
  23. else:
  24. temp = self.this[index]
  25. del self.this[index]
  26. return temp
  27.  
  28. def draw(self, card):#カードを引く
  29. self.this.append(card);
  30.  
  31. def __str__(self):
  32. temp = [x.__str__() for x in self.this]
  33. return " ".join(temp)
  34.  
  35. def shuffle(self):
  36. random.shuffle(self.this)
  37.  
  38.  
  39. class Player():
  40. def __init__(self):
  41. self.hand = Deck()
  42.  
  43. def draw(self, card):
  44. self.hand.draw(card)
  45.  
  46. def discard(self, index=None):
  47. if index is None:
  48. return sef.hand.discard()
  49. else:
  50. return self.hand.discard(index)
  51.  
  52. def __str__(self):
  53. return self.hand.__str__()
  54.  
  55.  
  56. class CardGenerator():
  57. @staticmethod
  58. def generate():
  59. suit_symbol = ["B","C","D","E"]
  60. rank_symbol = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
  61.  
  62. deck = Deck()
  63.  
  64. for suit in range(len(suit_symbol)):
  65. for rank in range(len(rank_symbol)):
  66. deck.draw(Card(suit, rank+1, suit_symbol[suit], rank_symbol[rank]))
  67.  
  68. return deck
  69.  
  70.  
  71. def main():
  72. deck = CardGenerator.generate()
  73. player = Player()
  74.  
  75. deck.shuffle()
  76.  
  77. for _ in range(5):
  78. player.draw(deck.discard())
  79.  
  80. print(player)
  81.  
  82.  
  83. main()
  84.  
Success #stdin #stdout 0.03s 11812KB
stdin
Standard input is empty
stdout
C7 C4 D2 BQ B5