简单实现python数独游戏

作者:单鹏飞 时间:2023-06-21 15:24:22 

网上看到一个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)

来源:https://blog.csdn.net/Pnfy__Shan/article/details/54970361

标签:python,数独游戏
0
投稿

猜你喜欢

  • django-rest-framework解析请求参数过程详解

    2023-03-26 18:18:00
  • python利用元类和描述器实现ORM模型的详细步骤

    2023-11-13 14:54:12
  • Python解决走迷宫问题算法示例

    2023-04-18 02:14:45
  • python 美化输出信息的实例

    2022-04-15 09:53:54
  • python中利用h5py模块读取h5文件中的主键方法

    2022-07-26 12:02:36
  • 如何设计广告的用户体验?

    2007-12-20 13:12:00
  • Python实用日期时间处理方法汇总

    2022-10-21 03:07:48
  • webpack 打包压缩js和css的方法示例

    2023-07-02 05:18:32
  • 成功安装vscode中go的相关插件(详细教程)

    2024-05-08 10:14:32
  • python中stdout输出不缓存的设置方法

    2023-03-08 10:23:15
  • Python编程求质数实例代码

    2021-12-03 23:17:18
  • js实现axios限制请求队列

    2024-05-10 13:59:31
  • python语言使用技巧分享

    2022-10-24 09:29:38
  • 用mysql做站点时怎样记录未知错误的发生

    2009-01-14 13:16:00
  • 哈工大自然语言处理工具箱之ltp在windows10下的安装使用教程

    2022-02-23 04:17:19
  • Python 通过截图匹配原图中的位置(opencv)实例

    2021-10-06 02:04:44
  • Python中的exec、eval使用实例

    2022-07-05 21:01:41
  • Python做图像处理及视频音频文件分离和合成功能

    2022-07-31 14:58:05
  • 详解python3中socket套接字的编码问题解决

    2023-09-11 20:28:48
  • 关于element-ui中el-form自定义验证(调用后端接口)

    2024-04-27 15:57:00
  • asp之家 网络编程 m.aspxhome.com