简单实现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中使用dict和set方法的教程
Apr 27 Python
将Emacs打造成强大的Python代码编辑工具
Nov 20 Python
Python画图学习入门教程
Jul 01 Python
python使用arcpy.mapping模块批量出图
Mar 06 Python
python利用微信公众号实现报警功能
Jun 10 Python
攻击者是如何将PHP Phar包伪装成图像以绕过文件类型检测的(推荐)
Oct 11 Python
python实现给微信指定好友定时发送消息
Apr 29 Python
详解Python sys.argv使用方法
May 10 Python
python Matplotlib底图中鼠标滑过显示隐藏内容的实例代码
Jul 31 Python
python滑块验证码的破解实现
Nov 10 Python
TensorFlow tensor的拼接实例
Jan 19 Python
python调用API接口实现登陆短信验证
May 10 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
BBS(php &amp; mysql)完整版(八)
2006/10/09 PHP
vs中通过剪切板循环来循环粘贴不同内容
2011/04/30 PHP
使用php测试硬盘写入速度示例
2014/01/27 PHP
php发送post请求的三种方法
2014/02/11 PHP
PHP的反射机制实例详解
2017/03/29 PHP
PHP递归删除多维数组中的某个值
2017/04/17 PHP
详解Yii2.0使用AR联表查询实例
2017/06/16 PHP
php操作mongodb封装类与用法实例
2018/09/01 PHP
php使用curl模拟多线程实现批处理功能示例
2019/07/25 PHP
用js得到网页中所有的div的id
2020/10/19 Javascript
详细讲解JS节点知识
2010/01/31 Javascript
javascript 动态修改样式和层叠样式表代码
2010/04/27 Javascript
常用一些Javascript判断函数
2012/08/14 Javascript
window.open关于浏览器拦截问题分析及解决方法
2013/02/05 Javascript
getAsDataURL在Firefox7.0下无法预览本地图片的解决方法
2013/11/15 Javascript
node.js中的fs.futimes方法使用说明
2014/12/17 Javascript
浅谈javascript中for in 和 for each in的区别
2015/04/23 Javascript
实例详解JSON数据格式及json格式数据域字符串相互转换
2016/01/07 Javascript
nodejs加密Crypto的实例代码
2016/07/07 NodeJs
javascript容错处理代码(屏蔽js错误)
2017/01/20 Javascript
input 标签实现输入框带提示文字效果(两种方法)
2017/10/09 Javascript
Javacript中自定义的map.js  的方法
2017/11/26 Javascript
JS字符串去除连续或全部重复字符的实例
2018/03/08 Javascript
NodeJS实现同步的方法
2019/03/02 NodeJs
package.json中homepage属性的作用详解
2020/03/11 Javascript
2020京东618叠蛋糕js脚本(亲测好用)
2020/06/02 Javascript
[04:27]DOTA2官方论坛水友赛集锦
2013/09/16 DOTA
[45:52]2018DOTA2亚洲邀请赛 4.1小组赛 A组加赛 LGD vs Liquid
2018/04/02 DOTA
python3结合openpyxl库实现excel操作的实例代码
2018/09/11 Python
Django 创建后台,配置sqlite3教程
2019/11/18 Python
Python Dict找出value大于某值或key大于某值的所有项方式
2020/06/05 Python
HTML5中的网络存储实现方式
2020/04/28 HTML / CSS
巴西最大的珠宝连锁店:Vivara
2019/04/18 全球购物
中专生自荐信
2013/10/12 职场文书
《灰椋鸟》教学反思
2014/04/27 职场文书
优秀党员自我评价范文
2014/09/15 职场文书