仿『斗牛』休闲游戏编写的python代码版斗牛
如题,就是根据斗牛游戏规则编写的代码, 可以手动设置玩家个数(最大不超过7个).代码一运行即可知道玩家的牌,且自动计算是否有牛。
庄家的牌需按下回车,(增加趣味性... )
自主编写的,如果有大神能指点下,有更好的代码的逻辑,盼回复~~
代码如下 :
import random
puke =
def niuniu():
x = []
first = random.choice(puke)
x.append(first)
puke.pop(puke.index(first))
second = random.choice(puke)
x.append(second)
puke.pop(puke.index(second))
thrid = random.choice(puke)
x.append(thrid)
puke.pop(puke.index(thrid))
fourth = random.choice(puke)
x.append(fourth)
puke.pop(puke.index(fourth))
fifth = random.choice(puke)
x.append(fifth)
puke.pop(puke.index(fifth))
return x
def conversion(xlist):
'''
将花牌和大小王转换成数字10
'''
l = []
for i in xlist:
if i in ["J", "Q", "K", "小王", "大王"]:
l.append(10)
elif i == "A":
l.append(1)
else:
l.append(i)
return l
def compute(xlist):
if (xlist+xlist+xlist)%10 == 0 or (xlist+xlist+xlist)%10 == 0 or (xlist+xlist+xlist)%10 == 0 or (xlist+xlist+xlist)%10 == 0 or (xlist+xlist+xlist)%10 == 0 or (xlist+xlist+xlist)%10 == 0 or (xlist+xlist+xlist)%10 == 0 or (xlist+xlist+xlist)%10 == 0 or (xlist+xlist+xlist)%10 == 0 or (xlist+xlist+xlist)%10 == 0 :
total = xlist+xlist+xlist+xlist+xlist
result = int(str(total)[-1])
if result == 0:
return 0 # 牛牛
else:
return result
return -1 # 无牛
def format_print(x):
if len(str(x)) > 1:
return str(x)
return " " + str(x)
def players(x):
di = {1 : "一", 2 : "二", 3 : "三", 4 : "四", 5 : "五", 6 : "六", 7 : "七"}
for i in range(x):
player_puke = niuniu()
player = conversion(player_puke)
player_puke = f"{format_print(player_puke)}{format_print(player_puke)}{format_print(player_puke)}{format_print(player_puke)}{format_print(player_puke)}"
if compute(player) != -1:
if compute(player) != 0:
print(f"玩家{di}: {player_puke}--恭喜牛{compute(player)}")
else:
print(f"玩家{di}: {player_puke}--恭喜 『牛牛』")
else:
print(f"玩家{di}: {player_puke}--哈哈~ 无牛")
def zhuang():
zhuang_puke = niuniu()
player = conversion(zhuang_puke)
zhuang_puke = f"{format_print(zhuang_puke)}{format_print(zhuang_puke)}{format_print(zhuang_puke)}{format_print(zhuang_puke)}{format_print(zhuang_puke)}"
if compute(player) != -1:
if compute(player) != 0:
print(f"庄 家: {zhuang_puke}--恭喜牛{compute(player)}")
else:
print(f"庄 家: {zhuang_puke}--恭喜 『牛牛』")
else:
print(f"庄 家: {zhuang_puke}--哈哈~ 无牛")
if __name__ == '__main__':
players(3)
input("按下回车键开庄...")
zhuang()
运行效果图:
手机端: 使用 QPython
yanshiqi 发表于 2022-1-29 20:10
用while, 需要把 puke这个列表,放在while里面第一行。报错的原因是, 几次循环后, 54张牌用完了,就 ...
if __name__ == '__main__':
while True:
puke = [3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A", 2, 3, 4, 5,
6, 7, 8, 9, 10, "J", "Q", "K", "A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A", 2, "小王", "大王"]
players(3)
input("按下回车键开庄...")
zhuang()
print("-"*46)
inp = input("请问是否继续游戏? (y/n) ")
if inp == "n" or inp == "N":
break
elif inp == "y" or inp == "Y":
pass
else:
print("输入有误,请重新开始游戏")
break
print("\n\n")
将原代码 第二行删掉, 将 if __name__ == '__main__': 以下代码 改成上面的代码,即可无限循环 本帖最后由 liuxiaoxin 于 2022-1-29 21:10 编辑
yanshiqi 发表于 2022-1-29 20:22
if __name__ == '__main__':
while True:
puke =
if __name__ == '__main__':
while True:
puke = [3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A", 2, 3, 4, 5, 6, 7, 8, 9, 10,
"J", "Q", "K", "A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A", 2, "小王", "大王"]
players(3)
input("按下回车键开庄...")
zhuang()
print("")
我直接这样整了{:1_918:} 按回车一直继续{:301_998:} 其实我就好奇什么是斗牛 我也好奇啥是斗牛{:1_926:} w92vv 发表于 2022-1-29 16:08
其实我就好奇什么是斗牛
大小王、花牌算10, A算 1. 各得五张牌, 其中任三张相加,得10或10的倍数即为有牛(凑不出即无牛),再看个位数 1-9,即牛1-9, 若个位0,就是牛牛 不错不错{:1_893:}感谢分享 咱也不可能说只玩一把加个循环更好吧 lz那个手机端是什么软件 感谢分享,给力支持{:301_993:} 感谢大佬分享,可以尝试一下 yanshiqi 发表于 2022-1-29 16:18
大小王、花牌算10, A算 1. 各得五张牌, 其中任三张相加,得10或10的倍数即为有牛(凑不出即无牛), ...
他的意思可能是这个玩法叫“牛牛”。而不是“斗牛”