Guest User

Untitled

a guest
Jul 6th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. import random
  2. import pandas as pd
  3. from collections import Counter
  4.  
  5. def can_form_set(cnt):
  6. jokers = cnt.get('J', 0)
  7. # Check three of a kind with jokers
  8. for t in ['I', 'C', 'A']:
  9. if cnt.get(t, 0) + jokers >= 3:
  10. return True
  11. # Check mixed set with jokers: need 3 distinct types
  12. distinct = sum(1 for t in ['I', 'C', 'A'] if cnt.get(t, 0) >= 1)
  13. if distinct + jokers >= 3:
  14. return True
  15. return False
  16.  
  17. def simulate_once(heuristic):
  18. # Initialize and shuffle deck once
  19. deck = ['I'] * 14 + ['C'] * 14 + ['A'] * 14 + ['J'] * 2
  20. random.shuffle(deck)
  21. hand = []
  22.  
  23. events_3 = success_3 = events_4 = success_4 = 0
  24. kind_trades = mixed_trades = 0
  25.  
  26. while deck:
  27. while len(hand) < 5 and deck:
  28. hand.append(deck.pop(random.randrange(len(deck))))
  29. cnt = Counter(hand)
  30. if len(hand) == 3:
  31. events_3 += 1
  32. if can_form_set(cnt): success_3 += 1
  33. if len(hand) == 4:
  34. events_4 += 1
  35. if can_form_set(cnt): success_4 += 1
  36.  
  37. if len(hand) < 5:
  38. break
  39.  
  40. cnt = Counter(hand)
  41. possible_sets = []
  42. for t in ['I', 'C', 'A']:
  43. if cnt[t] + cnt['J'] >= 3:
  44. jokers_needed = max(0, 3 - cnt[t])
  45. possible_sets.append(('kind', t, jokers_needed))
  46. distinct = sum(1 for t in ['I', 'C', 'A'] if cnt[t] >= 1)
  47. if distinct + cnt['J'] >= 3:
  48. jokers_needed = max(0, 3 - distinct)
  49. possible_sets.append(('mixed', None, jokers_needed))
  50.  
  51. # Determine sub-heuristics
  52. base, joker_pref = heuristic.split('_')
  53. # Filter by joker preference
  54. if joker_pref == 'first':
  55. with_joker = [s for s in possible_sets if s[2] > 0]
  56. p_sets = with_joker if with_joker else possible_sets
  57. else: # avoid
  58. without_joker = [s for s in possible_sets if s[2] == 0]
  59. p_sets = without_joker if without_joker else possible_sets
  60.  
  61. # Choose based on base heuristic
  62. if base == 'good':
  63. chosen = next((s for s in p_sets if s[0] == 'kind'), p_sets[0])
  64. else: # bad
  65. chosen = next((s for s in p_sets if s[0] == 'mixed'), p_sets[0])
  66.  
  67. trade_type, trade_val, jokers_needed = chosen
  68. if trade_type == 'kind':
  69. for _ in range(3 - jokers_needed):
  70. hand.remove(trade_val)
  71. for _ in range(jokers_needed):
  72. hand.remove('J')
  73. kind_trades += 1
  74. else:
  75. for t in ['I', 'C', 'A']:
  76. if t in hand:
  77. hand.remove(t)
  78. else:
  79. hand.remove('J')
  80. mixed_trades += 1
  81.  
  82. return events_3, success_3, events_4, success_4, kind_trades, mixed_trades
  83.  
  84. def run_simulation(num_games):
  85. heuristics = ['good_first', 'good_avoid', 'bad_first', 'bad_avoid']
  86. records = []
  87. for heuristic in heuristics:
  88. total_e3 = total_s3 = total_e4 = total_s4 = 0
  89. total_kind = total_mixed = 0
  90. for _ in range(num_games):
  91. e3, s3, e4, s4, kt, mt = simulate_once(heuristic)
  92. total_e3 += e3; total_s3 += s3
  93. total_e4 += e4; total_s4 += s4
  94. total_kind += kt; total_mixed += mt
  95. records.append({
  96. 'Heuristic': heuristic,
  97. 'P(Tradeable at 3 cards)': total_s3 / total_e3 if total_e3 else 0,
  98. 'P(Tradeable at 4 cards)': total_s4 / total_e4 if total_e4 else 0,
  99. 'Avg Kind Trades per Game': total_kind / num_games,
  100. 'Avg Mixed Trades per Game': total_mixed / num_games
  101. })
  102. return pd.DataFrame(records)
  103.  
  104. # Run and display
  105. df = run_simulation(20000)
  106. print(df)
  107.  
Advertisement
Add Comment
Please, Sign In to add comment