简单实现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中统计函数运行耗时的方法
May 05 Python
在Python的Django框架中包装视图函数
Jul 20 Python
Python探索之pLSA实现代码
Oct 25 Python
Python简单实现socket信息发送与监听功能示例
Jan 03 Python
对numpy Array [: ,] 的取值方法详解
Jul 02 Python
Pycharm无法使用已经安装Selenium的解决方法
Oct 13 Python
python 弹窗提示警告框MessageBox的实例
Jun 18 Python
linux下安装python3和对应的pip环境教程详解
Jul 01 Python
解决在pycharm运行代码,调用CMD窗口的命令运行显示乱码问题
Aug 23 Python
Python序列化与反序列化pickle用法实例
Nov 11 Python
Pandas数据离散化原理及实例解析
Nov 16 Python
Pytorch 中retain_graph的用法详解
Jan 07 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中is_dir()函数使用指南
2015/05/08 PHP
php文件系统处理方法小结
2016/05/23 PHP
基于jquery循环map功能的代码
2011/02/26 Javascript
js实现通用的微信分享组件示例
2014/03/10 Javascript
Jquery 在页面加载后执行的几种方式
2014/03/14 Javascript
javascript判断chrome浏览器的方法
2014/03/26 Javascript
Javascript玩转继承(二)
2014/05/08 Javascript
js限制文本框的输入内容代码分享(3类)
2015/08/20 Javascript
JavaScript获取当前cpu使用率的方法
2015/12/15 Javascript
快速掌握Node.js之Window下配置NodeJs环境
2016/03/21 NodeJs
jQueryUI Datepicker组件设置日期高亮
2016/10/13 Javascript
浅谈javascript中的 “ &amp;&amp; ” 和 “ || ”
2017/02/02 Javascript
ES6中Array.find()和findIndex()函数的用法详解
2017/09/16 Javascript
vue+springboot实现项目的CORS跨域请求
2018/09/05 Javascript
浅谈Vue.js 中的 v-on 事件指令的使用
2018/11/25 Javascript
从0到1搭建element后台框架优化篇(打包优化)
2019/05/12 Javascript
echarts多条折线图动态分层的实现方法
2019/05/24 Javascript
vue中使用router全局守卫实现页面拦截的示例
2020/10/23 Javascript
Python实现扫描指定目录下的子目录及文件的方法
2014/07/16 Python
python实现字典(dict)和字符串(string)的相互转换方法
2017/03/01 Python
Python设计实现的计算器功能完整实例
2017/08/18 Python
python实现协同过滤推荐算法完整代码示例
2017/12/15 Python
用TensorFlow实现lasso回归和岭回归算法的示例
2018/05/02 Python
python 调用有道api接口的方法
2019/01/03 Python
Python读取csv文件分隔符设置方法
2019/01/14 Python
Python实现的微信支付方式总结【三种方式】
2019/04/13 Python
Django 再谈一谈json序列化
2020/03/16 Python
python读取mysql数据绘制条形图
2020/03/25 Python
美国家喻户晓的保健品品牌:Vitamin World(维他命世界)
2016/08/19 全球购物
美国从事品牌鞋类零售的连锁店:Famous Footwear
2016/08/25 全球购物
单位未婚证明范本
2014/01/18 职场文书
幼儿教师2014年度工作总结
2014/12/16 职场文书
2016关于军训的心得体会
2016/01/11 职场文书
Springboot如何同时装配两个相同类型数据库
2021/11/17 Java/Android
PostGIS的安装与入门使用指南
2022/01/18 PostgreSQL
python和Appium的移动端多设备自动化测试框架
2022/04/26 Python