1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
| import random from collections import Counter import matplotlib.pyplot as plt
deck = [i for i in range(1, 10) for _ in range(5)] + ['luck'] print(deck)
initial_game_zone_size = 9 max_card_count = 5 max_luck_count = 1 max_total_cards = 46
def draw_initial_game_zone(): print(random.sample(deck, initial_game_zone_size)) return random.sample(deck, initial_game_zone_size)
def draw_from_deck(deck, total_counter): if not deck: raise ValueError("牌堆已空,无法继续抽牌!")
while True: card = random.choice(deck) total_counter.update([card]) if isinstance(card, int) and total_counter[card] > max_card_count: continue elif card == 'luck' and total_counter['luck'] > max_luck_count: continue elif len(deck) > max_total_cards: continue
deck.remove(card) return card
def play_game(): score = 0 deck_copy = deck.copy() game_zone = draw_initial_game_zone() game_zone_counter = Counter(game_zone) total_counter = Counter(game_zone)
wish_number = random.choice(range(1, 10)) print(f"本轮游戏的许愿数字是: {wish_number}")
print(f"初始游戏区: {game_zone}")
draw_sequence = [] game_zone_history = [] card_additions = [] current_total_counter = [] additional_card_step = None
while any(count >= 2 for count in game_zone_counter.values()): print(f"当前游戏区: {game_zone}") print(f"当前得分: {score}")
game_zone_history.append(game_zone.copy()) current_total_counter.append(total_counter.copy()) matched = False for number, count in game_zone_counter.items(): if count >= 2: print(f"匹配数字 {number},得分 +1") to_remove_indices = [] removed_count = 0 for i, card in enumerate(game_zone): if card == number and removed_count < 2: to_remove_indices.append(i) removed_count += 1 if removed_count == 2: break for index in sorted(to_remove_indices, reverse=True): game_zone.pop(index)
score += 1
if number == wish_number: print(f"许愿数字 {number} 匹配成功,补充3张牌") added_cards = [] for _ in range(3): new_card = draw_from_deck(deck_copy, total_counter) game_zone.append(new_card) added_cards.append(new_card) draw_sequence.append(new_card) card_additions.append((added_cards, f"许愿数字 {number} 匹配成功,补充3张牌:{added_cards}")) else: print(f"补充1张牌到游戏区") added_cards = [draw_from_deck(deck_copy, total_counter)] game_zone.extend(added_cards) card_additions.append((added_cards, f"匹配数字 {number},补充1张牌{added_cards}")) draw_sequence.extend(added_cards)
matched = True break
if 'luck' in game_zone: print("运气牌触发,补充3张牌") game_zone.remove('luck') added_cards = [] for _ in range(3): new_card = draw_from_deck(deck_copy, total_counter) game_zone.append(new_card) added_cards.append(new_card) draw_sequence.append(new_card) card_additions.append((added_cards, "运气牌触发,补充3张牌"))
game_zone_counter = Counter(game_zone) print(game_zone_counter) if not matched and 'luck' not in game_zone: break
print(f"正常游戏结束!最终得分:{score}") game_zone_history.append(game_zone.copy()) current_total_counter.append(total_counter.copy()) if score >= 5 and deck_copy: print("得分大于等于5,奶一口继续从牌堆中抽一张牌并计算新的得分。") added_cards = [draw_from_deck(deck_copy, total_counter)] game_zone.extend(added_cards) game_zone_history.append(game_zone.copy()) current_total_counter.append(total_counter.copy()) card_additions.append((added_cards, f"满足奶一口规则,补充1张牌{added_cards}")) draw_sequence.extend(added_cards) print(f"奶一口新抽取的牌: {added_cards}")
game_zone_counter = Counter(game_zone) new_score = score
milk_matched = False for number, count in game_zone_counter.items(): if count >= 2: print(f"奶一口后匹配数字 {number},得分 +1") to_remove_indices = [] removed_count = 0 for i, card in enumerate(game_zone): if card == number and removed_count < 2: to_remove_indices.append(i) removed_count += 1 if removed_count == 2: break for index in sorted(to_remove_indices, reverse=True): game_zone.pop(index)
new_score += 1
if number == wish_number: print(f"奶一口后许愿数字 {number} 匹配成功,补充3张牌") added_cards = [] for _ in range(3): new_card = draw_from_deck(deck_copy, total_counter) game_zone.append(new_card) added_cards.append(new_card) draw_sequence.append(new_card) game_zone_history.append(game_zone.copy()) current_total_counter.append(total_counter.copy()) card_additions.append((added_cards, f"奶一口后许愿数字 {number} 匹配成功,补充3张牌:{added_cards}")) else: print(f"奶一口后补充1张牌到游戏区") added_cards = [draw_from_deck(deck_copy, total_counter)] game_zone.extend(added_cards) game_zone_history.append(game_zone.copy()) current_total_counter.append(total_counter.copy()) card_additions.append((added_cards, f"奶一口后匹配数字 {number},补充1张牌{added_cards}")) draw_sequence.extend(added_cards)
milk_matched = True break
if 'luck' in game_zone: print("奶一口后运气牌触发,补充3张牌") game_zone.remove('luck') added_cards = [] for _ in range(3): new_card = draw_from_deck(deck_copy, total_counter) game_zone.append(new_card) added_cards.append(new_card) draw_sequence.append(new_card) game_zone_history.append(game_zone.copy()) current_total_counter.append(total_counter.copy()) card_additions.append((added_cards, "奶一口后运气牌触发,补充3张牌"))
game_zone_counter = Counter(game_zone) print(game_zone_counter) if not milk_matched and 'luck' not in game_zone: print("奶一口后也无法再加分")
print(f"奶一口之后的新得分:{new_score}") score = new_score
return score, draw_sequence, wish_number, game_zone_history,current_total_counter, card_additions, additional_card_step
def monte_carlo_simulation(num_simulations=500000): highest_score = 0 highest_score_sequence = [] highest_wish_number = None highest_game_zone_history = [] highest_current_total_counter = [] highest_card_additions = [] highest_additional_card_step = None scores = []
for _ in range(num_simulations): score, draw_sequence, wish_number, game_zone_history, current_total_counter,card_additions, additional_card_step = play_game() scores.append(score)
if score > highest_score: highest_score = score highest_score_sequence = draw_sequence highest_wish_number = wish_number highest_game_zone_history = game_zone_history highest_current_total_counter = current_total_counter highest_card_additions = card_additions highest_additional_card_step = additional_card_step
plt.hist(scores, bins=range(min(scores), max(scores) + 1), edgecolor='black') plt.xlabel('得分') plt.ylabel('出现次数') plt.title(f'游戏得分分布(模拟 {num_simulations} 次)') plt.show()
return highest_score, highest_score_sequence, highest_wish_number, highest_game_zone_history,highest_current_total_counter, highest_card_additions, highest_additional_card_step
highest_score, highest_score_sequence, highest_wish_number, highest_game_zone_history,highest_current_total_counter, highest_card_additions, highest_additional_card_step = monte_carlo_simulation() print(f"最高得分: {highest_score}") print(f"最高得分时的抽牌顺序: {highest_score_sequence}") print(f"最高得分时的许愿数字: {highest_wish_number}")
print(f"最高得分时的游戏区变化:")
for idx, (history, used_count) in enumerate(zip(highest_game_zone_history, highest_current_total_counter), start=1): print(f"步骤 {idx}: {history},已经用掉的卡牌次数:{used_count}")
print(f"最高得分时的每步牌补充详情:") for idx, (cards, reason) in enumerate(highest_card_additions, start=1): print(f"步骤 {idx}: {reason} -> 补充了: {cards}")
print(f"最高得分时增加牌的步骤: {highest_additional_card_step}")
|