-1

I am a beginner and wrote a small script for playing sudoku

I was able to check for the row and column conditions but i need little help to check for square condition i.e there should be only one unique digit in a 3x3 square

Also i would be grateful if anyone would help me to reduce the size of the conditions and can please explain me how to create a random sudoku board generating random numbers as i have taken a static board in

tf()

Also i want to make GUI version for it so please recommend me a way to do it.

Thanks in advance following is the code link is available here https://drive.google.com/file/d/0B0mzB3WBm-8-VzJua1BLakExajQ/view?usp=sharing

board=[[5,7,3,6,9,4,2,1,8],[9,2,4,7,8,1,3,5,'X'],['X',1,6,3,5,'X',7,9,4],[3,8,5,4,2,7,9,6,1],[1,9,2,5,3,6,8,4,7],[4,6,7,9,1,8,5,2,3],[7,3,1,'X',4,5,6,8,'X'],
['X',5,8,1,7,9,4,3,2],[2,4,9,8,6,3,'X',7,5]]#simple sudoku board for testing
def initiator():#print the board and start redirect to the funtion
    for i in range(0,9):
        print str(board[i])+"\n"
    rc_checker()

def find_xs():#find whether there are any remaining spaces to fill
    count = 0
    for i in range(0,9):
        for j in range(0,9):
            if board[i][j] == 'X':
                count+=1
    if count>0:
        return 1
    else:
        for i in range(0,9):
            print str(board[i])+"\n"
            print "Thanks for playing"
def rc_checker():#checks whether inputted row and column are valid 
    if find_xs()==1:
        for i in range(0,9):
            print str(board[i])+"\n"
        print "Give the row and column"
        r = int(input())
        c = int(input())
        if r<10 and c<10:
            r=r-1
            c-=1
            validator(r,c)


def validator(r,c):#validate whether the field is empty and redirects accordingly
    if board[r][c]=='X':
        print "Enter the value"
        val = int(input())
        if val>0 and val <10:
            tf(r,c,val)
            rc_checker() 
    else:
        print "The feild is not empty please try again"
        rc_checker()
def tf(r,c,val):#checking if the inputted value is repeated in corresponding row or column
    i = 0
    r1 = 0
    c1 = 0

    if val!=board[r][i] and val!=board[r][i+1] and val!=board[r][i+2] and val!=board[r][i+3] and val!=board[r][i+4] and val!=board[r][i+5] and val!=board[r][i+6] and val!=board[r][i+7] and val!=board[r][i+8] and val!=board[r1][c] and val!=board[r1+1][c] and val!=board[r1+2][c] and val!=board[r1+3][c] and val!=board[r1+4][c] and val!=board[r1+5][c] and val!=board[r1+6][c] and val!=board[r1+7][c] and val!=board[r1+8][c]:
        print "Value entered is correct"
        board[r][c]=val#value is set
    else:
        print "Invalid value Retry" 
        board[r][c]='X'


def main():#welcome message
    print "Welcome to sudoku game"
    print "Fill all 'X' with valid input"
    initiator()

main()
RajkumarG
  • 146
  • 13
  • 1
    You have a lot of questions combined here. Try breaking it up into a few different, simple questions. – Jeremy Dec 05 '16 at 19:15

1 Answers1

0

Questions you asked:

1. How to check for squares?

2. How to reduce the size of the conditions?

3. How to generate random numbers?

4. How to make a GUI version?


Answers.

1. and 2.

First, one simple way to check these kind of conditions is:

val = 6
my_list = [2, 4, 3, 1, 'X', 7, 8, 9, 5] # this might be your row for example

if val in my_list:
  print 'Invalid value Retry'
else:
  print 'Value entered is correct'

Depending on the case you have to construct my_list differently:

  • get the list for the row

    r = 3
    my_list = board[r]
    
  • get the list for the column

    c = 4
    my_list = [row[4] for row in board]
    
  • get the list for the square

    # where the squares are numbered as follows:
    # 0 1 2
    # 3 4 5
    # 6 7 8
    
    s = 3 # second row, first column
    r = int(s/3) # r = 1 
    c =  s%3  # c = 0
    
    my_list = [element for row in board[r*3:r*3+3] 
               for element in row[c*3:c*3+3]]
    

Here I used 2 important features:

Make sure to understand them as they are really useful.

3.

You may use the function randint

import random
random.randint(0,8) # random integer in range [0, 8], including both end points.

4.

There are many ways to do it and I don't have enough experience to tell you about. Personally I liked a lot this tutorial. It might be useful, if you want to program a simple game with GUI. Before starting a project with GUI, my advice is to learn classes.

Community
  • 1
  • 1
IIIaxS
  • 18
  • 2
  • Thanks a lot. my 3rd question was that "i want to create a sudoku board for example when you start a game clicking play then you see an incomplete board and then you fill it" so i want to create that initial incomplete board. The rest was fully understood thanks again – RajkumarG Dec 07 '16 at 17:44