代码写得很烂,有兴趣的人可以跑一跑:
# /usr/bin/env python# -*- encoding=utf-8 -*-import randomfrom itertools import permutations, combinationsclass Guess3(): def __init__(self): self.WIN = "4A0B" self.LENGTH = 4 self.POOL = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] self.record = [] self.stair = -1 self.ai_memory = list(self.__gen_all_result_set()) self.WIN_NUMBER = self.__gen_random_number() # 程序开始时调用,生成随机数 def __gen_random_number(self): result = list(self.__gen_all_result_set()) ran_value = random.randrange(len(result)) result1 = list(result[ran_value]) for i in range(len(result1)): result1[i] = str(result1[i]) return ''.join(result1) # 返回所有组合的迭代器 def __gen_all_result_set(self): return permutations(self.POOL, 4) # 转换嵌套的列表为元组 def __changeListToTuple(self, l1=[]): for i in range(len(l1)): l1[i] = tuple(l1[i]) return l1 # 转换list中字符串元素为数字元素(没有健壮性) def __str_to_int(self, list1): for i in range(len(list1)): list1[i] = eval(list1[i]) return list1 # 去除list1中的list2元素(没有健壮性) def __gen_new_list(self, list1, list2): for i in list2: list1.remove(i) return list1 # 根据输入的4位数字和答案返回对应的结果集 def gen_new_pool(self, number, index_a, index_b): pool_orgin = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] # 根据传入的Number,组成新的NUMBERlist,用于组合新的number pool_number = list(number) pool_number = self.__str_to_int(pool_number) # 结合传入的NUMBER,与0-9组成的数组取补集 pool_orgin_running = self.__gen_new_list(pool_orgin, pool_number) # 控制下标 index_model = [0, 1, 2, 3] # 参与组合返回的数字 result_null = ['', '', '', ''] # 将程序生成的数组放入result中,形成二位数组。最终返回result result = [] # 0A0B-0A4B生成的结果集与剩余的结果集进行组合时,会非法形成空集,导致Bug if index_a == 0: if index_b == 0: result_list = list(permutations(pool_orgin_running, 4)) result = result_list if index_b == 1: result1 = [] result_pack1 = list(combinations(pool_orgin_running, 3)) result_pack2 = list(combinations(pool_number, 1)) # print("result_pack1",result_pack1) # print("result_pack2", result_pack2) for i in range(len(result_pack1)): for j in range(len(result_pack2)): temp = (list(result_pack1[i]) + list(result_pack2[j])) temp_1 = list(permutations(temp, 4)) # print("temp:",temp) # print("temp_1:", temp_1) for m in range(len(temp_1)): result1.append(list(temp_1[m])) result = result1 if index_b == 2: result2 = [] result_pack1 = list(combinations(pool_orgin_running, 2)) result_pack2 = list(combinations(pool_number, 2)) # print("result_pack1",result_pack1) # print("result_pack2", result_pack2) for i in range(len(result_pack1)): for j in range(len(result_pack2)): temp = (list(result_pack1[i]) + list(result_pack2[j])) temp_1 = list(permutations(temp, 4)) # print("temp:",temp) # print("temp_1:", temp_1) for m in range(len(temp_1)): result2.append(list(temp_1[m])) result = result2 if index_b == 3: result3 = [] result_pack1 = list(combinations(pool_orgin_running, 1)) result_pack2 = list(combinations(pool_number, 3)) # print("result_pack1",result_pack1) # print("result_pack2", result_pack2) for i in range(len(result_pack1)): for j in range(len(result_pack2)): temp = (list(result_pack1[i]) + list(result_pack2[j])) temp_1 = list(permutations(temp, 4)) # print("temp:", temp) # print("temp_1:", temp_1) for m in range(len(temp_1)): result3.append(list(temp_1[m])) result = result3 if index_b == 4: result4 = [] result_pack2 = list(permutations(pool_number, 4)) for m in range(len(result_pack2)): result4.append(list(result_pack2[m])) # print(result4) result = result4 elif index_a == 1: for i in range(4): result_orgin = pool_number.copy() result1 = result_null.copy() # ['', '', '', ''] # 控制下标,因为后面要从6个元素的数组中取数字给4元素数字,所以需要控制4元素数组的下标 a1_index = index_model.copy() # [0, 1, 2, 3] first = a1_index.pop(i) result1[first] = result_orgin.pop(first) # print("result1",result1) if index_b == 0: for m in range(6): a1_index1 = a1_index.copy() second = a1_index1.pop() bl_list1 = pool_orgin_running.copy() result2 = result1.copy() result2[second] = bl_list1.pop(m) for n in range(5): a1_index2 = a1_index1.copy() third = a1_index2.pop() result3 = result2.copy() bl_list2 = bl_list1.copy() result3[third] = bl_list2.pop(n) for h in range(4): a1_index3 = a1_index2.copy() fourth = a1_index3.pop() result4 = result3.copy() bl_list3 = bl_list2.copy() result4[fourth] = bl_list3.pop(h) result.append(result4) # print(result4) if index_b == 1: result_orgin2 = result_orgin.copy() for j in range(3): index2 = a1_index.copy() second = index2.pop(j) result2 = result1.copy() result2[second] = result_orgin2.pop(2-j) # print(result2) for k in range(2): index3 = index2.copy() third = index3.pop(k) for n in range(6): result3 = result2.copy() result_orgin3 = pool_orgin_running.copy() result3[third] = result_orgin3.pop(n) # print(result3) for m in range(5): index4 = index3.copy() fourth = index4.pop() result4 = result3.copy() result_orgin4 = result_orgin3.copy() result4[fourth] = result_orgin4.pop(m) if index_b == 2: for j in range(0,3): # 控制下标,因为后面要从6个元素的数组中取数字给4元素数字,所以需要控制4元素数组的下标 a1_index2 = a1_index.copy() # [0, 1, 2] second = a1_index2.pop(j) # 这是需要填充的数组,最终组合成4元素数组 result2 = result1.copy() # 这是用于取数字的数组,从这个数组取元素给需要填充的数组 # 填充数组 result_orgin2 = result_orgin.copy() result2[second] = result_orgin2.pop(2-j) # print(result2) for k in range(2): # 控制下标,因为后面要从6个元素的数组中取数字给4元素数字,所以需要控制4元素数组的下标 a1_index3 = a1_index2.copy() # [0, 1] third = a1_index3.pop(k) # [0] result_orgin3 = result_orgin2.copy() result3 = result2.copy() result3[third] = result_orgin3.pop(1-k) # print(result3) for l in range(6): # 控制下标,因为后面要从6个元素的数组中取数字给4元素数字,所以需要控制4元素数组的下标 a1_index4 = a1_index3.copy() # [0] fourth = a1_index4.pop() result4 = result3.copy() result_orgin4 = pool_orgin_running.copy() result4[fourth] = result_orgin4.pop(l) # print(result4) result.append(result4) # print(result.__len__()) if index_b == 3: for j in range(3): # 控制下标,因为后面要从6个元素的数组中取数字给4元素数字,所以需要控制4元素数组的下标 a1_index2 = a1_index.copy() # [0, 1, 2] second = a1_index2.pop() # [0, 1] # 复制被填充的数组 result2 = result1.copy() # 复制数据源 result_orgin2 = result_orgin.copy() result2[second] = result_orgin2.pop(j) # print(result2) for k in range(2): a1_index3 = a1_index2.copy() # [0, 1] third = a1_index3.pop() # [0] result_orgin3 = result_orgin2.copy() result3 = result2.copy() result3[third] = result_orgin3.pop(k) # print(result3) for l in range(1): a1_index4 = a1_index3.copy() # [0, 1] fourth = a1_index4.pop() # [0] result_orgin4 = result_orgin3.copy() result4 = result3.copy() result4[fourth] = result_orgin4.pop(l) # print(result4) result.append(result4) elif index_a == 2: for i in range(4): result_orgin = pool_number.copy() result1 = result_null.copy() # ['', '', '', ''] # 控制下标,因为后面要从6个元素的数组中取数字给4元素数字,所以需要控制4元素数组的下标 a2_index = index_model.copy() # [0, 1, 2, 3] first = a2_index.pop(i) # [0, 1, 2] result1[first] = result_orgin.pop(first) # print(result1) for j in range(3): # 控制下标,因为后面要从6个元素的数组中取数字给4元素数字,所以需要控制4元素数组的下标 a2_index2 = a2_index.copy() # [0, 1, 2] second = a2_index2.pop(j) # [0, 1] # 复制被填充的数组 result2 = result1.copy() # 复制数据源 result_orgin2 = result_orgin.copy() result2[second] = result_orgin2.pop(j) # print(result2) if index_b == 0: for m in range(6): a2_index3 = a2_index2.copy() third = a2_index3.pop() result_orgin3 = pool_orgin_running.copy() result3 = result2.copy() result3[third] = result_orgin3.pop(m) # print(result3) for n in range(5): a2_index4 = a2_index3.copy() fourth = a2_index4.pop() result4 = result3.copy() result_orgin4 = result_orgin3.copy() result4[fourth] = result_orgin4.pop(n) # print(result4) result.append(result4) if index_b == 1: for m in range(2): a2_index3 = a2_index2.copy() third = a2_index3.pop(m) result_orgin3 = result_orgin2.copy() result3 = result2.copy() result3[third] = result_orgin3.pop(1-m) # print(result3) for n in range(6): a2_index4 = a2_index3.copy() fourth = a2_index4.pop() result4 = result3.copy() result_orgin4 = pool_orgin_running.copy() #复制6元素数组 result4[fourth] = result_orgin4.pop(n) # print(result4) result.append(result4) pass if index_b == 2: for m in range(2): a2_index3 = a2_index2.copy() third = a2_index3.pop() result_orgin3 = result_orgin2.copy() result3 = result2.copy() result3[third] = result_orgin3.pop(m) for l in range(1): a2_index4 = a2_index3.copy() fourth = a2_index4.pop() result_orgin4 = result_orgin3.copy() result4 = result3.copy() result4[fourth] = result_orgin4.pop(l) result.append(result4) # print(result4) elif index_a == 3: for i in range(4): result_orgin = pool_number.copy() result1 = result_null.copy() # ['', '', '', ''] # 控制下标,因为后面要从6个元素的数组中取数字给4元素数字,所以需要控制4元素数组的下标 a3_index = index_model.copy() # [0, 1, 2, 3] first = a3_index.pop(i) #[0, 1, 2] result1[first] = result_orgin.pop(first) # print(result1) for j in range(3): # 控制下标,因为后面要从6个元素的数组中取数字给4元素数字,所以需要控制4元素数组的下标 a3_index2 = a3_index.copy() #[0, 1, 2] second = a3_index2.pop(j) #[0, 1] # 复制被填充的数组 result2 = result1.copy() # 复制数据源 result_orgin2 = result_orgin.copy() result2[second] = result_orgin2.pop(j) # print(result2) for k in range(2): a3_index3 = a3_index2.copy() #[0, 1] third = a3_index3.pop(k) #[0] result_orgin3 = result_orgin2.copy() result3 = result2.copy() result3[third] = result_orgin3.pop(k) # print(result3) for l in range(6): a3_index4 = a3_index3.copy() fourth = a3_index4.pop() result_orgin4 = pool_orgin_running.copy() result4 = result3.copy() result4[fourth] = result_orgin4.pop(l) result.append(result4) temp = [] for x in result: if x not in temp: temp.append(x) else: pass result = temp return result # 判断输入的数字是否有存在重复数字组合 def __repeat(self, number): Flag = None temp = list(number) for i in range(len(temp)): for j in range(i, len(temp)): if i != j: if temp[i] == temp[j]: return False else: Flag = True return Flag # 获取用户输入的数字 def __get_user_number(self): Flag = True count = 0 try: while Flag: number = input("请输入4位没有重复的数:") temp = list(number) if eval(number) > 9999: print("输入的数字太大>>>>>>>>") elif eval(number) < 1000: print("输入的数字太小>>>>>>>>") elif self.__repeat(number): return number elif number == '\r': print("123") except Exception: print("输入错误!程序终止>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") # 根据输入的数字判断结果 def __logical(self, number): first = 0 second = 0 for n in range(self.LENGTH): if number[n] == self.WIN_NUMBER[n]: first += 1 elif number[n] in self.WIN_NUMBER: second += 1 else: pass result = "%sA%sB" % (first, second) return result # AI的大脑 def __ai_think(self, number, answer): result = [] for i in range(len(self.ai_memory)): self.ai_memory[i] = list(self.ai_memory[i]) # 根据答案计算可能的集合 self.temp = self.gen_new_pool(number,eval(answer[0]),eval(answer[2])) # print("上个得到的AI集合:",self.ai_memory.__len__()) # print("上个结果的集合:",self.temp.__len__()) # 与历史结果集进行比较,生成最新结果集 self.ai_memory = [var for var in self.temp if var in self.ai_memory] #将数字从数组组成字符串 for i in self.ai_memory: temp = "" for j in i: temp += str(j) result.append(temp) # 去除已经猜测过的结果 for j in self.record: if j in result: result.remove(j) temp2 = [] for j in result: if j not in temp2: temp2.append(j) result = temp2 # print("result:", result.__len__(),result) # 从result数组中取出一个随机元素作为作为AI计算结果返回 if result == []: # 修复未知BUG,重置AI存储库(0A开头的结果会导致Bug,懒得修复了,该BUG经过修复后只会延长游戏时间,哈哈) self.ai_memory = list(self.__gen_all_result_set()) # 出现0结果集的BUG时,返回一个已输入的结果作为答案 return self.record[random.randrange(len(self.record))] else: result = result[random.randrange(result.__len__())] return result # 游戏开始 def start(self): print("作弊:", self.WIN_NUMBER) win_flag = True while win_flag: print("-------------------用户回合--------------------") user_guess_number = self.__get_user_number() print("用户猜测的数字是:", user_guess_number) user_guess_result = self.__logical(user_guess_number) print("结果是:", user_guess_result) if user_guess_number not in self.record: self.record.append(user_guess_number) if user_guess_result == "4A0B": print("你赢了!!!!!") break print("-------------------AI回合--------------------") ai_guess_number = self.__ai_think(user_guess_number, user_guess_result) ai_guess_result = self.__logical(ai_guess_number) print("AI猜测的数字是:", ai_guess_number) print("结果是:", ai_guess_result) if ai_guess_number not in self.record: self.record.append(ai_guess_number) if ai_guess_result == "4A0B": print("你输了!!!!!") break else: self.__ai_think(ai_guess_number, ai_guess_result)a = Guess3()a.start()