你好,我是公众号3分钟学堂的郭立员!
春晚上由于小尼的失误,使得刘谦的魔术成了热门话题,不少人开始分析这个魔术的原理是什么,下面我们跟着刘谦的步骤写一下代码:
一、随机拿4张不一样的扑克牌打乱顺序
为了保证不一样我们准备13张扑克牌{"A","2","3","4","5","6","7","8","9","10","J","Q","K"}从里面随机拿出4张牌。
Dim cards = { "A" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" }Dim Rnd_4_card = Rnd_Sample(cards, 4 )TracePrint "打乱扑克牌:" ,encode.TableToJson(Rnd_4_card) Function Rnd_Sample(cards,n) Dim arr = { null } Dim new_cards = Rnd_Shuffle(cards) For i = 1 To n arr[i] = new_cards[i] Next Rnd_Sample = arrEnd Function Function Rnd_Shuffle(cards) Dim container For i = 1 To UBOUND(cards) dim Rnd_num = Int(Len(cards)*rnd() + 1 ) container = cards[i] cards[i] = cards[Rnd_num] cards[Rnd_num] = container Next Rnd_shuffle = cardsEnd Function
输出结果:
打乱扑克牌: [ "9" , "2" , "A" , "4" ]
二、把4张扑克牌对折后撕开,并叠放在一起
原本4个随机数翻倍变成8个
Dim Rnd_8_card = Multiple(Rnd_4_card,2) TracePrint "撕开后的8张扑克牌:",encode.TableToJson(Rnd_8_card) Function Multiple(cards,n) Dim arr = {null} For j = 1 To n For i = 1 To Len(cards) arr[Len(cards)*(j-1)+i] = cards[i] Next Next Multiple = arr End Function
输出结果:
打乱扑克牌: [ "9" , "2" , "A" , "4" ] 撕开后的8张扑克牌: [ "9" , "2" , "A" , "4" , "9" , "2" , "A" , "4" ]
三、根据自己名字的字数,从扑克牌顶一张一张的拿牌放到扑克牌底,名字是几个字就拿几张牌。
Dim name = "郭立员" Dim name_num = utf8.len(name) TracePrint "我的名字字数是:"&name_num Rnd_8_card = Rotate(Rnd_8_card,name_num) TracePrint "新的8张扑克牌:",encode.TableToJson(Rnd_8_card) Function Rotate(cards,n) Dim arr = {null} For i = 1 To Len(cards) If i + n Len(cards) Then arr[i] = cards[i + n - Len(cards)] Else arr[i] = cards[i + n] End If Next Rotate = arr End Function
输出结果:
打乱扑克牌: [ "9" , "2" , "A" , "4" ] 撕开后的8张扑克牌: [ "9" , "2" , "A" , "4" , "9" , "2" , "A" , "4" ] 我的名字字数是:3新的8张扑克牌: [ "4" , "9" , "2" , "A" , "4" , "9" , "2" , "A" ]
四、取上面3张牌放入剩余牌的中间位置
把数组里面前3个数放到后5个数的中间,8个数字分成3和5两组,先找后5个的**位置,注意不能是首尾。
Rnd_8_card = insert (Rnd_8_card, 3 )TracePrint "新的8张扑克牌:" ,encode.TableToJson(Rnd_8_card) Function insert (cards,n) Dim arr = { null } For i = 1 To len (cards)-n arr[i] = cards[i + n] Next TracePrint "后面张扑克牌:" ,encode.TableToJson(arr) Dim position = int (( len (cards)-(n+ 1 ))*rnd() + 1 ) TracePrint "**位置:" , position For i = n To 1 step -1 arr = zm.ArrayInsert (arr,cards[i], position ) Next insert = arr End Function
输出结果:
新的8张扑克牌: [ "4" , "9" , "2" , "A" , "4" , "9" , "2" , "A" ] 后面张扑克牌: [ "A" , "4" , "9" , "2" , "A" ] **位置: 4新的8张扑克牌: [ "A" , "4" , "9" , "2" , "4" , "9" , "2" , "A" ]
五、将最上面一张藏起来,最后见证奇迹的时刻要使用
用一个变量单独存放数组的靠前个数据。
Dim firs_card = Rnd_8_card[1] TracePrint "藏起来1张牌:",firs_card Rnd_8_card = zm.ArrayRemove(Rnd_8_card, 0) TracePrint "剩余7张牌:",encode.TableToJson(Rnd_8_card)
输出结果:
新的8张扑克牌: [ "A" , "4" , "9" , "4" , "9" , "2" , "2" , "A" ] 藏起来1张牌: A 剩余7张牌: [ "4" , "9" , "4" , "9" , "2" , "2" , "A" ]
注意:藏得牌是A
六、根据南北方得到新的排序
从牌顶取若干张牌,北方 1,南方 2,不确定 3,**剩余牌中
Dim area = "1" // 北方 1,南方 2,不确定 3 Rnd_8_card = insert(Rnd_8_card, area)TracePrint "剩余7张牌:" ,encode.TableToJson(Rnd_8_card)
输出结果:
剩余7张牌: [ "4" , "4" , "9" , "2" , "9" , "2" , "A" ] 后面张扑克牌: [ "4" , "9" , "2" , "9" , "2" , "A" ] **位置: 5剩余7张牌: [ "4" , "9" , "2" , "9" , "2" , "4" , "A" ]
七、根据性别扔牌
男生扔一张,女生扔两种
Dim gender = "1" // 男生 1 , 女生 2 Rnd_8_card = zm.ArrayRemove(Rnd_8_card, gender)TracePrint "剩余牌:" ,encode.TableToJson(Rnd_8_card)
输出结果:
剩余7张牌: [ "9" , "2" , "4" , "9" , "2" , "4" , "A" ] 剩余牌: [ "2" , "4" , "9" , "2" , "4" , "A" ]
八、见证奇迹的时刻
“见证奇迹的时刻”一共是7个字,那就是牌从上往下滚动7次。
Rnd_8_card = Rotate(Rnd_8_card,code_num)TracePrint "剩余牌:" ,encode.TableToJson(Rnd_8_card)
输出结果:
剩余牌: [ "9" , "2" , "9" , "2" , "4" , "A" ]
九、好运留下来,烦恼丢出去
好运留下来:靠前张放到最下面(数组滚动一下)
烦恼丢出去:靠前张扔出去(删除靠前个数组内容)
由于男女生剩的牌数不一样,所以用个do循环,直到手里只剩1张牌为止。
Do If Len (Rnd_8_card) = 1 Then Exit Do End If Rnd_8_card = Rotate(Rnd_8_card, 1 ) Rnd_8_card = zm.ArrayRemove(Rnd_8_card, 0 ) Loop TracePrint "剩余牌:" ,encode.TableToJson(Rnd_8_card)
输出结果:
剩余牌: [ "A" ]
这里看到最后剩余的是A,和之前藏起来的牌一样都是A。
完整代码:
Import "zm.luae" zm.Init Dim cards = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"} //Dim Rnd_4_card = Rnd_Sample(cards,4) Dim Rnd_4_card = {"9","2","A","4"} TracePrint "打乱扑克牌:",encode.TableToJson(Rnd_4_card) Dim Rnd_8_card = Multiple(Rnd_4_card,2) TracePrint "撕开后的8张扑克牌:",encode.TableToJson(Rnd_8_card) Dim name = "郭立员" Dim name_num = utf8.len(name) TracePrint "我的名字字数是:"&name_num Rnd_8_card = Rotate(Rnd_8_card,name_num) TracePrint "新的8张扑克牌:",encode.TableToJson(Rnd_8_card) Rnd_8_card = insert(Rnd_8_card,3) TracePrint "新的8张扑克牌:",encode.TableToJson(Rnd_8_card) Dim firs_card = Rnd_8_card[1] TracePrint "藏起来1张牌:",firs_card Rnd_8_card = zm.ArrayRemove(Rnd_8_card, 0) TracePrint "剩余7张牌:",encode.TableToJson(Rnd_8_card) Dim area = "1" // 北方 1,南方 2,不确定 3 Rnd_8_card = insert(Rnd_8_card, area) TracePrint "剩余7张牌:",encode.TableToJson(Rnd_8_card) Dim gender = "1" // 男生 1 , 女生 2 Rnd_8_card = zm.ArrayRemove(Rnd_8_card, gender-1) TracePrint "剩余牌:",encode.TableToJson(Rnd_8_card) Dim 口令 = "见证奇迹的时刻" Dim code_num = utf8.len(口令) Rnd_8_card = Rotate(Rnd_8_card,code_num) TracePrint "剩余牌:",encode.TableToJson(Rnd_8_card) Do If Len(Rnd_8_card) = 1 Then Exit Do End If Rnd_8_card = Rotate(Rnd_8_card, 1) Rnd_8_card = zm.ArrayRemove(Rnd_8_card, 0) Loop TracePrint "剩余牌:",encode.TableToJson(Rnd_8_card) Function insert(cards,n) Dim arr = {null} For i = 1 To len(cards)-n arr[i] = cards[i + n] Next TracePrint "后面张扑克牌:",encode.TableToJson(arr) Dim position = int((len(cards)-(n+1))*rnd() + 1) TracePrint "**位置:",position For i = n To 1 step -1 arr = zm.ArrayInsert (arr,cards[i],position) Next insert = arr End Function Function Rotate(cards,n) Dim arr = {null} Dim num = n mod Len(cards) For i = 1 To Len(cards) If i + num Len(cards) Then arr[i] = cards[i + num - Len(cards)] Else arr[i] = cards[i + num] End If Next Rotate = arr End Function Function Multiple(cards,n) Dim arr = {null} For j = 1 To n For i = 1 To Len(cards) arr[Len(cards)*(j-1)+i] = cards[i] Next Next Multiple = arr End Function Function Rnd_Sample(cards,n) Dim arr = {null} Dim new_cards = Rnd_Shuffle(cards) For i = 1 To n arr[i] = new_cards[i] Next Rnd_Sample = arr End Function Function Rnd_Shuffle(cards) Dim container For i = 1 To UBOUND(cards) dim Rnd_num = Int(Len(cards)*rnd() + 1) container = cards[i] cards[i] = cards[Rnd_num] cards[Rnd_num] = container Next Rnd_shuffle = cards End Function