import random as r
import pickle
admin_energy = 1 # 我方初始能量
ai_energy = 1 # 敌方初始能量
admin_action = "" # 我方行动
def player_action():
global admin_action, admin_energy, ai_energy
print("这是你的回合")
print("你的运有%s" % admin_energy)
while True:
admin_action = input('请输入您的手势')
if admin_action == '运':
admin_energy += 1
break
elif admin_action == '攻':
if admin_energy >= 1:
admin_energy -= 1
break
else:
print('能量不足,请重新输入')
elif admin_action == '防':
break
elif admin_action == '小防':
break
elif admin_action == '小波':
if admin_energy >= 3:
admin_energy -= 3
break
else:
print('能量不足,请重新输入')
elif admin_action == '大防':
if admin_energy >= 1:
admin_energy -= 1
break
else:
print('能量不足,请重新输入')
elif admin_action == '大波':
if admin_energy >= 5:
admin_energy -= 5
break
else:
print('能量不足,请重新输入')
else:
print('输入错误请重新输入')
def Pc_action(string):
action = ["运", "攻", "防", "小波", "大波", "大防"]
weight = Ai_Get_Data(string) # 从“人工智能获取数据”函数里,得到权重的值
print("Ai_action已运行!action为")
temp = r.choices(action, weights=weight)
print(temp)
return temp
def Ai_Get_Data(string): # 传参string,如果人类和AI运都为1,则传OneOne,如果人为1 AI为2,则传OneTwo,以此类推
file = open(r"AiLearn.txt", 'rb')
print("打开文件成功")
ai_data = []
print("正在获取Ai数据……")
print(getData)
if string == "OneOne": # 检测输入字符串是否为标识查找符,3运以上全归为三运
OneOne_ai_data = pickle.load(file)
ai_data = OneOne_ai_data
elif string == "OneTwo":
OneTwo_ai_data = pickle.load(file)
ai_data = OneTwo_ai_data
elif string == "OneThree":
OneThree_ai_data = pickle.load(file)
ai_data = OneThree_ai_data
elif string == "TwoOne":
TwoOne_ai_data = pickle.load(file)
ai_data = TwoOne_ai_data
elif string == "TwoTwo":
TwoTwo_ai_data = pickle.load(file)
ai_data = TwoTwo_ai_data
elif string == "TwoThree":
TwoThree_ai_data = pickle.load(file)
ai_data = TwoThree_ai_data
elif string == "ThreeOne":
ThreeOne_ai_data = pickle.load(file)
ai_data = ThreeOne_ai_data
elif string == "ThreeTwo":
ThreeTwo_ai_data = pickle.load(file)
ai_data = ThreeTwo_ai_data
elif string == "ThreeThree":
ThreeThree_ai_data = pickle.load(file)
ai_data = ThreeThree_ai_data
file.close()
print("获取的ai的data为")
print(ai_data)
return ai_data
''' # 弃用该方法
ai_data = []
f = open(r"AiLearn.txt")
for each in f.readline(): # 遍历所有权重的值
if len(string) == 6:
if each[:6] == string: # 如果该标识查找符在该行
each = each.replace(string, '') # 去除标识查找符
ai_data = ai_data + each.split(',') # 以逗号分开,将数据变成列表
elif len(string) == 8:
if each[:8] == string:
each = each.replace(string, '') # 去除标识查找符
ai_data = ai_data + each.split(',') # 以逗号分开,将数据变成列表
elif len(string) == 10:
if each[:10] == string:
each = each.replace(string, '') # 去除标识查找符
ai_data = ai_data + each.split(',') # 以逗号分开,将数据变成列表
'''
def Ai_Learn(ai_action_number):
file = open(r"AiLearn.txt", "rb")
OneOne_ai_data = pickle.load(file)
OneTwo_ai_data = pickle.load(file)
OneThree_ai_data = pickle.load(file)
TwoOne_ai_data = pickle.load(file)
TwoTwo_ai_data = pickle.load(file)
TwoThree_ai_data = pickle.load(file)
ThreeOne_ai_data = pickle.load(file)
ThreeTwo_ai_data = pickle.load(file)
ThreeThree_ai_data = pickle.load(file)
print("Learn函数中已加载Data")
file.close()
file = open(r"AiLearn.txt", 'ab')
if getData == "OneOne": # 检测输入字符串是否为标识查找符,3运以上全归为三运
OneOne_ai_data[ai_action_number] = OneOne_ai_data[ai_action_number] + 1 # 每个number代表action在该数组的索引值
pickle.dump(OneOne_ai_data, file)
elif getData == "OneTwo":
OneTwo_ai_data[ai_action_number] = OneTwo_ai_data[ai_action_number] + 1 # 每个number代表action在该数组的索引值
pickle.dump(OneTwo_ai_data, file)
elif getData == "OneThree":
OneThree_ai_data[ai_action_number] = OneThree_ai_data[ai_action_number] + 1 # 每个number代表action在该数组的索引值
pickle.dump(OneThree_ai_data, file)
elif getData == "TwoOne":
TwoOne_ai_data[ai_action_number] = TwoOne_ai_data[ai_action_number] + 1 # 每个number代表action在该数组的索引值
pickle.dump(TwoOne_ai_data, file)
elif getData == "TwoTwo":
TwoTwo_ai_data[ai_action_number] = TwoTwo_ai_data[ai_action_number] + 1 # 每个number代表action在该数组的索引值
pickle.dump(TwoTwo_ai_data, file)
elif getData == "TwoThree":
TwoThree_ai_data[ai_action_number] = TwoThree_ai_data[ai_action_number] + 1 # 每个number代表action在该数组的索引值
pickle.dump(TwoThree_ai_data, file)
elif getData == "ThreeOne":
ThreeOne_ai_data[ai_action_number] = ThreeOne_ai_data[ai_action_number] + 1 # 每个number代表action在该数组的索引值
pickle.dump(ThreeOne_ai_data, file)
elif getData == "ThreeTwo":
ThreeTwo_ai_data[ai_action_number] = ThreeTwo_ai_data[ai_action_number] + 1 # 每个number代表action在该数组的索引值
pickle.dump(ThreeTwo_ai_data, file)
elif getData == "ThreeThree":
ThreeThree_ai_data[ai_action_number] = ThreeThree_ai_data[ai_action_number] + 1 # 每个number代表action在该数组的索引值
pickle.dump(ThreeThree_ai_data, file)
file.close()
''' # 弃用该方法
ai_alldata = file.read()
ai_data_update[ai_action_number] = ai_data[ai_action_number] + 1 # 每个number代表action在该数组的索引值
for each in ai_alldata:
if getData in each:
ai_alldata.replace("%s%s" % (getdata, aidata), "%s%s" % (getdata, ai_data_update))
file.close()
'''
def stringTranslation(num):
trans = ""
if num <= 1:
trans = "One"
elif num == 2:
trans = "Two"
elif num >= 3:
trans = "Three"
return trans
def DelE():
global admin_energy, ai_energy
admin_energy = 0
ai_energy = 0
def compete(pc_action):
if admin_action == "运" and pc_action == ["运"]: # action = ["运", "攻", "防", "小波", "大波", "大防"]
Ai_Learn(0)
print("平")
elif admin_action == "运" and pc_action == ["攻"]:
Ai_Learn(1)
print("Ai胜")
DelE()
elif admin_action == "运" and pc_action == ["防"]:
Ai_Learn(2)
print("平")
elif admin_action == "运" and pc_action == ["小波"]:
Ai_Learn(3)
print("Ai胜")
DelE()
elif admin_action == "运" and pc_action == ["大波"]:
Ai_Learn(4)
print("Ai胜")
DelE()
elif admin_action == "运" and pc_action == ["大防"]:
Ai_Learn(5)
print("平")
elif admin_action == "攻" and pc_action == ["运"]: # action = ["运", "攻", "防", "小波", "大波", "大防"]
print("人类胜利")
DelE()
elif admin_action == "攻" and pc_action == ["攻"]:
Ai_Learn(1)
print("平")
elif admin_action == "攻" and pc_action == ["防"]:
Ai_Learn(2)
print("平")
elif admin_action == "攻" and pc_action == ["小波"]:
Ai_Learn(3)
print("平")
elif admin_action == "攻" and pc_action == ["大波"]:
Ai_Learn(4)
print("平")
elif admin_action == "攻" and pc_action == ["大防"]:
Ai_Learn(5)
print("平")
elif admin_action == "防" and pc_action == ["运"]: # action = ["运", "攻", "防", "小波", "大波", "大防"]
Ai_Learn(0)
print("平")
elif admin_action == "防" and pc_action == ["攻"]:
Ai_Learn(1)
print("平")
elif admin_action == "防" and pc_action == ["防"]:
Ai_Learn(2)
print("平")
elif admin_action == "防" and pc_action == ["小波"]:
Ai_Learn(3)
print("Ai胜利")
DelE()
elif admin_action == "防" and pc_action == ["大波"]:
Ai_Learn(4)
print("Ai胜利")
DelE()
elif admin_action == "防" and pc_action == ["大防"]:
Ai_Learn(5)
print("平")
elif admin_action == "小波" and pc_action == ["运"]: # action = ["运", "攻", "防", "小波", "大波", "大防"]
print("人类胜利")
DelE()
elif admin_action == "小波" and pc_action == ["攻"]:
Ai_Learn(1)
print("平")
elif admin_action == "小波" and pc_action == ["防"]:
print("人类胜利")
DelE()
elif admin_action == "小波" and pc_action == ["小波"]:
Ai_Learn(3)
print("平")
elif admin_action == "小波" and pc_action == ["大波"]:
Ai_Learn(4)
print("平")
elif admin_action == "小波" and pc_action == ["大防"]:
Ai_Learn(5)
print("平")
elif admin_action == "大波" and pc_action == ["运"]: # action = ["运", "攻", "防", "小波", "大波", "大防"]
print("人类胜利")
DelE()
elif admin_action == "大波" and pc_action == ["攻"]:
Ai_Learn(1)
print("平")
elif admin_action == "大波" and pc_action == ["防"]:
print("人类胜利")
DelE()
elif admin_action == "大波" and pc_action == ["小波"]:
Ai_Learn(3)
print("平")
elif admin_action == "大波" and pc_action == ["大波"]:
Ai_Learn(4)
print("平")
elif admin_action == "大波" and pc_action == ["大防"]:
print("人类胜利")
DelE()
elif admin_action == "大防" and pc_action == ["运"]: # action = ["运", "攻", "防", "小波", "大波", "大防"]
Ai_Learn(0)
print("平")
elif admin_action == "大防" and pc_action == ["攻"]:
Ai_Learn(1)
print("平")
elif admin_action == "大防" and pc_action == ["防"]:
Ai_Learn(2)
print("平")
elif admin_action == "大防" and pc_action == ["小波"]:
Ai_Learn(3)
print("平")
elif admin_action == "大防" and pc_action == ["大波"]:
Ai_Learn(4)
print("平")
elif admin_action == "大防" and pc_action == ["大防"]:
Ai_Learn(5)
print("平")
def ai_energy_count():
global ai_energy, ai_action
print("Ai的之前的运为")
print(ai_energy)
while True:
if ai_action == ['运']:
ai_energy += 1
break
elif ai_action == ['攻']:
if ai_energy >= 1:
ai_energy -= 1
break
else:
ai_action = Pc_action(getData)
print("test1")
elif ai_action == ['防']:
break
elif ai_action == ['小防']:
break
elif ai_action == ['小波']:
if ai_energy >= 3:
ai_energy -= 3
break
else:
ai_action = Pc_action(getData)
print("test2")
elif ai_action == ['大防']:
if ai_energy >= 1:
ai_energy -= 1
break
else:
ai_action = Pc_action(getData)
print("test3")
elif ai_action == ['大波']:
if ai_energy >= 5:
ai_energy -= 5
break
else:
ai_action = Pc_action(getData)
print("test4")
print("Ai的之后的运为")
print(ai_energy)
while 1:
player_action()
str1 = stringTranslation(admin_energy)
str2 = stringTranslation(ai_energy)
getData = "%s%s" % (str1, str2)
if ai_energy == 0:
ai_action = ["运"]
elif ai_energy > 0 and admin_energy == 0:
choicelist = ["运", "攻", "小波", "大波"]
ai_action = r.choices(choicelist)
else:
ai_action = Pc_action(getData)
ai_energy_count()
print("Ai做出的手势为:%s" % ai_action)
compete(ai_action)