简单实现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正则表达式操作指南(re使用)
Sep 06 Python
Python笔记(叁)继续学习
Oct 24 Python
Python中的异常处理学习笔记
Jan 28 Python
Python描述器descriptor详解
Feb 03 Python
使用Python生成随机密码的示例分享
Feb 18 Python
Python实现的简单排列组合算法示例
Jul 04 Python
Python高级特性切片(Slice)操作详解
Sep 27 Python
Python玩转Excel的读写改实例
Feb 22 Python
树莓派用python中的OpenCV输出USB摄像头画面
Jun 22 Python
Django CSRF跨站请求伪造防护过程解析
Jul 31 Python
Python 使用 Pillow 模块给图片添加文字水印的方法
Aug 30 Python
基于Python实现ComicReaper漫画自动爬取脚本过程解析
Nov 11 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中如何将数组变量写入文件
2013/06/06 PHP
解析php中获取系统信息的方法
2013/06/25 PHP
PHP实现生成唯一编号(36进制的不重复编号)
2014/07/01 PHP
php计算函数执行时间的方法
2015/03/20 PHP
JavaScript中的匀速运动和变速(缓冲)运动详细介绍
2012/11/11 Javascript
使用Jasmine和Karma对AngularJS页面程序进行测试
2016/03/05 Javascript
AngularJs Understanding the Model Component
2016/09/02 Javascript
webpack学习--webpack经典7分钟入门教程
2017/06/28 Javascript
Vue2.0 axios前后端登陆拦截器(实例讲解)
2017/10/27 Javascript
p5.js入门教程之键盘交互
2018/03/19 Javascript
JavaScript面试出现频繁的一些易错点整理
2018/03/29 Javascript
vue 项目地址去掉 #的方法
2018/10/20 Javascript
简单说说angular.json文件的使用
2018/10/29 Javascript
javascript中的this作用域详解
2019/07/15 Javascript
vue css 引入asstes中的图片无法显示的四种解决方法
2020/03/16 Javascript
11个Javascript小技巧帮你提升代码质量(小结)
2020/12/28 Javascript
[00:32]2016完美“圣”典风云人物:Maybe宣传片
2016/12/05 DOTA
[00:11]战神迅矛
2019/03/06 DOTA
python Flask实现restful api service
2017/12/04 Python
python2.7到3.x迁移指南
2018/02/01 Python
利用Django-environ如何区分不同环境
2018/08/26 Python
简单了解Pandas缺失值处理方法
2019/11/16 Python
Python3监控windows,linux系统的CPU、硬盘、内存使用率和各个端口的开启情况详细代码实例
2020/03/18 Python
基于python实现MQTT发布订阅过程原理解析
2020/07/27 Python
纯CSS3代码实现switch滑动开关按钮效果
2016/08/30 HTML / CSS
新西兰便宜隐形眼镜购买网站:QUICKLENS New Zealand
2019/03/02 全球购物
军训生自我鉴定范文
2013/12/27 职场文书
小学清明节活动方案
2014/03/08 职场文书
早读课迟到检讨书
2014/09/25 职场文书
土木工程专业本科生求职信
2014/10/01 职场文书
校园广播稿精选
2014/10/01 职场文书
工艺技术员岗位职责
2015/02/04 职场文书
Nginx配置并兼容HTTP实现代码解析
2021/03/31 Servers
「月刊Comic Alive」2022年5月号封面公开
2022/03/21 日漫
html中相对位置与绝对位置的具体使用
2022/05/15 HTML / CSS
Redis特殊数据类型HyperLogLog基数统计算法讲解
2022/06/01 Redis