Advertisement
makispaiktis

21 stats - For 1 target and stop

Feb 27th, 2022 (edited)
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.83 KB | None | 0 0
  1. from random import shuffle
  2.  
  3. # Function 1
  4. def createCards():
  5.  
  6.     cards = []
  7.     for i in range(1, 14):
  8.         for times in range(4):
  9.             cards.append(i)
  10.     shuffle(cards)
  11.     shuffle(cards)
  12.     return cards
  13.  
  14.  
  15. # Function 2
  16. def convert(cards):
  17.  
  18.     cards2 = cards
  19.     for i in range(len(cards)):
  20.         if cards[i] == 11 or cards[i] == 12 or cards[i] == 13:
  21.             cards2[i] = 10
  22.     return cards2
  23.  
  24.  
  25. # Function 3
  26. def aces(xartia):
  27.  
  28.     counter = 0
  29.     no_aces = []
  30.     for i in range(len(xartia)):
  31.         if xartia[i] == 1:
  32.             counter += 1
  33.         else:
  34.             no_aces.append(xartia[i])
  35.     return counter, no_aces
  36.  
  37. # Function 4
  38. def rules(xartia):
  39.  
  40.     SUM = 0        # IN CASE OF 1 ACE, AFTER I WILL ADD +10 IN CASE OF 11
  41.     SUM2 = -1000   # IT WILL BE +10 IF I HAVE 1 ACE, OTHERWISE SUM1 = SUM2
  42.     counter, no_aces = aces(xartia)
  43.     if counter == 0:
  44.         SUM = sum(xartia)
  45.         SUM2 = SUM
  46.     elif counter == 4:
  47.         SUM = 24
  48.         SUM = SUM + sum(no_aces)
  49.         SUM2 = SUM
  50.     elif counter == 3:
  51.         SUM = 1 + 11 + 1
  52.         SUM = SUM + sum(no_aces)
  53.         SUM2 = SUM
  54.     elif counter == 2:
  55.         SUM = 1 + 11
  56.         SUM = SUM + sum(no_aces)
  57.         SUM2 = SUM
  58.     elif counter == 1:                      # TO METRAEI MONO GIA +1 OXI SYN 11 PROS TO PARON
  59.         SUM = 1
  60.         SUM = SUM + sum(no_aces)
  61.         SUM2 = SUM + 10
  62.     else:
  63.         print("Error in function '" + rules.__name__ + "'")
  64.  
  65.     return SUM, SUM2
  66.  
  67.  
  68. # Function 5
  69. def define_sum(xartia):
  70.  
  71.     SUM = -1000
  72.     SUM1, SUM2 = rules(xartia)
  73.     if SUM1 == SUM2:
  74.         SUM = SUM1
  75.     else:
  76.         # We are in the case of 1 ace, 2 different sums (SUM2 = SUM1 + 10)
  77.         if SUM2 <= 21:
  78.             SUM = SUM2
  79.         else:
  80.             # SUM2 = 23 for example, I have to take the SUM1
  81.             SUM = SUM1
  82.     return SUM
  83.  
  84.  
  85.  
  86. # FUNCTION 6
  87. def AI_user(user_cards, cards, target_user, offset_user):
  88.  
  89.     user_sum = define_sum(user_cards)
  90.     if user_sum < target_user:
  91.         # Prepei na proxwrisw
  92.         user_cards.append(cards[offset_user])
  93.         offset_user += 1
  94.         return AI_user(user_cards, cards, target_user, offset_user)
  95.     else:
  96.         return user_cards, cards, target_user, offset_user
  97.  
  98.  
  99. # FUNCTION 7
  100. def AI_mana(mana_cards, cards, stop_mana_tie, offset_mana, user_sum):
  101.  
  102.     # H mana proxwraei se 2 cases:
  103.     # 1. An exei sum katw apo tou user_sum
  104.     # 2. Se isopalia mporei na proxwrhsei mono an einai <= stop_mana_tie
  105.     mana_sum = define_sum(mana_cards)
  106.     if mana_sum < user_sum or mana_sum <= stop_mana_tie - 1:
  107.         # Prepei na proxwrisw
  108.         mana_cards.append(cards[offset_mana])
  109.         offset_mana += 1
  110.         return AI_mana(mana_cards, cards, stop_mana_tie, offset_mana, user_sum)
  111.     else:
  112.         return mana_cards, cards, stop_mana_tie, offset_mana, user_sum
  113.  
  114.  
  115.  
  116. # FUNCTION 8
  117. def simulate_round(target_user, stop_mana_tie):
  118.  
  119.     # 1. Create cards
  120.     cards = createCards()
  121.     cards = convert(cards)
  122.  
  123.     # 2. The first two cards are fixed, so cards are the last 50 cards
  124.     print("Cards = " + str(cards))
  125.     user_first = cards[0]
  126.     mana_first = cards[1]
  127.     cards = cards[2:]                   # Cards now have a length of 50
  128.  
  129.     # 3. Data for AI calculations
  130.     user_cards = [user_first]
  131.     mana_cards = [mana_first]
  132.     offset_user = 0                 # Poses kartes exei parei
  133.  
  134.  
  135.     # 3. AI user
  136.     user_cards, cards, target_user, offset_user = AI_user(user_cards, cards, target_user, offset_user)
  137.     user_sum = define_sum(user_cards)
  138.     print("User: sum = " + str(user_sum) + ", cards = " + str(user_cards))
  139.  
  140.     # 4. AI mana
  141.     offset_mana = offset_user
  142.     mana_cards, cards, stop_mana_tie, offset_mana, user_sum = AI_mana(mana_cards, cards, stop_mana_tie, offset_mana, user_sum)
  143.     mana_sum = define_sum(mana_cards)
  144.     print("Mana: sum = " + str(mana_sum) + ", cards = " + str(mana_cards))
  145.     return user_sum, mana_sum
  146.  
  147.  
  148. # FUNCTION 9
  149. def simulate_round_winner(target_user, stop_mana_tie):
  150.  
  151.     user_sum, mana_sum = simulate_round(target_user, stop_mana_tie)
  152.     winner = -1000                   # winner = 1 for user win, winner = 0 for a tie result and winner = -1 for mana win
  153.     if user_sum > 21:
  154.         winner = -1
  155.         print("Mana wins!")
  156.     else:
  157.         if mana_sum > 21:
  158.             winner = 1
  159.             print("User wins!")
  160.         else:                                       # In case of 2 sums <= 21
  161.             if user_sum > mana_sum:
  162.                 winner = 1
  163.                 print("User wins!")
  164.             elif user_sum < mana_sum:
  165.                 winner = -1
  166.                 print("Mana wins!")
  167.             else:
  168.                 winner = 0
  169.                 print("It's a tie!")
  170.     print()
  171.     return winner
  172.  
  173.  
  174.  
  175.  
  176. # FUNCTION 10
  177. def simulate_stats(target_user, stop_mana_tie, num_of_sims):
  178.  
  179.     user_wins = 0
  180.     mana_wins = 0
  181.     ties = 0
  182.     for i in range(num_of_sims):
  183.         print("******** ROUND " + str(i + 1) + " ********")
  184.         winner = simulate_round_winner(target_user, stop_mana_tie)
  185.         if winner == 1:
  186.             user_wins += 1
  187.         elif winner == -1:
  188.             mana_wins += 1
  189.         else:
  190.             ties += 1
  191.     print()
  192.     print()
  193.     print("Simulations: " + str(num_of_sims))
  194.     print("User wins: " + str(user_wins) + " (" + str(100 * user_wins / num_of_sims) + "%)")
  195.     print("Mana wins: " + str(mana_wins) + " (" + str(100 * mana_wins / num_of_sims) + "%)")
  196.     print("     Ties: " + str(ties) + " (" + str(100 * ties / num_of_sims) + "%)")
  197.  
  198.  
  199.  
  200.  
  201. # MAIN FUNCTION
  202. # Data
  203. target_user = 15                    # AN FTASW SE AYTO, STOP
  204. stop_mana_tie = 16                  # AN H MANA EINAI ISOPALIA ME TON USER SE AYT, STOP, PIO KATW PROXWRAEI
  205. num_of_sims = 100
  206. simulate_stats(target_user, stop_mana_tie, num_of_sims)
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement