[Tutor] Sudoku

myles broomes mylesbroomes at hotmail.co.uk
Sun Sep 23 15:34:09 CEST 2012


Me again, I've been sat here for about an hour now staring at this code: # Sudoku
# Sudoku is a logic-based number-placement puzzle
# The objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 sub-grids that compose the grid contains all of the digits from 1 to 9import randomdef new_board():
    """Creates the game board. """
    board=[]
    for i in range(9):
        column=[]
        for j in range(9):
            column.append(i)
        board.append(column)
    return boarddef display_board(board):
    """Displays the game board on screen. """
    for i in range(9):
        for j in range(9):
            rand_num=random.randint(1,9)
            if rand_num not in board[i]:
                board[i][j]=rand_num
            else:
                board[i][j]=' '
            print(board[i][j],"|",end='')
        print()# assign the new board to a variable and display it on the screen
game_board=new_board()
display_board(game_board)
I'm cant figure out how to make it so that each column only has (at most) 1 of each number. I've managed to do it fine for the rows but I'm not sure of how I can do it for the columns. I dont want it to seem like I'm being lazy and just getting you guys to do all the work for me so I'm not necceserily asking for a solution, just advice really.  		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120923/2dee05c1/attachment.html>


More information about the Tutor mailing list