简单实现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装饰器decorator介绍
Nov 21 Python
python通过函数属性实现全局变量的方法
May 16 Python
基于python的Tkinter实现一个简易计算器
Dec 31 Python
git使用.gitignore设置不生效或不起作用问题的解决方法
Jun 01 Python
Python使用PIL模块生成随机验证码
Nov 21 Python
Python OpenCV对本地视频文件进行分帧保存的实例
Jan 08 Python
Python 实现中值滤波、均值滤波的方法
Jan 09 Python
Python学习笔记之视频人脸检测识别实例教程
Mar 06 Python
Python 编程速成(推荐)
Apr 15 Python
使用django自带的user做外键的方法
Nov 30 Python
python 模拟登录B站的示例代码
Dec 15 Python
pytorch实现ResNet结构的实例代码
May 17 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/25 PHP
php实现的简单美国商品税计算函数
2015/07/13 PHP
WordPress过滤垃圾评论的几种主要方法小结
2016/07/11 PHP
PHP之将POST数据转化为字符串的实现代码
2016/11/03 PHP
一个加密JavaScript的开源工具PACKER2.0.2
2006/11/04 Javascript
iis6+javascript Add an Extension File
2007/06/13 Javascript
基于JQuery.timer插件实现一个计时器
2010/04/25 Javascript
基于javascipt-dom编程 table对象的使用
2013/04/22 Javascript
Javascript实现动态菜单添加的实例代码
2013/07/05 Javascript
jQuery获得IE版本不准确webbrowser的解决方法
2014/02/23 Javascript
JavaScript中的迭代器和生成器详解
2014/10/29 Javascript
基于jquery实现发送文章到手机的代码
2014/12/26 Javascript
jQuery中:selected选择器用法实例
2015/01/04 Javascript
JavaScript中的Object对象学习教程
2016/05/20 Javascript
浅析JavaScript中命名空间namespace模式
2016/06/22 Javascript
Angularjs的ng-repeat中去除重复数据的方法
2016/08/05 Javascript
js原生实现FastClick事件的实例
2016/11/20 Javascript
nodejs操作mysql实现增删改查的实例
2017/05/28 NodeJs
vue.js实现标签页切换效果
2018/06/07 Javascript
解决LayUI表单获取不到data的问题
2018/08/20 Javascript
解决vue更新路由router-view复用组件内容不刷新的问题
2019/11/04 Javascript
[03:38]TI4西雅图DOTA2前线报道 71专访
2014/07/08 DOTA
简单谈谈python的反射机制
2016/06/28 Python
使用apidocJs快速生成在线文档的实例讲解
2018/02/07 Python
Django 多语言教程的实现(i18n)
2018/07/07 Python
python中实现字符串翻转的方法
2018/07/11 Python
Django压缩静态文件的实现方法详析
2018/08/26 Python
Python Pandas对缺失值的处理方法
2019/09/27 Python
如何真正的了解python装饰器
2020/08/14 Python
AmazeUI 网格的实现示例
2020/08/13 HTML / CSS
法国珠宝店:CLEOR
2017/01/29 全球购物
美国婴儿用品及配件购买网站:Munchkin
2019/04/03 全球购物
职工运动会感言
2014/02/07 职场文书
超市重阳节活动方案
2014/02/10 职场文书
领导干部作风整顿个人剖析材料
2014/10/11 职场文书
Python手拉手教你爬取贝壳房源数据的实战教程
2021/05/21 Python