简单实现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的Django框架中模板碎片缓存简介
Jul 24 Python
python中函数默认值使用注意点详解
Jun 01 Python
利用python如何在前程无忧高效投递简历
May 07 Python
Python 操作 ElasticSearch的完整代码
Aug 04 Python
如何在VSCode上轻松舒适的配置Python的方法步骤
Oct 28 Python
解决Tensorflow sess.run导致的内存溢出问题
Feb 05 Python
keras load model时出现Missing Layer错误的解决方式
Jun 11 Python
Keras实现DenseNet结构操作
Jul 06 Python
python 牛顿法实现逻辑回归(Logistic Regression)
Oct 15 Python
Numpy中np.random.rand()和np.random.randn() 用法和区别详解
Oct 23 Python
Python 列表反转显示的四种方法
Nov 16 Python
Python爬虫之Selenium多窗口切换的实现
Dec 04 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 session常见问题集锦及解决办法总结
2007/03/18 PHP
PHP伪静态页面函数附使用方法
2008/06/20 PHP
PHP+jquery+CSS制作头像登录窗(仿QQ登陆)
2016/10/20 PHP
thinkPHP简单导入和使用阿里云OSSsdk的方法
2017/03/15 PHP
php实现将二维关联数组转换成字符串的方法详解
2017/07/31 PHP
javascript 处理HTML元素必须避免使用的一种方法
2009/07/30 Javascript
奉献给JavaScript初学者的编写开发的七个细节
2011/01/11 Javascript
基于jquery ui的alert,confirm方案(支持换肤)
2015/04/03 Javascript
JavaScript给按钮绑定点击事件(onclick)的方法
2015/04/07 Javascript
浅谈javascript构造函数与实例化对象
2015/06/22 Javascript
理解JS事件循环
2016/01/07 Javascript
jquery精度计算代码 jquery指定精确小数位
2017/02/06 Javascript
JS库之Waypoints的用法详解
2017/09/13 Javascript
解决Webpack 热部署检测不到文件变化的问题
2018/02/22 Javascript
vue-auto-focus: 控制自动聚焦行为的 vue 指令方法
2018/08/25 Javascript
微信小程序如何获取群聊的openGid以及名称详解
2019/07/17 Javascript
Vue函数式组件的应用实例详解
2019/08/30 Javascript
基于JQuery实现页面定时弹出广告
2020/05/08 jQuery
Vue解决echart在element的tab切换时显示不正确问题
2020/08/03 Javascript
小程序实现点击tab切换左右滑动
2020/11/16 Javascript
Python 多进程并发操作中进程池Pool的实例
2017/11/01 Python
30秒轻松实现TensorFlow物体检测
2018/03/14 Python
Python反转序列的方法实例分析
2018/03/21 Python
python按照多个条件排序的方法
2019/02/08 Python
python三方库之requests的快速上手
2019/03/04 Python
python中的列表与元组的使用
2019/08/08 Python
python如何进行矩阵运算
2020/06/05 Python
CSS3的新特性介绍
2008/10/31 HTML / CSS
采购主管的岗位职责
2013/12/17 职场文书
宿舍卫生检讨书
2014/01/16 职场文书
战略合作意向书范本
2014/04/01 职场文书
2015年全民国防教育日活动总结
2015/03/23 职场文书
JavaScript实现显示和隐藏图片
2021/04/29 Javascript
Pytest中skip skipif跳过用例详解
2021/06/30 Python
PHP使用QR Code生成二维码实例
2021/07/07 PHP
【DOTA2】当街暴打?PSG LGD vs VG - DPC 2022 WINTER TOUR CN
2022/04/02 DOTA