Advertisement
makispaiktis

Tichu: Monofylla vs Difyllies

Mar 8th, 2021 (edited)
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. # I will simulate the problem like this
  2. # J = 11, Q =12, K = 13, A = 14
  3. # Also, the 4 special cards will have the "value" of 15, there is no problem, it's all about simulation
  4. # I will try to make 100K or 1M simulations like this
  5. # I will randomize the deck of card and i will select the player 1. Here , I will check 2 cases
  6. # 1. If he has not even a single one card not in pairs
  7. # 2. If he has not a single pair
  8. from random import shuffle
  9. from timeit import default_timer as timer
  10.  
  11. # Function 1
  12. def player1Pick():
  13.     # 1. I will create the deck of cards, 4 of every "value"
  14.     cards = list()
  15.     for i in range(2, 16):
  16.         for j in range(4):
  17.             cards.append(i)
  18.     # I will shuffle the cards deck twice for extra randomness
  19.     shuffle(cards)
  20.     shuffle(cards)
  21.     # 2. I will take the first 14 cards of my shuffled list to simulate the 1st player's cards
  22.     # This is like taking 1/4 cards with a certain row, because now we talk about randomness
  23.     player1PickList = cards[0:14]
  24.     # 3. Now, that I have the 14 cards of the 1st player, I will sort my list to make the things easier
  25.     player1PickList = sorted(player1PickList)
  26.     # 4. I will have to exclude 15 = special cards
  27.     for i in range(len(player1PickList), len(player1PickList) - 4, -1):
  28.         if player1PickList[len(player1PickList) - 1 - i] == 15:
  29.             del player1PickList[len(player1PickList) - 1 - i]
  30.     # 5. Now, my list is sorted and does not contain any special cards
  31.     return player1PickList
  32.  
  33.  
  34. # Function 2
  35. def checkMonofylla(cards):
  36.     MonofylloExists = False
  37.     # Για να είναι ένα φύλλο σε μία θέση i της λίστας μου μονόφυλλο, θα πρέπει να είναι διαφορετικό
  38.     # και του προηγούμενου και του επόμενου, μιας και τα έχω ταξινομήσει σε αύξουσα σειρά
  39.     # Εξαίρεση αποτελεί η περίπτωση i = 0, η οποί απαιτεί ειδική μεταχείριση
  40.     if cards[0] != cards[1]:
  41.         MonofylloExists = True
  42.     if cards[len(cards)-1] != cards[len(cards)-2]:
  43.         MonofylloExists = True
  44.     for i in range(1, len(cards)-1):
  45.         if cards[i] != cards[i-1] and cards[i] != cards[i+1]:
  46.             MonofylloExists = True
  47.             break
  48.     return MonofylloExists
  49.  
  50. # Function 3
  51. def checkDifyllies(cards):
  52.     difylliaExists = False
  53.     # I will have to exclude 15 = special cards
  54.     for i in range(len(cards)-1):
  55.         if cards[i] == cards[i+1]:
  56.             difylliaExists = True
  57.             break
  58.     return difylliaExists
  59.  
  60.  
  61. # Function 4
  62. def simulate():
  63.     cards = player1Pick()
  64.     flag1 = checkMonofylla(cards)
  65.     flag2 = checkDifyllies(cards)
  66.     return cards, flag1, flag2
  67.  
  68. # MAIN FUNCTION
  69. print()
  70. start = timer()
  71. LIMIT = 10**5
  72. counter1 = 0
  73. counter2 = 0
  74.  
  75. for i in range(LIMIT):
  76.     cards, flag1, flag2 = simulate()
  77.     if flag1 == False or flag2 == False:
  78.         print(i, cards, flag1, flag2)
  79.         if flag1 == False:
  80.             counter1 += 1
  81.         if flag2 == False:
  82.             counter2 += 1
  83.  
  84. end = timer()
  85. elapsed = end - start
  86. elapsed = round(elapsed, 4)
  87. print()
  88. percentage1 = counter1 / LIMIT
  89. print("No Monofylla ----> " + str(counter1) + " / " + str(LIMIT) + " ----> percentage = " + str(100 * percentage1) + "%")
  90. percentage2 = counter2 / LIMIT
  91. print("No Difyllies ----> " + str(counter2) + " / " + str(LIMIT) + " ----> percentage = " + str(100 * percentage2) + "%")
  92. print()
  93. print("Execution time: " + str(elapsed) + " seconds")
  94. # cards = [2, 2, 2, 4, 4, 4, 4, 6, 6, 8, 8, 8, 9, 9]
  95. # print(checkMonofylla(cards))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement