简单实现python数独游戏
作者:单鹏飞 发布时间:2023-06-21 15:24:22
标签:python,数独游戏
网上看到一个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)
来源:https://blog.csdn.net/Pnfy__Shan/article/details/54970361


猜你喜欢
- 最近一段时间一直比较忙,已经有好几个月没有打理博客了。现将一个最近在项目中制作的一个菜单实例整理出来,共享一下。在后台或OA系统中最常用到的
- 有两个简单的方法MySQL中的数据加载到MySQL数据库从先前备份的文件。LOAD DATA导入数据:MySQL提供了LOAD DATA语句
- 什么叫模板继承呢在我的理解就是:在前端页面中肯定有很多页面中有很多相同的地方,比如页面顶部的导航栏,底部的页脚等部分,这时候如果每一个页面都
- $("input").attr("checked","checked") 设置以
- 本文实例分析了python动态性强类型用法。分享给大家供大家参考。具体如下:Python变量声明和定义与C#不同,Python在使用变量之前
- 摘要在这篇文章里,我将以反模式的角度来直接讨论Django的低级ORM查询方法的使用。作为一种替代方式,我们需要在包含业务逻辑的
- 如何判断一个数值(字符串)为整数不严格检查方法浮点数的自带方法is_integer()如果确定输入的内容为浮点数,是可以直接使用float数
- 1、MFCC概述在语音识别(Speech Recognition)和话者识别(Speaker Recognition)方面,最常用到的语音特
- 如何做一个只能从本站点才能访问的页面?可以用与防止盗链<%if left(Request.ServerVariables(&
- 某次一不小心,用了delete from xxx 删除了几条重要数据,在网上找了很多方法,但都比较零散,打算记录本次数据找回的过程。大致分为
- 目录一、数据库瓶颈二、分库分表2、水平分表3、垂直分库4、垂直分表三、分库分表工具四、分库分表步骤五、分库分表问题1、非partition
- 实现一个不规则窗体这里我们实现一个圆形窗体,实现其他形状的窗体与这个方法类似。首先,把窗口的高度(height)和宽度(width)值修改为
- 先推荐一个学习python的好网址简明 Python 教程 Swaroop, C. H. 著 沈洁元 译在线教程的网址
- 有时引用其它js时,其js却使用了window.onload事件,这样的话,引入的页面的onload事件就有可能执行不了,怎样才能两个都运行
- 原作者:Nik Piepenbreier翻译&内容补充:费弗里原文地址:https://towardsdatascience.com
- 最近做项目的时候有一个发布新闻的需求,新闻编辑的时候要求能发布带格式的文本内容和能展示支持图片。由于项目是用 Vue 开发的,所以找编辑器的
- 使用threading.Event可以实现线程间相互通信,之前的Python:使用threading模块实现多线程编程七[使用Conditi
- bs4的安装要使用BeautifulSoup4需要先安装lxml,再安 * s4pip install lxmlpip install bs4
- 时间格式化函数,代码简单但较实用代码很简单,谁都能看懂Function fmstr(str, str1, Lens) Dim str2For
- 需求细化:1.身份证必须能够通过身份证校验程序。2.通过查询,发现身份证号码是有国家标准的,标准号为 GB 11643-1999 可以从百度