简单实现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 相关文章推荐
Linux RedHat下安装Python2.7开发环境
May 20 Python
对Python中的@classmethod用法详解
Apr 21 Python
python 利用pandas将arff文件转csv文件的方法
Feb 12 Python
Python sql注入 过滤字符串的非法字符实例
Apr 03 Python
Python flask框架如何显示图像到web页面
Jun 03 Python
基于SQLAlchemy实现操作MySQL并执行原生sql语句
Jun 10 Python
python能开发游戏吗
Jun 11 Python
Python如何将将模块分割成多个文件
Aug 04 Python
Python Pillow(PIL)库的用法详解
Sep 19 Python
如何基于Python和Flask编写Prometheus监控
Nov 25 Python
Django缓存Cache使用详解
Nov 30 Python
Python实现猜拳与猜数字游戏的方法详解
Apr 06 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 中文处理函数集合
2008/08/27 PHP
openflashchart 2.0 简单案例php版
2012/05/21 PHP
Sublime里直接运行PHP配置方法
2014/11/28 PHP
js substr、substring和slice使用说明小记
2011/09/15 Javascript
Javascript计算两个marker之间的距离(Google Map V3)
2013/04/26 Javascript
jQuery实现的多选框多级联动插件
2014/05/02 Javascript
AMD异步模块定义介绍和Require.js中使用jQuery及jQuery插件的方法
2014/06/06 Javascript
JavaScript中对象property的读取和写入方法介绍
2014/12/30 Javascript
JS JQUERY实现滚动条自动滚到底的方法
2015/01/09 Javascript
es6的数字处理的方法(5个)
2017/03/16 Javascript
用angular实现多选按钮的全选与反选实例代码
2017/05/23 Javascript
基于vue-cli 打包时抽离项目相关配置文件详解
2018/03/07 Javascript
深入剖析Node.js cluster模块
2018/05/23 Javascript
ES6 Array常用扩展的应用实例分析
2019/06/26 Javascript
Node.js API详解之 timer模块用法实例分析
2020/05/07 Javascript
vue3.0 项目搭建和使用流程
2021/03/04 Vue.js
[49:30]DOTA2-DPC中国联赛正赛 Dragon vs Dynasty BO3 第二场 3月4日
2021/03/11 DOTA
讲解python参数和作用域的使用
2013/11/01 Python
用Python进行TCP网络编程的教程
2015/04/29 Python
Python list操作用法总结
2015/11/10 Python
Python集中化管理平台Ansible介绍与YAML简介
2019/06/12 Python
python opencv鼠标事件实现画框圈定目标获取坐标信息
2020/04/18 Python
Python Web框架之Django框架文件上传功能详解
2019/08/16 Python
使用pygame写一个古诗词填空通关游戏
2019/12/03 Python
Python坐标轴操作及设置代码实例
2020/06/04 Python
香港彩色隐形眼镜在线商店:Stunninglens(全球免费送货)
2019/05/10 全球购物
数控专业大学生的自我鉴定
2013/11/13 职场文书
校本教研工作制度
2014/01/22 职场文书
英语教师个人总结
2015/02/09 职场文书
毕业生对母校寄语
2015/02/26 职场文书
雷锋电影观后感
2015/06/10 职场文书
员工手册董事长致辞
2015/07/29 职场文书
小学美术教学反思
2016/02/17 职场文书
Angular CLI发布路径的配置项浅析
2021/03/29 Javascript
python开发实时可视化仪表盘的示例
2021/05/07 Python
MYSQL 的10大经典优化案例场景实战
2021/09/14 MySQL