Question

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 into your arrays or lists. Please create four lists of 9 x 9 each, and call them S1, S2, S3, and S4 respectively. The complete output of your 4 games must look as follows.

Welcome to play the Sudoku Game Check of "XX"!

1===================================.

Your game 1 is as follows:

123456789

234567891

345678912

456789123

567891234

678912345

789123456

891234567

912345678

Your game 1:

Square 1 has a problem.

Square 2 has a problem.

Square 3 has a problem.

Square 4 has a problem.

Square 5 has a problem.

Square 6 has a problem.

Square 7 has a problem.

Square 8 has a problem.

Square 9 has a problem.

2===================================.

Your game 2 is as follows:

123456789

456789123

789123456

234567891

567891234

891234567

345678912

678912345

912345678

Your game 2:

Congratulations! You won the game.

3==================================.

Your game 3 is as follows:

123456782

456789123

789123456

234567891

567891234

891234567

345678912

678912345

912345678

Your game 3:

Row 1 has a problem.

Column 9 has a problem.

Square 3 has a problem.

4===================================.

Your game 4 is as follows:

123456789

456789123

789123456

234567891

567891234

891234567

345678912

678918345

912345678

Your game 4:

Row 8 has a problem.

Column 6 has a problem.

Square 8 has a problem.

5===================================.

Thank you for playing this Sudoku Game Check of "XX"!

6===================================.

Some programming techniques:

Your main program may have the following code to drive your game 1:

print("Your game 1 is as follows: " )

showGame(S1)   # to print 9x9 game board

print("Your game 1: ")

checkGame(S1) # to check 9 rows/columns/squares

Your checkGame( ) function need to call the following 3 functions:

RowOK(S1, row) to check row: 0 through 8 (meaning 1 through 9 from user’s view).

ColumnOK(S1,column) to check column: 0 through 8 (meaning 1 through 9 from user’s view).

SquareOK(S1, square) to check square: 0 through 8 (meaning 1 through 9 from user’s view).

Your S1 may be defined as follows:     # nice to show row index 0 to 8 and column index 0 to 8

         S1 = [                                            # Game 1

# column index:         0 1 2 3 4 5 6 7 8     

   [1,2,3,4,5,6,7,8,9],    # row index 0

                [2,3,4,5,6,7,8,9,1],    #                  1

                [3,4,5,6,7,8,9,1,2],    #                  2

                  [4,5,6,7,8,9,1,2,3],    #                  3

                [5,6,7,8,9,1,2,3,4],     #                 4

                  [6,7,8,9,1,2,3,4,5],     #                 5

                  [7,8,9,1,2,3,4,5,6],     #                 6

                  [8,9,1,2,3,4,5,6,7],     #                 7

                  [9,1,2,3,4,5,6,7,8]   ] #                 8

How to define Slist1 to be Square 1?

Slist1=[S[0][0],S[0][1],S[0][2],S[1][0],S[1][1],S[1][2],S[2][0],S[2][1],S[2][2]] # Square 1 (red)

How to define Slist9 to be Square 9?

Slist9=[S[6][6],S[6][7],S[6][8],S[7][6],S[7][7],S[7][8],S[8][6],S[8][7],S[8][8]] # Square 9 (purple)

The above hard-coded statements are easier ways to define squares. You may use nested for loops to set up all 9 squares in a function.

Your checkGame( ) function may look as follows:

def checkGame(S): # check game board S for 9 rows, 9 cols, 9 squares

    countBad = 0   # count how many problems being detected

    for r in range(9): # 9 rows check: 0 to 8

        if (not RowOK(S, r) ):

            print("Row ", r+1, " has a problem.")

            countBad += 1

    for c in range(9): # 9 columns check: 0 to 8

        # more coding here ###################

    for q in range(9): # 9 squares check: 0 to 8, actually they mean square 1 to 9

        # more coding here ###################

    if countBad == 0: # perfect game

        print("Congratulations! You won the game.")

Your RowOK( ) function may look as follows:

def RowOK(S, r): # check Row OK

    goodlist = [1,2,3,4,5,6,7,8,9]   # a perfect list of 1 thru 9 sorted order

    slist = S[r]    # get row r: 0 to 8

    clist = [ ]       # We must make a real copy of the original source list

    for element in slist:

        clist.append(element) # make a real copy to avoid side effect to the original 9x9 array

    clist.sort( )    # sort the list before comparing with goodlist

    return (clist == goodlist)    # true means OK since they are equal

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

Given below are screenshoys of code & output:

Code:

- 5 x PS Sign-Language-master [C:\Users\naman\Downloads\Sign-Language-master Sign-Language-master] - ... sudokochecker.py (Si- 5 x ve.py x fe cnn_tf.py i E 1: Project PS Sign-Language-master [C:\Users\naman\Downloads\Sign-Language-master Sign-Languag- 5 x PS Sign-Language-master [C:\Users\naman\Downloads\Sign-Language-master Sign-Language-master] - ... sudokochecker.py (Si

Oitput:

- x PS Sign-Language-master [C:\Users\naman\Downloads\Sign-Language-master Sign-Language-master] - ... sudokochecker.py (Sign- x PS Sign-Language-master [C:\Users\naman\Downloads\Sign-Language-master Sign-Language-master] - ... sudokochecker.py (Sign

Given below is the python code for the same. I have used almost the same procedure as in the template mentioned in question. However instead of separately checking for rows,columns and square grids I created their copies as lists of lists in checkGame method and used a single generalized method OK which checks if the list is valid for sudoku. The output is generated in same format as mentioned in question

NOTE: The pasted code often loses its indentation after being post. In case ,the below code loses indentation please refer to above screenshots for proper indentation.

#function to play sudoku grid

def showGame(S):

for row in S:

for element in row:

print(element,end='')

print()

#check if a given list is OK: generalizing for rows, columns and squares

def OK(list):

goodlist = [1,2,3,4,5,6,7,8,9]

return sorted(list)==goodlist

#function to check board

def checkgame(S):

countBad = 0 # count how many problems being detected

#For Making copies of rows, columns & squares of sudoku

Rows = [[]for i in range(9)]

Columns=[[]for i in range(9)]

Squares=[[]for i in range(9)]

#copying rows

for i in range(9):

for j in range(9):

Rows[i].append(S[i][j])

#copying columns

for i in range(9):

for j in range(9):

Columns[i].append(S[j][i])

#copying squares

for s in range(9):

for i in range(3*(s//3),3*(s//3)+3):

for j in range(3*(s%3),3*(s%3)+3):

Squares[s].append(S[i][j])

#checking if a row is valid

for r in range(9):

if not OK(Rows[r]):

print("Row ",r+1," has a problem.")

countBad+=1

#checking if a column is valid

for c in range(9):

if not OK(Columns[c]):

print("Column ",c+1," has a problem.")

countBad+=1

#checking if a square is valid

for q in range(9):

if not OK(Squares[q]):

print("Square ",q+1," has a problem.")

countBad+=1

# if everything is valid prompt that you have won

if countBad == 0:

print("Congratulations! You won the game.")

#pre-loading S1,S2,S3,S4

S1=[[1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9, 1], [3, 4, 5, 6, 7, 8, 9, 1, 2], [4, 5, 6, 7, 8, 9, 1, 2, 3], [5, 6, 7, 8, 9, 1, 2, 3, 4], [6, 7, 8, 9, 1, 2, 3, 4, 5], [7, 8, 9, 1, 2, 3, 4, 5, 6], [8, 9, 1, 2, 3, 4, 5, 6, 7], [9, 1, 2, 3, 4, 5, 6, 7, 8]]

S2=[[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7, 8, 9, 1], [5, 6, 7, 8, 9, 1, 2, 3, 4], [8, 9, 1, 2, 3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8, 9, 1, 2], [6, 7, 8, 9, 1, 2, 3, 4, 5], [9, 1, 2, 3, 4, 5, 6, 7, 8]]

S3=[[1, 2, 3, 4, 5, 6, 7, 8, 2], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7, 8, 9, 1], [5, 6, 7, 8, 9, 1, 2, 3, 4], [8, 9, 1, 2, 3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8, 9, 1, 2], [6, 7, 8, 9, 1, 2, 3, 4, 5], [9, 1, 2, 3, 4, 5, 6, 7, 8]]

S4=[[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7, 8, 9, 1], [5, 6, 7, 8, 9, 1, 2, 3, 4], [8, 9, 1, 2, 3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8, 9, 1, 2], [6, 7, 8, 9, 1, 8, 3, 4, 5], [9, 1, 2, 3, 4, 5, 6, 7, 8]]

#displaying output for the 4 pre-loaded games

print("Welcome to play the Sudoku Game Check of \"XX\"!")

#GAME 1

print("1=================================================")

print("Your game 1 is as follows:")

showGame(S1)

print("Your game 1:")

checkgame(S1)

#GAME 2

print("2=================================================")

print("Your game 2 is as follows:")

showGame(S2)

print("Your game 2:")

checkgame(S2)

#GAME 3

print("3=================================================")

print("Your game 3 is as follows:")

showGame(S3)

print("Your game 3:")

checkgame(S3)

#GAME 4

print("4=================================================")

print("Your game 4 is as follows:")

showGame(S4)

print("Your game 4:")

checkgame(S4)

print("5=================================================")

print("Thank you for playing this Sudoku Game Check of \"XX\"!")

print("6=================================================")

Add a comment
Know the answer?
Add Answer to:
USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku...
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
  • 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...

  • 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...

  • I'm making a sudoku solver to check if the sudoku grid created is legal. There are...

    I'm making a sudoku solver to check if the sudoku grid created is legal. There are supposed to be no repeated numbers in each row, column, and block. I got the code for the repeated numbers for row and column but confused on how to write the code for the blocks. Here is my code: 134 135 136 / COMPLETE THIS 137 // Returns true if this grid is legal. A grid is legal if no row, column, or //...

  • Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here }...

    Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here } int main() { int sudoku[9][9] = {{7, 3, 5, 6, 1, 4, 8, 9, 2}, {8, 4, 2, 9, 7, 3, 5, 6, 1}, {9, 6, 1, 2, 8, 5, 3, 7, 4}, {2, 8, 6, 3, 4, 9, 1, 5, 7}, {4, 1, 3, 8, 5, 7, 9, 2, 6}, {5, 7, 9, 1, 2, 6, 4, 3, 8}, {1, 5, 7, 4,...

  • C PROGRAMMING You must write a C program to check the validity of a Sudoku solution. Follow the link to know more about...

    C PROGRAMMING You must write a C program to check the validity of a Sudoku solution. Follow the link to know more about Sudoku: https://www.bigfishgames.com/blog/how-to-solve-sudoku-puzzles-quickly-andreliably/ You must at least do the following: 1- Ask the user to provide a minimum of first two rows of the Sudoku grid. For the rest of the entries, you should use random number generator. 2- Use appropriate logic to make sure random number generator generates distinct set of valid integers! 3- It should be...

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • In this assignment, you must write a C program to check the validity of a Sudoku solution. You must at least do the foll...

    In this assignment, you must write a C program to check the validity of a Sudoku solution. You must at least do the following: 1- Ask the user to provide a minimum of first two rows of the Sudoku grid. For the rest of the entries, you should use a random number generator. 2- Use appropriate logic to make sure the random number generator generates a distinct set of valid integers! 3- It should be a console-based, yet convenient and...

  • Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as...

    Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...

  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

    Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...

  • Write a program that reads a matrix from the keyboard and displays the summations of all...

    Write a program that reads a matrix from the keyboard and displays the summations of all its rows on the screen. The size of matrix (i.e. the number of rows and columns) as well as its elements are read from the keyboard. A sample execution of this program is illustrated below: Enter the number of rows of the matrix: 3 Enter the number of columns of the matrix: 4 Enter the element at row 1 and chd umn 1: 1...

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