موضوعات وبسایت : برنامه نویسی

مطالب نوشته شده توسط علی بجنوردی

سورس کد سودوکو پایتون

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105#?using? python 3.3.4 to develop sudoku game?#?sudoku? gamefromrandomimportrandint?#?generate? boarddefbuild_board():board=[]foriinrange(9):block=[[" "," "," "],[" "," "," "],[" "," "," "]]board.append(block)returnboard#### Ensure no other block in the same row has the value##defrow_available(block,row,board,num):# Determine which of the main 3 rows this 3x3 block is atboardRow=int(block/3);good=Trueforbinrange(boardRow*3,(boardRow*3)+3):ifb!=block:ifnum inboard[b][row]:good=Falsebreakreturngood#### Ensure no other block in the same col has the value##defcol_available(block,row,col,board,num):# Determine which of the main 3 columns this 3x3 block is atboardCol=block%3;good=Trueforbin(boardCol,boardCol+3,boardCol+6):ifb!=block:ifnum==board[b][row][col]:good=Falsebreakreturngooddeffill_board(board):# to fill all numbers 1 through 9fornum inrange(1,10):# for each of the 9 3x3 blocksforblock inrange(len(board)):triedRow=[-1]foundSpot=Falseforiinrange(3):row=-1whilerow intriedRow:row=randint(0,2)triedRow.append(row)if" "inboard[block][row]androw_available(block,row,board,num):triedCol=[-1]forjinrange(3):col=-1whilecol intriedCol:col=randint(0,2)triedCol.append(col)if" "==board[block][row][col]andcol_available(block,row,col,board,num):board[block][row][col]=numfoundSpot=TrueiffoundSpot:breakiffoundSpot:breakifnotfoundSpot:print("Never Found a Spot for "+str(num)+" in block "+str(block))returnboard?#?display? boarddefdisplay(board):num=[]foriinboard:?#?block? levelforsubI ini:?#?row?forsubsubI insubI:?#?item?num.append(subsubI)print("---------------------------------------")print("| {} | {}...

تاریخ 19 شهریور 1400