未加星标

简单实现python数独游戏

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

网上看到一个python写的数独,很好玩,分享给大家。

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实现解数独程序代码Python如何判断数独是否合法python实现数独算法实例Java基于二维数组实现的数独问题示例java数独游戏完整版分享java版数独游戏界面实现(二)java版数独游戏核心算法(一)简单实现java数独游戏Java实现解数独的小程序python和java分别实现数独游戏

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: 算法JavaPython
tags: None,challange,candidate,board,return,range,print,id,spacer,数独,++,numbers
分页:12
转载请注明
本文标题:简单实现python数独游戏
本站链接:http://www.codesec.net/view/574844.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(87)