简单实现python数独游戏

 更新时间:2018年03月30日 15:28:02   作者:单鹏飞  
这篇文章主要为大家详细介绍了如何简单实现python数独游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

网上看到一个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的pytest框架之命令行参数详解(下)

    python的pytest框架之命令行参数详解(下)

    这篇文章主要介绍了python的pytest框架之命令行参数详解,今天将继续更新其他一些命令选项的使用,和pytest收集测试用例的规则,需要的朋友可以参考下
    2019-06-06
  • python处理大数字的方法

    python处理大数字的方法

    这篇文章主要介绍了python处理大数字的方法,涉及Python递归操作的相关技巧,需要的朋友可以参考下
    2015-05-05
  • Python+Django+MySQL实现基于Web版的增删改查的示例代码

    Python+Django+MySQL实现基于Web版的增删改查的示例代码

    这篇文章主要介绍了Python+Django+MySQL实现基于Web版的增删改查的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Python Numpy 数组的初始化和基本操作

    Python Numpy 数组的初始化和基本操作

    Python 是一种高级的,动态的,多泛型的编程语言。接下来通过本文给大家介绍Python Numpy 数组的初始化和基本操作,感兴趣的朋友一起看看吧
    2018-03-03
  • Python中enumerate函数及其应用详解

    Python中enumerate函数及其应用详解

    在 Python 编程中,enumerate 函数是一个非常实用的工具,它能够将一个可迭代对象组合为一个索引序列,同时列出数据和数据下标,这种功能在处理列表、元组、字符串等可迭代对象时非常有用,尤其是在需要同时获取每个元素的索引和值的情况下,需要的朋友可以参考下
    2025-01-01
  • 基于Python实现将列表数据生成折线图

    基于Python实现将列表数据生成折线图

    这篇文章主要介绍了如何利用Python中的pandas库和matplotlib库,实现将列表数据生成折线图,文中的示例代码简洁易懂,需要的可以参考一下
    2022-03-03
  • 基于Numpy.convolve使用Python实现滑动平均滤波的思路详解

    基于Numpy.convolve使用Python实现滑动平均滤波的思路详解

    这篇文章主要介绍了Python极简实现滑动平均滤波(基于Numpy.convolve)的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • 详解基于K-means的用户画像聚类模型

    详解基于K-means的用户画像聚类模型

    这篇文章主要介绍了基于K-means的用户画像聚类模型,本文中就是使用one-hot思想将不同维度的数据利用字典映射的方式将其转化为数据向量,需要的朋友可以参考下
    2022-05-05
  • python matplotlib实现将图例放在图外

    python matplotlib实现将图例放在图外

    这篇文章主要介绍了python matplotlib实现将图例放在图外,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • python服务器与android客户端socket通信实例

    python服务器与android客户端socket通信实例

    这篇文章主要介绍了python服务器与android客户端socket通信的实现方法,实例形式详细讲述了Python的服务器端实现原理与方法,以及对应的Android客户端实现方法,需要的朋友可以参考下
    2014-11-11

最新评论