重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这期内容当中小编将会给大家带来有关使用python怎么实现一个数独游戏,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
在巴州等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都网站建设 网站设计制作定制网站制作,公司网站建设,企业网站建设,品牌网站设计,全网营销推广,成都外贸网站制作,巴州网站建设费用合理。Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。
import random import itertools from copy import deepcopy def make_board(m = 3): numbers = list(range(1, m**2 + 1)) board = None while board is None: board = attempt_board(m, numbers) return board def attempt_board(m, numbers): n = m**2 board = [[None for _ in range(n)] for _ in range(n)] for i, j in itertools.product(range(n), repeat = 2): i0, j0 = i - i % m, j - j % m random.shuffle(numbers) for x in numbers: if(x not in board[i]) and all(row[j] != x for row in board) and all(x not in row[j0:j0+m] for row in board[i0:i]): board[i][j] = x break else: return None return board def print_board(board, m = 3): numbers = list(range(1, m**2 + 1)) omit = 5 challange = deepcopy(board) for i, j in itertools.product(range(omit), range(m ** 2)): x = random.choice(numbers) - 1 challange[x][j] = None spacer = "++---+---+---++---+---+---++---+---+---++" print (spacer.replace('-', '=')) for i, line in enumerate(challange): print("|| {0} | {1} | {2} || {3} | {4} | {5} || {6} | {7} | {8} ||".format(*(cell or ' ' for cell in line))) if(i + 1) % 3 == 0: print(spacer.replace('-', '=')) else: print(spacer) return challange def print_answer(board): spacer = "++---+---+---++---+---+---++---+---+---++" print(spacer.replace('-','=')) for i, line in enumerate(board): print("|| {0} | {1} | {2} || {3} | {4} | {5} || {6} | {7} | {8} ||".format(*(cell or ' ' for cell in line))) if(i + 1) % 3 == 0: print(spacer.replace('-','=')) else: print(spacer) def is_full(challange, m = 3): for i, j in itertools.product(range(m**2), repeat = 2): if challange[i][j] is None: return False return True def cal_candidate(challange, x, y, m = 3): candidate = range(1, m ** 2 + 1) for i in range(m ** 2): if challange[x][i] in candidate: candidate.remove(challange[x][i]) if challange[i][y] in candidate: candidate.remove(challange[i][y]) for i, j in itertools.product(range(m), repeat = 2): x0, y0 = x - x % m, y - y % m if challange[x0 + i][y0 + j] in candidate: candidate.remove(challange[x0 + i][y0 + j]) return candidate def least_candidate(challange, m = 3): least, x, y = m ** 2, -1, -1 for i, j in itertools.product(range(m ** 2), repeat = 2): if not challange[i][j]: num = len(cal_candidate(challange, i, j)) if num < least: least = num x, y = i, j return x, y def solving_soduku(challange, m = 3): if is_full(challange): return challange x, y = least_candidate(challange) id = x * (m ** 2) + y result = try_candidate(challange, id) return result def try_candidate(challange, id, m = 3): if is_full(challange): return challange x = id / (m ** 2) y = id % (m ** 2) while challange[x][y]: id = (id + 1) % m ** 4 x = id / (m ** 2) y = id % (m ** 2) candidate = cal_candidate(challange, x, y) if len(candidate) == 0: return False for i in range(len(candidate)): challange[x][y] = candidate[i] result_r = try_candidate(challange, (id + 1) % m ** 4) if not result_r: pass else: return challange challange[x][y] = None return False #Board = make_board() #print Board #challange = print_board(Board) #print_answer(Board) #result = solving_soduku(challange) #print_answer(result) testing = [[8, None, None, None, None, None, None, None, None], [None, None, 3, 6, None, None, None, None, None], [None, 7, None, None, 9, None, 2, None, None], [None,5 , None, None, None, 7, None, None, None ], [None, None, None, None, 4, 6, 7, None, None], [None, None, None, 1, None, None, None, 3, None], [None, None, 1, None, None, None, None, 6, 8], [None, None, 8, 5, None, None, None, 1, None], [None, 9, None, None, None, None, 4, None, None]] result = solving_soduku(testing) print_answer(result)
上述就是小编为大家分享的使用python怎么实现一个数独游戏了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。