Advertisement
makispaiktis

21 - Different combinations - 3D plot with win percentages

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