# Klondike v1.0, by Jeffry Johnston
from casioplot import *
from random import *
d=int;c=input;b=divmod;I=False;H=True;F=print;D=range;A=len
S=b"e5Ee5Ee5"
T=b"#"
U=(b">k",b"kk",b"7JBB##	",b"	#BB#	")
V=(b"  Aw",b"EBBA ",b"EBE	",b"0Pa",b"?yE	",b"Dp!D",b"?EAAA",
b"Eq#E	",b"DaD",b"2)R$I",b";#C$E	",b"DbE>",b"w$(p BH;")
def K(C,w,x,y):
	F=C[0]-1;B=0
	for G in D(1,A(C)):
		E=(C[G]-F)%128
		for _ in D(7):
			H=E&1;E>>=1
			if H:set_pixel(x+B,y)
			B+=1
			if B>=w:B=0;y+=1
def G(x,y,A):
	x=18*x+1
	if A==-2:K(T,17,x,y);return
	K(S,17,x,y)
	if A!=-1:B,C=b(A,13);K(V[C],8,x+1,y+2);K(U[B],7,x+5,y+10)
def W():
	clear_screen()
	if A(C)>0:G(0,0,C[0])
	for B in D(4):
		if J[B]>=0:G(B+3,0,J[B])
	for B in D(7):
		F=N[B]
		if F:G(B,21,-2)
		if F>1:
			for I in D(0,F):set_pixel(18*B+15-2*I,22)
		E=O[B]
		if A(E)>0:G(B,23,E[0])
		if A(E)>1:G(B,44,E[-1])
	show_screen()
	try:
		while H:0
	except KeyboardInterrupt:pass
def help():
	F('0=Turn card to waste\n8=Auto foundations\n0,#=From waste\n1-7,#=From pile\n#,1-7=To pile\n#,8=To foundation')
	c('Press [EXE]')
def X():
	B=c('From,To=')
	if A(B)<1:return-1,-1
	if B=='"':help();return-1,-1
	C=B.split(',',1)
	try:
		D=d(C[0])
		if A(C)==1:return D,-1
		E=d(C[1])
	except:return-1,-1
	if not(-1<D<8 and 0<E<9):return-1,-1
	return D,E
def P(F,K):
	if F==0:D=C[0]
	else:
		B=O[F-1]
		if A(B)<1:return I
		D=B[-1]
	L,G=b(D,13)
	if K==8:
		if J[L]<0:
			if G!=0:return I
		elif G!=J[L]%13+1:return I
		J[L]=D
	else:
		E=O[K-1]
		if A(E)>0:P=E[-1];Q=P//26;R=P%13
		if A(E)<1 and G==12 or A(E)>0 and G==R-1 and Q!=D//26:M=I
		elif F==0 or A(B)<2:return I
		else:
			D=B[0];G=D%13
			if A(E)<1 and G==12 or A(E)>0 and G==R-1 and Q!=D//26:M=H
			else:return I
		if F!=0 and M:E.extend(B);B.clear()
		else:E.append(D)
	if F==0:C.pop(0);return H
	if K==8 or not M:B.pop()
	if A(B)<1 and N[F-1]>0:N[F-1]-=1;B.append(Y.pop())
	return H
B=[0]*52
for E in D(52):
	M=randint(0,E)
	if E!=M:B[E]=B[M]
	B[M]=E
J=[-1]*4;N=list(D(7));C=B[0:24];Y=B[24:45];O=[[A]for A in B[45:52]]
F('Type " for help')
while H:
	W();L,Q=X()
	if L==-1:continue
	if Q==-1:
		if L==0:
			if A(C)>0:Z=C[-1];C.pop();C.insert(0,Z)
		elif L==8:
			while H:
				R=I
				for E in D(1,8):
					a=P(E,8)
					if a:R=H
				if not R:break
	else:P(L,Q)