简单实现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迭代器的使用方法实例
Nov 21 Python
Python向MySQL批量插数据的实例讲解
Mar 31 Python
Python正则表达式和re库知识点总结
Feb 11 Python
Python函数中不定长参数的写法
Feb 13 Python
基于python实现自动化办公学习笔记(CSV、word、Excel、PPT)
Aug 06 Python
python3.7 利用函数os pandas利用excel对文件名进行归类
Sep 29 Python
Python数据可视化:幂律分布实例详解
Dec 07 Python
OpenCV python sklearn随机超参数搜索的实现
Jan 17 Python
python try...finally...的实现方法
Nov 25 Python
5分钟快速掌握Python定时任务框架的实现
Jan 26 Python
Python标准库pathlib操作目录和文件
Nov 20 Python
python字符串拼接.join()和拆分.split()详解
Nov 23 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
在windows iis5下安装php4.0+mysql之我见
2006/10/09 PHP
php中计算程序运行时间的类代码
2012/11/03 PHP
php判断上传的Excel文件中是否有图片及PHPExcel库认识
2013/01/11 PHP
删除html标签得到纯文本可处理嵌套的标签
2014/04/28 PHP
php图片水印添加、压缩、剪切的封装类实现
2020/04/18 PHP
PHPUnit测试私有属性和方法功能示例
2018/06/12 PHP
PHP实现负载均衡的加权轮询方法分析
2018/08/22 PHP
脚本吧 - 幻宇工作室用到js,超强推荐expand.js
2006/12/23 Javascript
iframe 上下滚动条如何默认在下方实现原理
2012/12/10 Javascript
js中AppendChild与insertBefore的用法详细解析
2013/12/16 Javascript
ParseInt函数参数设置介绍
2014/01/02 Javascript
实例讲解jquery中mouseleave和mouseout的区别
2016/02/17 Javascript
jQuery基础_入门必看知识点
2016/07/04 Javascript
在js中实现邮箱格式的验证方法(推荐)
2016/10/24 Javascript
利用select实现年月日三级联动的日期选择效果【推荐】
2016/12/13 Javascript
vuejs实现ready函数加载完之后执行某个函数的方法
2018/08/31 Javascript
浅谈Vue页面级缓存解决方案feb-alive(上)
2019/04/14 Javascript
vue keep-alive 动态删除组件缓存的例子
2019/11/04 Javascript
举例详解Python中循环语句的嵌套使用
2015/05/14 Python
Python ldap实现登录实例代码
2016/09/30 Python
Python 自动刷博客浏览量实例代码
2017/06/14 Python
Python3简单实例计算同花的概率代码
2017/12/06 Python
Python3 sys.argv[ ]用法详解
2019/10/24 Python
Django Form常用功能及代码示例
2020/10/13 Python
美国电视购物:QVC
2017/02/06 全球购物
狗狗玩具、零食和咀嚼物的月度送货服务:Super Chewer
2018/08/22 全球购物
高性能装备提升营地:Kammok
2019/02/27 全球购物
英国索普公园票务和酒店套餐:Thorpe Breaks
2019/09/14 全球购物
PHP面试题及答案二
2015/05/23 面试题
abstract是什么意思
2012/02/12 面试题
公司营业员的自我评价
2014/03/04 职场文书
师范大学生求职信
2014/06/13 职场文书
实体类或对象序列化时,忽略为空属性的操作
2021/06/30 Java/Android
java设计模式--原型模式详解
2021/07/21 Java/Android
Linux安装apache服务器的配置过程
2021/11/27 Servers
本地搭建minio文件服务器(使用bat脚本启动)的方法
2022/07/15 Servers