# cmd: C:\python.lnk D:\simulator.py > D:\1.txt
# cmd Ctrl+C 退出当前程序
class node(object):
def _init(self, val = 0, prev = 0, next = 0):
self.data = val
self.prev = prev
self.next = next
def init():
global obj
obj = [0]
for i in range(1,50):
obj.append(0)
for i in range(1,40+1):
obj[i] = node(); obj[i]._init()
for i in range(1,40+1):
obj[i].prev = obj[i-1]
for i in range(1,40+1):
obj[i].next = obj[i+1]
obj[1].prev = obj[40]
obj[40].next = obj[1]
q_h = obj[1]
q_t = obj[40]
return(q_h, q_t)
# shuffle cards
def shuffle_cards():
global handcard_head, handcard_tail
cards = [0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10]
import random
for i in range(1,40+1):
j = random.randint(i,40)
x = cards[i];
cards[i] = cards[j];
cards[j] = x
for i in range(1,40+1):
handcard_head.data = cards[i]
handcard_head = handcard_head.next
debug_here = 0
if debug_here ==1:
debug(handcard_head, handcard_tail, 'handcard after shuffling')
print('\n')
# 模拟时使用的函数
def func(q_h, q_t):
global handcard_head, handcard_tail
if(q_h.prev != q_t):
# 发一张牌
q_t = q_t.next
q_t.data = handcard_head.data
handcard_head.data = 0
handcard_head = handcard_head.next
# check是否能拾起三张牌
while(1):
if(q_h.prev == q_t):
return(q_h, q_t)
if(q_h == q_t):
return(q_h, q_t)
if(q_h.next == q_t):
return(q_h, q_t)
x1 = q_h.data
x2 = q_h.next.data
x3 = q_t.prev.prev.data
x4 = q_t.prev.data
x5 = q_t.data
if(x1+x2+x5==9 or x1+x2+x5==19 or x1+x2+x5==29):
q_h.data = q_h.next.data = q_t.data = 0
q_h = q_h.next.next
q_t = q_t.prev
handcard_tail = handcard_tail.next
handcard_tail.data = x5
handcard_tail = handcard_tail.next
handcard_tail.data = x2
handcard_tail = handcard_tail.next
handcard_tail.data = x1
elif(x1+x4+x5==9 or x1+x4+x5==19 or x1+x4+x5==29):
q_h.data = q_t.data = q_t.prev.data = 0
q_h = q_h.next
q_t = q_t.prev.prev
handcard_tail = handcard_tail.next
handcard_tail.data = x5
handcard_tail = handcard_tail.next
handcard_tail.data = x4
handcard_tail = handcard_tail.next
handcard_tail.data = x1
elif(x3+x4+x5==9 or x3+x4+x5==19 or x3+x4+x5==29):
q_t.data = q_t.prev.data = q_t.prev.prev.data = 0
q_t = q_t.prev.prev.prev
handcard_tail = handcard_tail.next
handcard_tail.data = x5
handcard_tail = handcard_tail.next
handcard_tail.data = x4
handcard_tail = handcard_tail.next
handcard_tail.data = x3
else:
return(q_h, q_t)
return(q_h, q_t)
def func_before_while(q_t, h_h):
q_t = q_t.next
q_t.data = h_h.data
h_h.data = 0
h_h = h_h.next
return(q_t, h_h)
def simulator():
global handcard_head, handcard_tail, queue1_head, queue1_tail, queue2_head, queue2_tail
global queue3_head, queue3_tail, queue4_head, queue4_tail
shuffle_cards()
#debug(handcard_head, handcard_tail, 'handcard')
#debug(queue1_head, queue1_tail, 'q1')
#start simulation
queue1_tail, handcard_head = func_before_while(queue1_tail, handcard_head)
queue2_tail, handcard_head = func_before_while(queue2_tail, handcard_head)
queue3_tail, handcard_head = func_before_while(queue3_tail, handcard_head)# 如果只摆两列注释掉这两行
queue4_tail, handcard_head = func_before_while(queue4_tail, handcard_head)#
i = 0;
while(1):
i = i + 1
if i > 10000:
return 0
queue1_head, queue1_tail = func(queue1_head, queue1_tail)
if(handcard_head.prev == handcard_tail):
if(handcard_head.data > 0):
return 1
elif(handcard_head.data == 0):
return 0
if(handcard_head.prev.prev == handcard_tail and handcard_head.data > 0):
return 1
queue2_head, queue2_tail = func(queue2_head, queue2_tail)
if(handcard_head.prev == handcard_tail):
if(handcard_head.data > 0):
return 1
elif(handcard_head.data == 0):
return 0
if(handcard_head.prev.prev == handcard_tail and handcard_head.data > 0):
return 1
queue3_head, queue3_tail = func(queue3_head, queue3_tail)
if(handcard_head.prev == handcard_tail):
if(handcard_head.data > 0):
return 1
elif(handcard_head.data == 0):
return 0
if(handcard_head.prev.prev == handcard_tail and handcard_head.data > 0):
return 1
queue4_head, queue4_tail = func(queue4_head, queue4_tail)
if(handcard_head.prev == handcard_tail):
if(handcard_head.data > 0):
return 1
elif(handcard_head.data == 0):
return 0
if(handcard_head.prev.prev == handcard_tail and handcard_head.data > 0):
return 1
def debug(q_h, q_t, str):
print('\n'+str)
for i in range(1,40+1):
print(q_h.data, end = ',')
q_h = q_h.next
print(q_h.prev == q_t, end = ',')
print(q_t.next == q_h, end = ',')
N = 100; M = 0;
handcard_head, handcard_tail = init()
queue1_head, queue1_tail = init()
queue2_head, queue2_tail = init()
queue3_head, queue3_tail = init()
queue4_head, queue4_tail = init()
for i in range(1,N+1):
for j in range(1,40+1):
queue1_head.data = 0; queue1_head = queue1_head.next
queue2_head.data = 0; queue2_head = queue2_head.next
queue3_head.data = 0; queue3_head = queue3_head.next
queue4_head.data = 0; queue4_head = queue4_head.next
handcard_head.data = 0; handcard_head = handcard_head.next
queue1_tail = queue1_head.prev
queue2_tail = queue2_head.prev
queue3_tail = queue3_head.prev
queue4_tail = queue4_head.prev
handcard_tail = handcard_head.prev
if simulator() == 1:
M = M + 1
print('success')
print(M/N)
debug(handcard_head, handcard_tail, 'handcard in the end')
debug(queue1_head, queue1_tail, 'queue1 in the end')
debug(queue2_head, queue2_tail, 'queue2 in the end')
debug(queue3_head, queue3_tail, 'queue3 in the end')
debug(queue4_head, queue4_tail, 'queue4 in the end')
input()
09-07