原理过程其实很简单,就不讲解了,直接上源码。
欢迎有兴趣的同学一起探讨学习!
后有附上北京大学数学院的魔术数学方面的分析解密。
import random
from colorama import Fore, init
init()
CARS_DB = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
NAME = "嵌入式软件实战派"
# 南方1 北方2 分不清3
AREA_CODE = 1
AREA_S = ['','南方(1)', '北方(2)', '分不清(3)']
# 男1 女2
GENDER_CODE = 1
GENDER_S = ['','男(1)','女(2)']
idxs = random.sample(range(13),4)
cards = [CARS_DB[j] for j in idxs]
print(Fore.RESET+"1. 随机找四张扑克牌:"+' '.join(cards))
cards = cards + cards
print(Fore.RESET+"2. 将扑克牌撕开叠一起:"+' '.join(cards))
name_len = len(NAME)
print(Fore.RESET+"3. 我的名字(%s)有%d个字,将上面%d张牌依次放到底下"%(NAME,name_len,name_len))
for _ in range(name_len):
cards.append(cards.pop(0))
print(Fore.LIGHTBLACK_EX+' ==> '+' '.join(cards))
top3 = cards[:3]
rest = cards[3:]
pos = random.randint(1,len(rest)-1)
cards = rest[:pos]+top3+rest[pos:]
print(Fore.RESET+"4. 将上面3张扑克牌插入其他牌随意位置中间:"+' '.join(cards))
# print(top3,rest,pos)
target = cards.pop(0)
print(Fore.RESET+"5. 然后将最上面的这张牌( "+Fore.GREEN+target+Fore.RESET+" )放到屁股底下")
print(Fore.LIGHTBLACK_EX+' ==> '+' '.join(cards))
top = cards[:AREA_CODE]
rest = cards[AREA_CODE:]
pos = random.randint(1,len(rest)-1)
cards = rest[:pos]+top+rest[pos:]
print(Fore.RESET+"6. 我是(%s)人,将上面%d张扑克牌插入其他牌随意位置中间:"%(AREA_S[AREA_CODE],AREA_CODE)+' '.join(cards))
print(Fore.RESET+"7. 我是(%s)生,扔掉最上面%d张扑克牌"%(GENDER_S[GENDER_CODE],GENDER_CODE))
for _ in range(GENDER_CODE):
cards.pop(0)
print(Fore.LIGHTBLACK_EX+' ==> '+' '.join(cards))
S = "见证奇迹的时刻"
print(Fore.RESET+"8. 按照“%s”字数将上面%d张扑克牌依次放到底下"%(S,len(S)))
for _ in range(len(S)):
cards.append(cards.pop(0))
print(Fore.LIGHTBLACK_EX+' ==> '+' '.join(cards))
while len(cards) > 1:
keep = cards.pop(0)
cards.append(keep)
print(Fore.RESET+'√ 好运留下来 '+Fore.GREEN+keep+Fore.LIGHTBLACK_EX+' ==> '+' '.join(cards))
drop = cards.pop(0)
print(Fore.RESET+'× 烦恼丢出去 '+Fore.RED+drop+Fore.LIGHTBLACK_EX+' ==> '+' '.join(cards))
# cards.pop(0)
print(Fore.RESET+"9. 最后剩下的 "+Fore.GREEN+cards[0]+Fore.RESET+" 跟屁股底下的 "+Fore.GREEN+target+Fore.RESET+" 是一样的!")