简单实现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的Django框架中的项目进行单元测试的方法
Apr 11 Python
Python多进程分块读取超大文件的方法
Apr 13 Python
用Python实现随机森林算法的示例
Aug 24 Python
Python实现文件信息进行合并实例代码
Jan 17 Python
python中使用PIL制作并验证图片验证码
Mar 15 Python
python求最大值最小值方法总结
Jun 25 Python
django admin组件使用方法详解
Jul 19 Python
Python 元组拆包示例(Tuple Unpacking)
Dec 24 Python
Python如何用filter函数筛选数据
Mar 05 Python
python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧(自定义信号与槽)详解与实例
Mar 06 Python
浅谈python量化 双均线策略(金叉死叉)
Jun 03 Python
浅谈Python类的单继承相关知识
May 12 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中ob_flush函数和flush函数用法分析
2015/03/18 PHP
理解php依赖注入和控制反转
2016/05/11 PHP
javascript 避免闭包引发的问题
2009/03/17 Javascript
Javascript 阻止javascript事件冒泡,获取控件ID值
2009/06/27 Javascript
Node.js的包详细介绍
2015/01/14 Javascript
jQuery在页面加载时动态修改图片尺寸的方法
2015/03/20 Javascript
js动态修改表格行colspan列跨度的方法
2015/03/30 Javascript
angularjs在ng-repeat中使用ng-model遇到的问题
2016/01/21 Javascript
基于jQuery实现Accordion手风琴自定义插件
2020/10/13 Javascript
Angular4实现动态添加删除表单输入框功能
2017/08/11 Javascript
基于js中this和event 的区别(详解)
2017/10/24 Javascript
AngularJs 终极购物车(实例讲解)
2017/11/08 Javascript
详解用webpack的CommonsChunkPlugin提取公共代码的3种方式
2017/11/09 Javascript
浅谈MUI框架中加载外部网页或服务器数据的方法
2018/01/31 Javascript
在vscode中统一vue编码风格的方法
2018/02/22 Javascript
Koa代理Http请求的示例代码
2018/10/10 Javascript
layui实现二维码弹窗、并下载到本地的方法
2019/09/25 Javascript
手把手教你如何编译打包video.js
2020/12/09 Javascript
Python中几种操作字符串的方法的介绍
2015/04/09 Python
python实现beta分布概率密度函数的方法
2019/07/08 Python
python 提取文件指定列的方法示例
2019/08/07 Python
Python整数与Numpy数据溢出问题解决
2019/09/11 Python
Django中Aggregation聚合的基本使用方法
2020/07/09 Python
CSS3支持IE6, 7, and 8的边框border属性
2012/12/28 HTML / CSS
基于zepto的插件之移动端无缝向上滚动并上下触摸滑动实例代码
2016/12/20 HTML / CSS
印尼在线精品店:Berrybenka.com
2016/10/22 全球购物
波兰办公用品和学校用品在线商店:Dlabiura24.pl
2020/11/18 全球购物
财务总经理岗位职责
2014/02/16 职场文书
会计求职信
2014/05/29 职场文书
机关作风建设心得体会
2014/10/22 职场文书
2014年超市工作总结
2014/11/19 职场文书
青年文明号申报材料
2014/12/23 职场文书
公司仓库管理制度
2015/08/04 职场文书
浅谈PHP7中的一些小技巧
2021/05/29 PHP
Windows下redis下载、redis安装及使用教程
2021/06/02 Redis
默认网关不可用修复后过一会又不好使了解决方法
2022/04/08 数码科技