SHARE
TWEET

Untitled

a guest Oct 16th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. class Deck():
  4.     def __init__(self, n=3):
  5.         self.cards = list(range(1,n+1))
  6.         random.shuffle(self.cards)
  7.         self.position = 0
  8.     def __len__(self):
  9.         return len(self.cards)
  10.     def _number_of_cards_left(self):
  11.         return len(self) - self.position
  12.     def draw(self):
  13.         assert(self._number_of_cards_left() > 0)
  14.         card = self.cards[self.position]
  15.         self.position += 1
  16.         return card
  17.     def alice_draw(self):
  18.         previousCards = self.cards[:self.position]
  19.         card = self.draw()
  20.         previousGreaterCards = len(list(filter(lambda c : card < c, previousCards)))
  21.         return (card,previousGreaterCards)
  22.  
  23.  
  24. def game(n=3):
  25.     deck = Deck(n)
  26.     final_score = 0
  27.     for turn in range(n):
  28.         (card, previousGreaterCards) = deck.alice_draw()
  29.         if previousGreaterCards == 1:
  30.             verb = "is "
  31.         else:
  32.             verb = "are "
  33.         print("Bob says, \"There " + verb + str(previousGreaterCards) + " previous card that are greater than your card.\"")
  34.         if deck._number_of_cards_left() == 0:
  35.             print("No cards left! Your score is " + str(card) + "!")
  36.             print("The deck was " + str(deck.cards))
  37.             final_score = card
  38.             break
  39.         response = input("Do you want to stop? [y/N] ")
  40.         if response.lower() == "y":
  41.             print("Your score is " + str(card) + "!")
  42.             print("The deck was " + str(deck.cards))
  43.             final_score = card
  44.             break
  45.         else:
  46.             print("Drawing another card...")
  47.     return final_score
  48.  
  49.  
  50. """
  51. >>> game(6)
  52. Bob says, "There are 0 previous card that are greater than your card."
  53. Do you want to stop? [y/N] n
  54. Drawing another card...
  55. Bob says, "There is 1 previous card that are greater than your card."
  56. Do you want to stop? [y/N] n
  57. Drawing another card...
  58. Bob says, "There are 2 previous card that are greater than your card."
  59. Do you want to stop? [y/N] y
  60. Your score is 1!
  61. The deck was [6, 5, 1, 2, 4, 3]
  62. 1
  63.  
  64. >>> game(6)
  65. Bob says, "There are 0 previous card that are greater than your card."
  66. Do you want to stop? [y/N] n
  67. Drawing another card...
  68. Bob says, "There are 0 previous card that are greater than your card."
  69. Do you want to stop? [y/N] n
  70. Drawing another card...
  71. Bob says, "There is 1 previous card that are greater than your card."
  72. Do you want to stop? [y/N] n
  73. Drawing another card...
  74. Bob says, "There are 2 previous card that are greater than your card."
  75. Do you want to stop? [y/N] y
  76. Your score is 3!
  77. The deck was [2, 6, 4, 3, 1, 5]
  78. 3
  79. """
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top