简单实现python数独游戏


Posted in Python onMarch 30, 2018

网上看到一个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处理圆角图片、圆形图片的例子
Apr 25 Python
全面解析Python的While循环语句的使用方法
Oct 13 Python
Python读取图片属性信息的实现方法
Sep 11 Python
Ubuntu 16.04 LTS中源码安装Python 3.6.0的方法教程
Dec 27 Python
Windows下安装python2和python3多版本教程
Mar 30 Python
Flask和Django框架中自定义模型类的表名、父类相关问题分析
Jul 19 Python
从0开始的Python学习016异常
Apr 08 Python
python基于Selenium的web自动化框架
Jul 14 Python
Django-xadmin后台导入json数据及后台显示信息图标和主题更改方式
Mar 11 Python
Django中ORM找出内容不为空的数据实例
May 20 Python
Python+OpenCV图像处理——实现直线检测
Oct 23 Python
opencv+pyQt5实现图片阈值编辑器/寻色块阈值利器
Nov 13 Python
Python使用MD5加密算法对字符串进行加密操作示例
Mar 30 #Python
windows环境下tensorflow安装过程详解
Mar 30 #Python
Python切片工具pillow用法示例
Mar 30 #Python
Python实现OpenCV的安装与使用示例
Mar 30 #Python
Ubuntu下使用Python实现游戏制作中的切分图片功能
Mar 30 #Python
Jupyter安装nbextensions,启动提示没有nbextensions库
Apr 23 #Python
python+opencv识别图片中的圆形
Mar 25 #Python
You might like
PHP中register_globals参数为OFF和ON的区别(register_globals 使用详解)
2012/02/05 PHP
PHP中防止直接访问或查看或下载config.php文件的方法
2012/07/07 PHP
PHP中防止SQL注入方法详解
2014/12/25 PHP
PHP编程基本语法快速入门手册
2016/01/07 PHP
php中mkdir()函数的权限问题分析
2016/09/24 PHP
Laravel手动分页实现方法详解
2016/10/09 PHP
Laravel如何创建服务器提供者实例代码
2019/04/15 PHP
Auntion-TableSort国人写的一个javascript表格排序的东西
2007/11/12 Javascript
使用Jquery打造最佳用户体验的登录页面的实现代码
2011/07/08 Javascript
纯JS实现的批量图片预览加载功能
2011/08/14 Javascript
js+html5获取用户地理位置信息并在Google地图上显示的方法
2015/06/05 Javascript
jQuery提示插件qTip2用法分析(支持ajax及多种样式)
2016/06/08 Javascript
jQuery基于ajax实现页面加载后检查用户登录状态的方法
2017/02/10 Javascript
理解Angular的providers给Http添加默认headers
2017/07/04 Javascript
使用jquery DataTable和ajax向页面显示数据列表的方法
2018/08/09 jQuery
vue服务端渲染缓存应用详解
2018/09/12 Javascript
微信小程序 wx:for遍历循环使用实例解析
2019/09/09 Javascript
Vue使用NProgress进度条的方法
2019/09/21 Javascript
[03:15]DOTA2-DPC中国联赛1月22日Recap集锦
2021/03/11 DOTA
Python 自动化表单提交实例代码
2017/06/08 Python
Python带动态参数功能的sqlite工具类
2018/05/26 Python
K最近邻算法(KNN)---sklearn+python实现方式
2020/02/24 Python
Python异常原理及异常捕捉实现过程解析
2020/03/25 Python
python 制作python包,封装成可用模块教程
2020/07/13 Python
澳大利亚领先的运动鞋商店:Hype DC
2018/03/31 全球购物
乌克兰数字设备、配件和智能技术的连锁商店:KTC
2020/08/18 全球购物
经典c++面试题二
2015/08/14 面试题
技校毕业生的自我评价
2013/12/27 职场文书
会计辞职信范文
2014/01/15 职场文书
前厅部经理岗位职责范文
2014/02/04 职场文书
大学生先进事迹材料
2014/02/16 职场文书
电钳工人个人求职信
2014/05/10 职场文书
拓展策划方案
2014/06/03 职场文书
大学国际贸易专业自荐信
2014/06/05 职场文书
祖国在我心中演讲稿450字
2014/09/05 职场文书
六一儿童节致辞稿(3篇)
2019/07/11 职场文书