Sudoku Game using python 3.3.4

Submitted by: 
Language: 
Visitors have accessed this post 3010 times.

#‎using‬ python 3.3.4 to develop sudoku game
‪#‎sudoku‬ game
from random import randint

‪#‎generate‬ board
def build_board():
board=[]
for i in range(9):
block=[[" "," "," "],
[" "," "," "],
[" "," "," "]]
board.append(block)
return board

##
## Ensure no other block in the same row has the value
##
def row_available(block, row, board, num):
# Determine which of the main 3 rows this 3x3 block is at
boardRow = int(block / 3);
good = True
for b in range(boardRow * 3, (boardRow * 3) + 3):
if b != block:
if num in board[b][row]:
good = False
break
return good

##
## Ensure no other block in the same col has the value
##
def col_available(block, row, col, board, num):
# Determine which of the main 3 columns this 3x3 block is at
boardCol = block % 3;
good = True
for b in (boardCol, boardCol + 3, boardCol + 6):
if b != block:
if num == board[b][row][col]:
good = False
break
return good

def fill_board(board):
# to fill all numbers 1 through 9
for num in range(1,10):
# for each of the 9 3x3 blocks
for block in range(len(board)):
triedRow = [-1]
foundSpot = False
for i in range(3):
row = -1
while row in triedRow:
row = randint(0,2)
triedRow.append(row)
if " " in board[block][row] and row_available(block, row, board, num):
triedCol = [-1]
for j in range(3):
col = -1
while col in triedCol:
col = randint(0,2)
triedCol.append(col)
if " " == board[block][row][col] and col_available(block, row, col, board, num):
board[block][row][col] = num
foundSpot = True
if foundSpot:
break
if foundSpot:
break
if not foundSpot:
print("Never Found a Spot for " + str(num) + " in block " + str(block))
return board

‪#‎display‬ board
def display(board):
num=[]
for i in board: ‪#‎block‬ level
for subI in i: ‪#‎row‬
for subsubI in subI: ‪#‎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)


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

I hope you can also give at least a simple description on your post.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.