簡單實現(xiàn)python數(shù)獨游戲
網(wǎng)上看到一個python寫的數(shù)獨,很好玩,分享給大家。
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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python去除字符串中的標(biāo)點符號的最優(yōu)方式
在Python編程中,去除字符串標(biāo)點符號是一項常見任務(wù),關(guān)鍵在于文本分析和數(shù)據(jù)清洗,Python提供了多種方法,包括使用str.replace()、str.translate()結(jié)合str.maketrans(),以及使用正則表達式,另外,可以利用string模塊中的punctuation屬性快速實現(xiàn)2024-09-09
淺談python 中的 type(), dtype(), astype()的區(qū)別
這篇文章主要介紹了淺談python 中的 type(), dtype(), astype()的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
解決Jupyter notebook更換主題工具欄被隱藏及添加目錄生成插件問題
這篇文章主要介紹了解決Jupyter notebook更換主題工具欄被隱藏及添加目錄生成插件問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python爬蟲實戰(zhàn)之網(wǎng)易云音樂加密解析附源碼
讀萬卷書不如行萬里路,學(xué)的扎不扎實要通過實戰(zhàn)才能看出來,本篇文章手把手帶你解析網(wǎng)易云音樂數(shù)據(jù),大家可以在實戰(zhàn)過程中更有效的掌握python2021-10-10
Python tempfile模塊學(xué)習(xí)筆記(臨時文件)
這篇文章主要介紹了Python tempfile模塊學(xué)習(xí)筆記,著重講解了模塊下的幾個函數(shù),需要的朋友可以參考下2014-05-05
Python自動化辦公之Excel數(shù)據(jù)的寫入
這篇文章主要為大家詳細介紹一下Python中excel的寫入模塊- xlsxwriter,并利用該模塊實現(xiàn)Excel數(shù)據(jù)的寫入,感興趣的小伙伴可以了解一下2022-05-05
matplotlib命令與格式之tick坐標(biāo)軸日期格式(設(shè)置日期主副刻度)
這篇文章主要介紹了matplotlib命令與格式之tick坐標(biāo)軸日期格式(設(shè)置日期主副刻度),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python使用Webargs實現(xiàn)簡化Web應(yīng)用程序的參數(shù)處理
在開發(fā)Web應(yīng)用程序時,參數(shù)處理是一個常見的任務(wù),Python的Webargs模塊為我們提供了一種簡單而強大的方式來處理這些參數(shù),下面我們就來學(xué)習(xí)一下具體操作吧2024-02-02

