Question

Please write a Python program to play a Sudoku game and show its winning result. This...

Please write a Python program to play a Sudoku game and show its winning result. This is an application program of using 2-dimensional arrays/lists.   Each array/list is for a game board of 9 x 9. Your program must know how to play Sudoku game and complete the game with a winning result.

The following is a sample test, which must be all your test cases. As you can see, you must pre-load the following 4 games into your arrays/lists.   Please create 4 arrays of 9 x 9 each, and call them G1, G2, G3, and G4 for examples.

Welcome to play the Sudoku real game designed by "Your Name"!

Game 1 is as follows:

123456789

4_6789123

7891_3456

234567891

5678912_4

891234567

_45678912

678912345

912345_78

Game 1 winning result is as follows:

123456789

456789123

789123456

234567891

567891234

891234567

345678912

678912345

912345678

Game 2 is as follows:

_2345678_

_56789_23

7891_3456

23_567891

56789_234

89_23456_

345678_12

_7891_345

91_34567_

Game 2 winning result is as follows:

123456789

456789123

789123456

234567891

567891234

891234567

345678912

678912345

912345678

Game 3 is as follows:

12_456_8_

_5678_123

7_912_456

_34_6_891

567_912_4

8_12345_7

345_78_12

67_9123_5

9_234_67_

Game 3 winning result is as follows:

123456789

456789123

789123456

234567891

567891234

891234567

345678912

678912345

912345678

Game 4 is as follows:

1_34_67_9

_5_78_1_3

_8_12_45_

2_45_78_1

_67_91_3_

8_1_3_56_

_45_7_9_2

6_8_1_34_

_12_456_8

Game 4 winning result is as follows:

123456789

456789123

789123456

234567891

567891234

891234567

345678912

678912345

912345678

Thank you for playing this Sudoku real game designed by "Your Name"!      

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# print the Grid
def print_grid(arr):
   for i in range(9):
       for j in range(9):
           print arr[i][j],
       print ('\n')

      
# Function to Find the entry in the Grid that is still not used

def find_empty_location(arr,l):
   for row in range(9):
       for col in range(9):
           if(arr[row][col]==0):
               l[0]=row
               l[1]=col
               return True
   return False

# Returns a boolean which indicates whether any assigned entry
# in the specified row matches the given number.
def used_in_row(arr,row,num):
   for i in range(9):
       if(arr[row][i] == num):
           return True
   return False

# Returns a boolean which indicates whether any assigned entry
# in the specified column matches the given number.
def used_in_col(arr,col,num):
   for i in range(9):
       if(arr[i][col] == num):
           return True
   return False


def used_in_box(arr,row,col,num):
   for i in range(3):
       for j in range(3):
           if(arr[i+row][j+col] == num):
               return True
   return False


# num to the given row,col location.
def check_location_is_safe(arr,row,col,num):
  
   # Check if 'num' is not already placed in current row,
  
   return not used_in_row(arr,row,num) and not used_in_col(arr,col,num) and not used_in_box(arr,row - row%3,col - col%3,num)



def solve_sudoku(arr):
  
   # 'l' is a list variable that keeps the record of row and col in find_empty_location Function     
   l=[0,0]
  
   # If there is no unassigned location, we are done     
   if(not find_empty_location(arr,l)):
       return True
  
   # Assigning list values to row and col that we got from the above Function
   row=l[0]
   col=l[1]
  
   # consider digits 1 to 9
   for num in range(1,10):
      
       # if looks promising
       if(check_location_is_safe(arr,row,col,num)):
          
           # make tentative assignment
           arr[row][col]=num

           # return, if success, ya!
           if(solve_sudoku(arr)):
               return True

           # failure, unmake & try again
           arr[row][col] = 0
          
  
   return False

# Driver main function to test above functions
if __name__=="__main__":
  
   # creating a 2D array for the grid
   grid=[[0 for x in range(9)]for y in range(9)]
  
   # assigning values to the grid
   grid=[[1,2,3,4,5,6,7,8,9],
       [4,0,6,7,8,9,1,2,3],
       [7,8,9,1,0,3,4,5,6],
       [2,3,4,5,6,7,8,9,1],
[5,6,7,8,9,1,2,0,4],
[8,9,1,2,3,4,5,6,7],
[0,4,5,6,7,8,9,1,2],
[6,7,8,9,1,2,3,4,5],
[9,1,2,3,4,5,0,7,8]]
  
   # if success print the grid
   if(solve_sudoku(grid)):
       print_grid(grid)
   else:
       print "No solution exists"

Add a comment
Know the answer?
Add Answer to:
Please write a Python program to play a Sudoku game and show its winning result. This...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Sudoku Game Real Please write a Java program to play a Sudoku game and show its...

    Sudoku Game Real Please write a Java program to play a Sudoku game and show its winning result. This is an application program of using 2-dimensional arrays.   Each array is for a game board of 9 x 9. Your program must know how to play Sudoku game and complete the game with a winning result. The following is a sample test, which must be all your test cases. As you can see, you must pre-load the following 4 games into...

  • USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku...

    USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku game and show its result in detail. This is an application program of using 2-dimensional arrays or lists. Each array or list is a game board of 9 x 9. Your program must check the 9 x 9 game board, and report all the problems among 9 rows, 9 columns, and 9 squares. As you can see, you must pre-load the following four games...

  • In C... Write a program to simulate a pick-5 lottery game. Your program must generate and...

    In C... Write a program to simulate a pick-5 lottery game. Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array. The program prompts the user for: an integer random seed five distinct integers between 1 and 9 (which are stored in another array) The program then compares the two arrays to determine if they are identical. If the two arrays are identical, then the user wins the game. otherwise the program...

  • USE PYTHON ONLY Please write a Python program to let you or the user play the...

    USE PYTHON ONLY Please write a Python program to let you or the user play the game of rolling 2 dice and win/lose money. Initially, you have 100 dollars in your account, and the dealer also has 100 dollars in his/her account. You would be asked to enter a bet to start the game. If your bet is zero, the game would be stopped immediately. Otherwise, dealer would roll 2 dice and get a number from ( random.randint(1, 6) +...

  • – Palindrome Game Please write a Java program to verify whether a given word is a...

    – Palindrome Game Please write a Java program to verify whether a given word is a palindrome. You must stop your program when the user enters ‘@’ character as the word. You may write a recursive program to solve this game, but you don’t have to. You may use a stack and/or a queue to solve this game, but you don’t have to. You must run 3 test cases for your program.   Your test case #1 must look as follows:...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

  • Write a c program that will allow two users to play a tic-tac-toe game. You should...

    Write a c program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assue they know how to play the game). Prompt first player(X) to enter their first move. .Then draw the gameboard showing the move. .Prompt the second player(O) to enter their first move. . Then draw the gameboard showing both moves. And so on...through 9 moves. You will need to:...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...

  • Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember...

    Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember the rules: 1. Rock beats scissors 2. Scissors beats paper 3. Paper beats rock The program should ask the users for their names, then ask them for their picks (rock, paper or scissors). After that, the program should print the winner's name. Note, the players may pick the same thing, in this case the program should say it's tie. when the user input an...

  • Need help with a number guessing game in java 1) You should store prior guessses in...

    Need help with a number guessing game in java 1) You should store prior guessses in a linked list, and the nodes in the list must follow the order of prior guesses. For example, if the prior guesses are 1000, 2111, 3222 in that order, the nodes must follow the same order 2) You should store the candidate numbers also in a linked list, and the nodes must follow the order of numbers (i.e. from the smallest to the largest)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT