简单实现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中的匿名函数使用简介
Apr 27 Python
Python利用字典将两个通讯录文本合并为一个文本实例
Jan 16 Python
python使用Pycharm创建一个Django项目
Mar 05 Python
python 爬虫 批量获取代理ip的实例代码
May 22 Python
python中pika模块问题的深入探究
Oct 13 Python
Python向excel中写入数据的方法
May 05 Python
python实现图片压缩代码实例
Aug 12 Python
python实现五子棋游戏(pygame版)
Jan 19 Python
Python使用configparser库读取配置文件
Feb 22 Python
简单了解Python变量作用域正确使用方法
Jun 12 Python
python GUI模拟实现计算器
Jun 22 Python
Python通过len函数返回对象长度
Oct 22 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
re0第二季蕾姆被制作组打入冷宫!艾米莉亚女主扶正,原因唏嘘
2020/04/02 日漫
解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)
2013/07/03 PHP
php防注入及开发安全详细解析
2013/08/09 PHP
php中利用str_pad函数生成数字递增形式的产品编号
2013/09/30 PHP
php取出数组单个值的方法
2018/03/12 PHP
jQuery 技巧大全(新手入门篇)
2009/05/12 Javascript
JQuery index()方法使用代码
2010/06/02 Javascript
JS+CSS 制作的超级简单的下拉菜单附图
2013/11/22 Javascript
jquery中attr和prop的区别分析
2015/03/16 Javascript
JS实现模拟风力的雪花飘落效果
2015/05/13 Javascript
js实现的奥运倒计时时钟效果代码
2015/12/09 Javascript
Javascript数组Array方法解读
2016/03/13 Javascript
第一次接触神奇的Bootstrap菜单和导航
2016/08/01 Javascript
浅谈jQuery this和$(this)的区别及获取$(this)子元素对象的方法
2016/11/29 Javascript
jquery判断页面网址是否有效的两种方法
2016/12/11 Javascript
javascript 跨域问题以及解决办法
2017/07/17 Javascript
Vue.js表单标签中的单选按钮、复选按钮和下拉列表的取值问题
2017/11/22 Javascript
[03:48]大碗DOTA
2019/07/25 DOTA
[01:10:48]完美世界DOTA2联赛PWL S2 GXR vs PXG 第一场 11.18
2020/11/18 DOTA
python编写简单爬虫资料汇总
2016/03/22 Python
Python设计模式之门面模式简单示例
2018/01/09 Python
python 自定义异常和异常捕捉的方法
2018/10/18 Python
基于python的socket实现单机五子棋到双人对战
2020/03/24 Python
Django错误:TypeError at / 'bool' object is not callable解决
2019/08/16 Python
pandas实现DataFrame显示最大行列,不省略显示实例
2019/12/26 Python
Django中的AutoField字段使用
2020/05/18 Python
关于keras中keras.layers.merge的用法说明
2020/05/23 Python
简单了解Django项目应用创建过程
2020/07/06 Python
python 实现数据库中数据添加、查询与更新的示例代码
2020/12/07 Python
森海塞尔美国官网:Sennheiser耳机与耳麦
2017/07/19 全球购物
哈萨克斯坦移动和数字技术在线商店:SatelOnline.kz
2020/09/04 全球购物
教学实习自我评价
2014/01/28 职场文书
战友聚会主持词
2014/04/02 职场文书
股权转让协议范本
2014/12/07 职场文书
学长教您写论文:经验总结
2019/07/09 职场文书
PyTorch 实现L2正则化以及Dropout的操作
2021/05/27 Python