田忌赛马模拟代码分享
前几天写了个田忌赛马模拟田忌赛马.pydef printplus(value):
if None in value:
return None
else:
print(value)
def change(Tuple):
dict1, dict2 = Tuple
for key1 in dict1:
value1 = dict1
print(f"{key1}了{value1}场", end=" ")
print("\n")
dict3 = {0: "下等马", 1: "中等马", 2: "上等马"}
for key2 in dict2:
Len = len(key2)
if Len == 2:
print("齐威王出"+dict3]+",田忌出"+dict3]+",结果为"+dict2)
elif Len == 4:
print("齐威王出"+dict3]+",田忌出"+dict3]+",结果为"+dict2+"(齐威王第一轮出"+dict3]+";田忌第一轮出"+dict3]+")")
elif Len == 6:
print("齐威王出"+dict3]+",田忌出"+dict3]+",结果为"+dict2+"(齐威王第一轮出"+dict3]+",第二轮出"+dict3]+";田忌第一轮出"+dict3]+",第二轮出"+dict3]+")")
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 = "输"
elif QiWeiWang < TianJi:
Sheng += 1
results = "胜"
else:
Ping += 1
results = "平"
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
Ping = 0
Sheng = 0
Shu = 0
results = {}
for key in First1:
for QiWeiWang in range(3):
for TianJi in range(3):
if QiWeiWang == key or TianJi == key:
continue
key1, key2 = key
result = (key1, key2 , QiWeiWang, TianJi)
if QiWeiWang > TianJi:
Shu += 1
results = "输"
elif QiWeiWang < TianJi:
Sheng += 1
results = "胜"
else:
Ping += 1
results = "平"
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
Ping = 0
Sheng = 0
Shu = 0
results = {}
for key in Second1:
for QiWeiWang in range(3):
for TianJi in range(3):
if QiWeiWang == key or QiWeiWang == key or TianJi == keyor TianJi == key:
continue
key3, key4, key5, key6 = key
result = (key3, key4, QiWeiWang, TianJi, key5, key6)
if QiWeiWang > TianJi:
Shu += 1
results = "输"
elif QiWeiWang < TianJi:
Sheng += 1
results = "胜"
else:
Ping += 1
results = "平"
Return = ({"胜": Sheng, "输": Shu, "平": Ping}, results)
if v == 0:
return Return
elif v == 1:
change(Return)
return Return + (None,)
田忌赛马test.py
from 田忌赛马 import *
First = first(1)
printplus(First)
Second = second(First, 1)
printplus(Second)
Third = third(Second, 1)
printplus(Third) 6
页:
[1]