1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #?using? python 3.3.4 to develop sudoku game ?#?sudoku? game fromrandomimportrandint ?#?generate? board defbuild_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 at boardRow=int(block/3); good=True forbinrange(boardRow*3,(boardRow*3)+3): ifb!=block: ifnum inboard[b][row]: good=False break returngood ## ## 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 at boardCol=block%3; good=True forbin(boardCol,boardCol+3,boardCol+6): ifb!=block: ifnum==board[b][row][col]: good=False break returngood deffill_board(board): # to fill all numbers 1 through 9 fornum inrange(1,10): # for each of the 9 3x3 blocks forblock inrange(len(board)): triedRow=[-1] foundSpot=False foriinrange(3): row=-1 whilerow intriedRow: row=randint(0,2) triedRow.append(row) if" "inboard[block][row]androw_available(block,row,board,num): triedCol=[-1] forjinrange(3): col=-1 whilecol intriedCol: col=randint(0,2) triedCol.append(col) if" "==board[block][row][col]andcol_available(block,row,col,board,num): board[block][row][col]=num foundSpot=True iffoundSpot: break iffoundSpot: break ifnotfoundSpot: print("Never Found a Spot for "+str(num)+" in block "+str(block)) returnboard ?#?display? board defdisplay(board): num=[] foriinboard:?#?block? level forsubI ini:?#?row? forsubsubI insubI:?#?item? num.append(subsubI) print("---------------------------------------") print("| {} | {} | {} || {} | {} | {} || {} | {} | {} |".format(num[0],num[1],num[2],num[9],num[10],num[11],num[18],num[19],num[20])) print("---------------------------------------") print("| {} | {} | {} || {} | {} | {} || {} | {} | {} |".format(num[3],num[4],num[5],num[12],num[13],num[14],num[21],num[22],num[23])) print("---------------------------------------") print("| {} | {} | {} || {} | {} | {} || {} | {} | {} |".format(num[6],num[7],num[8],num[15],num[16],num[17],num[24],num[25],num[26])) print("---------------------------------------") print("---------------------------------------") print("| {} | {} | {} || {} | {} | {} || {} | {} | {} |".format(num[27],num[28],num[29],num[36],num[37],num[38],num[45],num[46],num[47])) print("---------------------------------------") print("| {} | {} | {} || {} | {} | {} || {} | {} | {} |".format(num[30],num[31],num[32],num[39],num[40],num[41],num[48],num[49],num[50])) print("---------------------------------------") print("| {} | {} | {} || {} | {} | {} || {} | {} | {} |".format(num[33],num[34],num[35],num[42],num[43],num[44],num[51],num[52],num[53])) print("---------------------------------------") print("---------------------------------------") print("| {} | {} | {} || {} | {} | {} || {} | {} | {} |".format(num[54],num[55],num[56],num[63],num[64],num[65],num[72],num[73],num[74])) print("---------------------------------------") print("| {} | {} | {} || {} | {} | {} || {} | {} | {} |".format(num[57],num[58],num[59],num[66],num[67],num[68],num[75],num[76],num[77])) print("---------------------------------------") print("| {} | {} | {} || {} | {} | {} || {} | {} | {} |".format(num[60],num[61],num[62],num[69],num[70],num[71],num[78],num[79],num[80])) print("---------------------------------------") ?#?test? board=build_board() board=fill_board(board) display=display(board) |