-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarterFxns.py
40 lines (35 loc) · 1.6 KB
/
starterFxns.py
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
# prettyPrint() prints the board with row and column labels,
# and spaces the board out so that it looks nice
# Input: board; the square 2d game board (of integers) to print
# Output: None; prints the board in a pretty way
def prettyPrint(board):
# print column headings and top border
print("\n 1 2 3 | 4 5 6 | 7 8 9 ")
print(" +-------+-------+-------+")
for i in range(len(board)):
# convert "0" cells to underscores (DEEP COPY!!!)
boardRow = list(board[i])
for j in range(len(boardRow)):
if boardRow[j] == 0:
boardRow[j] = "_"
# fill in the row with the numbers from the board
print( "{} | {} {} {} | {} {} {} | {} {} {} |".format(i + 1,
boardRow[0], boardRow[1], boardRow[2],
boardRow[3], boardRow[4], boardRow[5],
boardRow[6], boardRow[7], boardRow[8]) )
# the middle and last borders of the board
if (i + 1) % 3 == 0:
print(" +-------+-------+-------+")
# savePuzzle() writes the contents a sudoku puzzle out
# to a file in comma separated format
# Input: board; the square 2d puzzle (of integers) to write to a file
# fileName; the name of the file to use for writing to
def savePuzzle(board, fileName):
ofp = open(fileName, "w")
for i in range(len(board)):
rowStr = ""
for j in range(len(board[i])):
rowStr += str(board[i][j]) + ","
# don't write the last comma to the file
ofp.write(rowStr[ : len(rowStr)-1] + "\n")
ofp.close()