简单实现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实现读取TXT文件数据并存进内置数据库SQLite3的方法
Aug 08 Python
Django admin实现图书管理系统菜鸟级教程完整实例
Dec 12 Python
Python hashlib模块用法实例分析
Jun 12 Python
python实现AES和RSA加解密的方法
Mar 28 Python
Python PyQt5 Pycharm 环境搭建及配置详解(图文教程)
Jul 16 Python
Python从列表推导到zip()函数的5种技巧总结
Oct 23 Python
python os.path.isfile()因参数问题判断错误的解决
Nov 29 Python
详解Python IO口多路复用
Jun 17 Python
django使用channels实现通信的示例
Oct 19 Python
python 带时区的日期格式化操作
Oct 23 Python
全面介绍python中很常用的单元测试框架unitest
Dec 14 Python
Python正则表达式中flags参数的实例详解
Apr 01 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 字符串正则替换函数preg_replace使用说明
2011/07/15 PHP
PHP使用递归生成文章树
2015/04/21 PHP
Laravel路由设定和子路由设定实例分析
2016/03/30 PHP
php提取微信账单的有效信息
2018/10/01 PHP
在Laravel的Model层做数据缓存的实现
2019/09/26 PHP
CSS中一些@规则的用法小结
2021/03/09 HTML / CSS
用dom+xhtml+css制作的一个相册效果代码打包下载
2008/01/24 Javascript
让firefox支持IE的一些方法的javascript扩展函数代码
2010/01/02 Javascript
JS测试显示屏分辨率以及屏幕尺寸的方法
2013/11/22 Javascript
JavaScript数据类型之基本类型和引用类型的值
2015/04/01 Javascript
jquery实现可点击伸缩与展开的菜单效果代码
2015/08/31 Javascript
JS实现浏览器状态栏显示时间的方法
2015/10/27 Javascript
浅谈html转义及防止javascript注入攻击的方法
2016/12/04 Javascript
jQuery如何跳转到另一个网页 就这么简单
2016/12/28 Javascript
js实现一个简单的数字时钟效果
2017/03/29 Javascript
从对象列表中获取一个对象的方法,依据关键字和值
2017/09/20 Javascript
express express-session的使用小结
2018/12/12 Javascript
为什么要使用Vuex的介绍
2019/01/19 Javascript
详解JavaScript原生封装ajax请求和Jquery中的ajax请求
2019/02/14 jQuery
微信小程序封装多张图片上传api代码实例
2019/12/30 Javascript
[01:20]2018DOTA2亚洲邀请赛总决赛战队Mineski晋级之路
2018/04/07 DOTA
浅析python中的分片与截断序列
2016/08/09 Python
Python Paramiko模块的安装与使用详解
2016/11/18 Python
Python使用内置json模块解析json格式数据的方法
2017/07/20 Python
pip matplotlib报错equired packages can not be built解决
2018/01/06 Python
python 代码实现k-means聚类分析的思路(不使用现成聚类库)
2020/06/01 Python
Django中Q查询及Q()对象 F查询及F()对象用法
2020/07/09 Python
css3进阶之less实现星空动画的示例代码
2019/09/10 HTML / CSS
利用html5 canvas动态画饼状图的示例代码
2018/04/02 HTML / CSS
编写一子程序,将一链表倒序,即使链表表尾变表头,表头变表尾
2016/02/10 面试题
大学生职业规划范文:象牙塔生活的四年计划
2014/01/14 职场文书
最新茶叶店创业计划书
2014/01/14 职场文书
学习标兵获奖感言
2014/02/20 职场文书
公共场所标语
2014/06/30 职场文书
电焊工岗位工作职责
2014/07/09 职场文书
2014年卫生院工作总结
2014/12/03 职场文书