简单实现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 相关文章推荐
python2.7删除文件夹和删除文件代码实例
Dec 18 Python
Python实现的数据结构与算法之双端队列详解
Apr 22 Python
python使用wxpython开发简单记事本的方法
May 20 Python
python爬虫面试宝典(常见问题)
Mar 02 Python
Python FTP两个文件夹间的同步实例代码
May 25 Python
对python mayavi三维绘图的实现详解
Jan 08 Python
Python3.5面向对象与继承图文实例详解
Apr 24 Python
对PyQt5中树结构的实现方法详解
Jun 17 Python
Python爬虫实现的根据分类爬取豆瓣电影信息功能示例
Sep 15 Python
如何安装并在pycharm使用selenium的方法
Apr 30 Python
Python3与fastdfs分布式文件系统如何实现交互
Jun 23 Python
python使用pywinauto驱动微信客户端实现公众号爬虫
May 19 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
PHP4 与 MySQL 数据库操作函数详解
2006/10/09 PHP
php设计模式之装饰模式应用案例详解
2019/06/17 PHP
二行代码解决全部网页木马
2008/03/28 Javascript
capacityFixed 基于jquery的类似于新浪微博新消息提示的定位框
2011/05/24 Javascript
JavaScript函数详解
2014/11/17 Javascript
javascript判断并获取注册表中可信任站点的方法
2015/06/01 Javascript
jquery SweetAlert插件实现响应式提示框
2015/08/18 Javascript
Jquery为DIV添加click事件的简单实例
2016/06/02 Javascript
AngularJS extend用法详解及实例代码
2016/11/15 Javascript
利用fecha进行JS日期处理
2016/11/21 Javascript
bootstrap3 dialog 更强大、更灵活的模态框
2017/04/20 Javascript
解决ionic和angular上拉加载的问题
2017/08/03 Javascript
JS实现匀速与减速缓慢运动的动画效果封装示例
2018/08/27 Javascript
微信小程序分享功能onShareAppMessage(options)用法分析
2019/04/24 Javascript
node Buffer缓存区常见操作示例
2019/05/04 Javascript
layer插件实现在弹出层中弹出一警告提示并关闭弹出层的方法
2019/09/24 Javascript
javaScript把其它类型转换为Number类型
2019/10/13 Javascript
vue实现设置载入动画和初始化页面动画效果
2019/10/28 Javascript
在Python中使用全局日志时需要注意的问题
2015/05/06 Python
Python使用turtule画五角星的方法
2015/07/09 Python
Python语言描述机器学习之Logistic回归算法
2017/12/21 Python
python使用生成器实现可迭代对象
2018/03/20 Python
python通过tcp发送xml报文的方法
2018/12/28 Python
解决python中画图时x,y轴名称出现中文乱码的问题
2019/01/29 Python
python调用摄像头拍摄数据集
2019/06/01 Python
Python中的 is 和 == 以及字符串驻留机制详解
2019/06/28 Python
使用python将mysql数据库的数据转换为json数据的方法
2019/07/01 Python
Python高级特性 切片 迭代解析
2019/08/23 Python
Django实现任意文件上传(最简单的方法)
2020/06/03 Python
Jupyter Notebook 安装配置与使用详解
2021/01/06 Python
使用Python爬虫爬取小红书完完整整的全过程
2021/01/19 Python
Prototype如何更新局部页面
2013/03/03 面试题
给物业的表扬信
2014/01/21 职场文书
青年文明号创建口号大全
2015/12/25 职场文书
中国梦宣传标语口号
2015/12/26 职场文书
创业计划书之宠物店
2019/09/19 职场文书