马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
前几天写了个田忌赛马模拟
田忌赛马.pydef printplus(value):
if None in value:
return None
else:
print(value)
def change(Tuple):
dict1, dict2 = Tuple
for key1 in dict1:
value1 = dict1[key1]
print(f"{key1}了{value1}场", end=" ")
print("\n")
dict3 = {0: "下等马", 1: "中等马", 2: "上等马"}
for key2 in dict2:
Len = len(key2)
if Len == 2:
print("齐威王出"+dict3[key2[0]]+",田忌出"+dict3[key2[0]]+",结果为"+dict2[key2])
elif Len == 4:
print("齐威王出"+dict3[key2[2]]+",田忌出"+dict3[key2[3]]+",结果为"+dict2[key2]+"(齐威王第一轮出"+dict3[key2[0]]+";田忌第一轮出"+dict3[key2[1]]+")")
elif Len == 6:
print("齐威王出"+dict3[key2[2]]+",田忌出"+dict3[key2[3]]+",结果为"+dict2[key2]+"(齐威王第一轮出"+dict3[key2[0]]+",第二轮出"+dict3[key2[4]]+";田忌第一轮出"+dict3[key2[1]]+",第二轮出"+dict3[key2[5]]+")")
print("\n")
def first(v=0):
'''
返回田忌赛马第一轮的排列组合
By Tiger
'''
Ping = 0
Sheng = 0
Shu = 0
results = {}
for QiWeiWang in range(3):
for TianJi in range(3):
result = (QiWeiWang, TianJi)
if QiWeiWang > TianJi:
Shu += 1
results[result] = "输"
elif QiWeiWang < TianJi:
Sheng += 1
results[result] = "胜"
else:
Ping += 1
results[result] = "平"
Return = ({"胜": Sheng, "输": Shu, "平": Ping}, results)
if v == 0:
return Return
elif v == 1:
change(Return)
return Return + (None,)
def second(First, v=0):
First1 = First[1]
Ping = 0
Sheng = 0
Shu = 0
results = {}
for key in First1:
for QiWeiWang in range(3):
for TianJi in range(3):
if QiWeiWang == key[0] or TianJi == key[1]:
continue
key1, key2 = key
result = (key1, key2 , QiWeiWang, TianJi)
if QiWeiWang > TianJi:
Shu += 1
results[result] = "输"
elif QiWeiWang < TianJi:
Sheng += 1
results[result] = "胜"
else:
Ping += 1
results[result] = "平"
Return = ({"胜": Sheng, "输": Shu, "平": Ping}, results)
if v == 0:
return Return
elif v == 1:
change(Return)
return Return + (None,)
def third(Second, v=0):
Second1 = Second[1]
Ping = 0
Sheng = 0
Shu = 0
results = {}
for key in Second1:
for QiWeiWang in range(3):
for TianJi in range(3):
if QiWeiWang == key[2] or QiWeiWang == key[0] or TianJi == key[1]or TianJi == key[3]:
continue
key3, key4, key5, key6 = key
result = (key3, key4, QiWeiWang, TianJi, key5, key6)
if QiWeiWang > TianJi:
Shu += 1
results[result] = "输"
elif QiWeiWang < TianJi:
Sheng += 1
results[result] = "胜"
else:
Ping += 1
results[result] = "平"
Return = ({"胜": Sheng, "输": Shu, "平": Ping}, results)
if v == 0:
return Return
elif v == 1:
change(Return)
return Return + (None,)
田忌赛马test.pyfrom 田忌赛马 import *
First = first(1)
printplus(First)
Second = second(First, 1)
printplus(Second)
Third = third(Second, 1)
printplus(Third)
|