简单实现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 相关文章推荐
使用django-suit为django 1.7 admin后台添加模板
Nov 18 Python
Python3实现发送QQ邮件功能(html)
Dec 15 Python
详解Python下ftp上传文件linux服务器
Jun 21 Python
python使用pandas处理大数据节省内存技巧(推荐)
May 05 Python
Django 多表关联 存储 使用方法详解 ManyToManyField save
Aug 09 Python
Python学习笔记之列表推导式实例分析
Aug 13 Python
在Pytorch中使用样本权重(sample_weight)的正确方法
Aug 17 Python
Python解析json代码实例解析
Nov 25 Python
python 浅谈serial与stm32通信的编码问题
Dec 18 Python
Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法实例分析
Dec 26 Python
Python实现栈的方法详解【基于数组和单链表两种方法】
Feb 22 Python
Django admin管理工具TabularInline类用法详解
May 14 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加密解密函数详解
2015/10/28 PHP
根据分辨率不同,调用不同的css文件
2006/08/25 Javascript
javascript控制frame,iframe的src属性代码
2009/12/31 Javascript
Firebug入门指南(Firefox浏览器)
2010/08/21 Javascript
ie8模式下click无反应点击option无反应的解决方法
2014/10/11 Javascript
jquery正则表达式验证(手机号、身份证号、中文名称)
2015/12/31 Javascript
js 获取范围内的随机数实例代码
2016/08/02 Javascript
jstl中判断list中是否包含某个值的简单方法
2016/10/14 Javascript
JS实现本地存储信息的方法(基于localStorage与userData)
2017/02/18 Javascript
JavaScript实现省市县三级级联特效
2017/05/16 Javascript
JavaScript实现一个空中避难的小游戏
2017/06/06 Javascript
基于jquery实现九宫格拼图小游戏
2018/11/30 jQuery
小程序组件之自定义顶部导航实例
2019/06/12 Javascript
node实现爬虫的几种简易方式
2019/08/22 Javascript
微信用户访问小程序的登录过程详解
2019/09/20 Javascript
vue 全局封装loading加载教程(全局监听)
2020/11/05 Javascript
[01:32]2014DOTA2西雅图邀请赛 CIS我们有信心进入正赛
2014/07/08 DOTA
[07:37]DOTA2-DPC中国联赛2月2日Recap集锦
2021/03/11 DOTA
深入浅析Python中list的复制及深拷贝与浅拷贝
2018/09/03 Python
使用python将请求的requests headers参数格式化方法
2019/01/02 Python
Python调用服务接口的实例
2019/01/03 Python
浅谈Python编程中3个常用的数据结构和算法
2019/04/30 Python
详解使用Python写一个向数据库填充数据的小工具(推荐)
2020/09/11 Python
JustFab加拿大:女鞋、靴子、手袋和服装在线
2018/05/18 全球购物
德国家具折扣店:POCO
2020/02/28 全球购物
Python文件操作的面试题
2013/06/22 面试题
环境科学专业大学生自荐信格式
2013/09/21 职场文书
教育科学研究生自荐信
2013/10/09 职场文书
兼职学生的自我评价
2013/11/24 职场文书
工作表扬信的范文
2014/01/10 职场文书
房屋继承公证书
2014/04/10 职场文书
高校师德师风自我剖析材料
2014/09/29 职场文书
骨干教师个人总结
2015/02/11 职场文书
2015年护士工作总结范文
2015/03/31 职场文书
财务人员廉洁自律心得体会
2016/01/13 职场文书
一道JS算法面试题——冒泡、选择排序
2021/04/21 Javascript