Question

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 your arrays.   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 "Dr. Simon Lin"!

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 "Dr. Simon Lin"!                                    

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

//Java program

public class Sudoku {
  
   //this is main fuction to solve sudoku
   public static void main(String args[]){
      
       int sudoku1[][]={{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}};
  
   int sudoku2[][]={{0,2, 3, 4, 5, 6, 7, 8, 0},
   {0, 5, 6, 7, 8, 9, 0, 2, 3},
   {7, 8, 9, 1, 0, 3, 4, 5, 6},
   {2, 3, 0, 5, 6, 7, 8, 9, 1},
   {5, 6, 7, 8, 9, 0, 2, 3, 4},
   {8, 9, 0, 2, 3, 4, 5, 6, 0},
   {3, 4, 5, 6, 7, 8, 0, 1, 2},
   {0, 7, 8, 9, 1, 0, 3, 4, 5},
   {9, 1, 0, 3, 4, 5, 6, 7, 0}};
  
   int sudoku3[][]={{1,2, 0, 4, 5, 6, 0, 8, 0},
   {0, 5, 6, 7, 8, 0, 1, 2, 3},
   {7, 0, 9, 1, 2, 0, 4, 5, 6},
   {0, 3, 4, 0, 6, 0, 8, 9, 1},
   {5, 6, 7, 0, 9, 1, 2, 0, 4},
   {8, 0, 1, 2, 3, 4, 5, 0, 7},
   {3, 4, 5, 0, 7, 8, 0, 1, 2},
   {6, 7, 0, 9, 1, 2, 3, 0, 5},
   {9, 0, 2, 3, 4, 0, 6, 7, 0}};
  
   int sudoku4[][]={{1,0, 3, 4, 0, 6, 7, 0, 9},
   {0, 5, 0, 7, 8, 0, 1, 0, 3},
   {0, 8, 0, 1, 2, 0, 4, 5, 0},
   {2, 0, 4, 5, 0, 7, 8, 0, 1},
   {0, 6, 7, 0, 9, 1, 0, 3, 4},
   {8, 0, 1, 0, 3, 0, 5, 6, 0},
   {0, 4, 5, 0, 7, 0, 9, 0, 2},
   {6, 0, 8, 0, 1, 0, 3, 4, 0},
   {0, 1, 2, 0, 4, 5, 6, 0, 8}};
  
   System.out.print("Welcome to play the Sudoku real game designed by \"You\"!\n\n");
  
   System.out.print("Game 1 is as follows : \n");
   printgrid(sudoku1);
       if(solveGrid(sudoku1,9)){
           System.out.print("\n\nGame1 winning result is as follow:\n");
       printgrid(sudoku1);}
       else System.out.print("no solution\n");
      
       System.out.print("Game 2 is as follows : \n");
       printgrid(sudoku2);
           if(solveGrid(sudoku2,9)){
               System.out.print("\n\nGame2 winning result is as follow:\n");
           printgrid(sudoku2);}
           else System.out.print("no solution\n");
      
           System.out.print("Game 3 is as follows : \n");
           printgrid(sudoku3);
               if(solveGrid(sudoku3,9)){
                   System.out.print("\n\nGame3 winning result is as follow:\n");
               printgrid(sudoku3);}
               else System.out.print("no solution\n");
      
               System.out.print("Game 4 is as follows : \n");
               printgrid(sudoku4);
                   if(solveGrid(sudoku4,9)){
                       System.out.print("\n\nGame4 winning result is as follow:\n");
                   printgrid(sudoku4);}
                   else System.out.print("no solution\n");
      
                   System.out.print("\nThank you for playing this Sudoku real game designed by \"You\"! ");
      
   }
   //function to print final solution
       public static void printgrid(int grid[][]){
           int i,j;
           for( i=0;i<grid.length;i++){
               for( j=0;j<grid[i].length;j++){
                   if(grid[i][j]==0)System.out.print("_ ");
                   else System.out.print(grid[i][j]+" ");
               }
              
               System.out.println();
           }
       }

       public static boolean isSafe(int[][] Grid,int row, int col,int num)
       {
       for (int d = 0; d < Grid.length; d++)
       {
       if (Grid[row][d] == num)
       {
       return false;
       }
       }
       for (int r = 0; r < Grid.length; r++)
       {

       if (Grid[r][col] == num)
       {
       return false;
       }
       }
       int sqrt = (int) Math.sqrt(Grid.length);
       int boxRowStart = row - row % sqrt;
       int boxColStart = col - col % sqrt;

       for (int r = boxRowStart;
       r < boxRowStart + sqrt; r++)
       {
       for (int d = boxColStart;
       d < boxColStart + sqrt; d++)
       {
       if (Grid[r][d] == num)
       {
       return false;
       }
       }
       }

       // if there is no clash, it's safe
       return true;
       }

public static boolean solveGrid(int[][] Grid, int n)
{
int row = -1;
int col = -1;
boolean isEmpty = true;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (Grid[i][j] == 0)
{
row = i;
col = j;
isEmpty = false;
break;
}
}
if (!isEmpty)
{
break;
}
}

if (isEmpty)
{
return true;
}

for (int num = 1; num <= n; num++)
{
if (isSafe(Grid, row, col, num))
{
Grid[row][col] = num;
if (solveGrid(Grid, n))
{
// print(board, n);
return true;
}
else
{
Grid[row][col] = 0;
}
}
}
return false;
}
  


}
//sample output

<terminated Sudoku [Java Applicationl C:Program FilesJava idk1.8.0 151bin iavaw.exe Welcome to play the Sudoku real game desi

Add a comment
Know the answer?
Add Answer to:
Sudoku Game Real Please write a Java program to play a Sudoku game and show its...
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...

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

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

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order,...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binar...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

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

  • Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not...

    Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree.    Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...

  • Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are n...

    Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree.    Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...

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

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

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