Advertisement
makispaiktis

Solitaire Successes

Jan 4th, 2021 (edited)
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. from random import shuffle
  2. from timeit import default_timer as timer
  3.  
  4. # 1. First, I will create the cards deck
  5. symbols = ["A", "B", "C", "D"]
  6. numbers = [str(i) for i in range(1, 14)]
  7. cards = list()
  8. for symbol in symbols:
  9.     for number in numbers:
  10.         card = symbol + number
  11.         cards.append(card)
  12.  
  13. # Function 1 - Checks if a card matches with another card
  14. def cardMatch(card, cardToBeCompared):
  15.     # I will compare the variables: "card" and "cardToBeCompared"
  16.     # First, of all we have to check if they have the same symbol
  17.     sym1 = card[0]
  18.     sym2 = cardToBeCompared[0]
  19.     num1 = int(card[1:])
  20.     num2 = int(cardToBeCompared[1:])
  21.     if sym1 != sym2:
  22.         return False
  23.     else:
  24.         # Now, I have to check if num1, num2 are consecutive
  25.         if (num1 == 1 and num2 == 13) or (num1 == 13 and num2 == 1):
  26.             return True
  27.         else:
  28.             # Here, we don't have the numbers combination 1-13
  29.             if (num1 == num2 + 1) or (num1 == num2 - 1):
  30.                 return True
  31.             else:
  32.                 return False
  33.  
  34.  
  35.  
  36. # Function 2 - Checks if a card matches with 1 of the down cards (in our problem we will have 7 down cards)
  37. def cardMatchSynolo(card, SYNOLO):
  38.     N = len(SYNOLO)
  39.     noHits = 0
  40.     for i in range(N):
  41.         cardToBeCompared = SYNOLO[i]
  42.         flag = cardMatch(card, cardToBeCompared)
  43.         if flag == False:
  44.             noHits += 1
  45.     # So, we have made N "comparisons" of our card. If noHits = unsuccessful_comparisons == N ----> I want to return False
  46.     if noHits == N:
  47.         return False
  48.     else:
  49.         return True
  50.  
  51.  
  52. # Function 3 - It "breaks" the cards into a shuffled one firstly and then it will return a tuple with 2 elements
  53. # element1 = the first 7 cards of the shuffled deck
  54. # element2 = all the other cards (45)
  55. def breakCards(N):
  56.     CARDS = cards.copy()
  57.     shuffle(CARDS)
  58.     first = CARDS[0:N]
  59.     rest = CARDS[N:]
  60.     return first, rest
  61.  
  62.  
  63. # Function 4 - It will simulate the game
  64. def simulate(number):
  65.     first, rest = breakCards(number)
  66.     # print(first)
  67.     # print(rest)
  68.     N = len(first)
  69.     M = len(rest)
  70.     # If number = 7, we have 45 (for example) cards in our "rest" variable. I will walk in steps of 3.
  71.     # Each card I take in the steps by 3 form the deck of "rest" deck, it will be the card that I will compare
  72.     # "N" times with the SYNOLO = first.
  73.  
  74.     # I walk in 3's, so it will be M/3 comparisons
  75.     comparisons = M / 3
  76.     sum = 0
  77.     for i in range(0, M, 3):
  78.         # First, I will not play with cards 1 and 2, but with card 3
  79.         card = rest[i+2]
  80.         # print(card)
  81.         # I will compare this "card", with the SYNOLO = "first"
  82.         flag = cardMatchSynolo(card, first)
  83.         if flag == False:
  84.             sum += 1
  85.  
  86.     # Now, the variable "sum" will tell me how many times, we had NO-SUCCESS
  87.     # If this "sum" == comparisons, then my function returns False
  88.     if sum == comparisons:
  89.         print(first)
  90.         print(rest)
  91.         print()
  92.         return False
  93.     else:
  94.         return True
  95.  
  96.  
  97. # MAIN FUNCTION
  98. sum = 0
  99. numOfSimulations = 10000
  100. start = timer()
  101. for i in range(numOfSimulations):
  102.     if simulate(7) == False:
  103.         sum += 1
  104. end = timer()
  105. percentage = 100 * sum / numOfSimulations
  106. percentage = round(percentage, 4)
  107. elapsed = end - start
  108. elapsed = round(elapsed, 2)
  109. print()
  110. print("****************************************************")
  111. print("The solitaire did not success in " + str(sum) + " / " + str(numOfSimulations) + " tries.")
  112. print("Failure rate = " + str(percentage) + " %")
  113. print("Execution time for " + str(numOfSimulations) + " simulations ----> " + str(elapsed) + " seconds.")
  114. print("****************************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement