小猫钓鱼纸牌游戏 python

源代码:

import random

ranks=["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
suits=["梅","方","红","黑"]

pp=[]
pai=[]






def shengcheng(pai,ranks,suits):
	for i in ranks:
		if i=="A":
			i=1
		elif i=="J":
			i=11
		elif i=="Q":
			i=12
		elif i=="K":
			i=13
		else:
			i=int(i)
		for j in suits:
			if j=="梅":
				pai.append(i)
			if j=="方":
				pai.append(i+13)
			if j=="红":
				pai.append(i+26)
			if j=="黑":
				pai.append(i+39)





def chupai(mm,hh):
	def yanse(hh):
		j=hh%4
		if j==1:
			return "梅"
		elif j==2:
			return "方"
		elif j==3:
			return "红"
		elif j==0:
			return "黑"
	i=hh%13
	if i==1:
		print(mm,yanse(hh)+"A")
	elif i==11:
		print(mm,yanse(hh)+"J")
	elif i==12:
		print(mm,yanse(hh)+"Q")
	elif i==0:
		print(mm,yanse(hh)+"K")
	else:
		print(mm,yanse(hh)+str(i))







shengcheng(pai,ranks,suits)
random.shuffle(pai)

pai1=pai[0:26]
pai2=pai[26:52]





for i in range(0,1000):
	if len(pai1)==0:
		print("\n玩家1牌已用尽,玩家2胜利!")
		break
	if len(pai2)==0:
		print("\n玩家2牌已用尽,玩家1胜利!")
		break
	h=pai1[i%len(pai1)]
	m=pai2[i%len(pai2)]
	#print("玩家1:"+str(h))
	chupai("玩家1",h)
	if h in pp or h+13 in pp or h+26 in pp or h+39 in pp:
		for k in range(len(pp)-1,0,-1):
			if h in pp or h+13 in pp or h+26 in pp or h+39 in pp:
				pai1.append(pp[k])
				pp.remove(pp[k])
			else:
				break
		print("玩家1成功“钓鱼”\n玩家1当前分数:"+str(len(pai1)),"玩家2当前分数"+str(len(pai2)))
	else:
		pp.append(h)
		pai1.remove(h)
	#print("玩家1分数:"+str(len(pai1)))
	
	#print("玩家2:"+str(m))
	chupai("玩家2",m)
	if m in pp or m+13 in pp or m+26 in pp or m+39 in pp:
		for k in range(len(pp)-1,0,-1):
			if m in pp or m+13 in pp or m+26 in pp or m+39 in pp:
				pai2.append(pp[k])
				pp.remove(pp[k])
			else:
				break
		print("玩家2成功“钓鱼”\n玩家2当前分数:"+str(len(pai2)),"玩家2当前分数"+str(len(pai1)))
	else:
		pp.append(m)
		pai2.remove(m)
	print("\n")
	if i>=240:
		print("Time out!")
		print("玩家1当前分数:"+str(len(pai1)),"玩家2当前分数"+str(len(pai2)))
		break
	#print("玩家2分数:"+str(len(pai2)))

备注:
有问题可以评论,看到后我会尽力及时回复的,谢谢!

你可能感兴趣的:(小猫钓鱼,小猫钓鱼,纸牌游戏,python)