Question
HELP, I KEEP PRINTING TWO BOARD GAMES IN MY CODE WHEN I ONLY WANT ONE, THIS IS MY CODE SO FAR. To implement a Sudoku
HELP, I KEEP PRINTING TWO BOARD GAMES IN MY CODE WHEN I ONLY WANT ONE, THIS IS MY CODE SO FAR.
To implement a Sudoku game in Java, we are going to use 3 different two-dimensional arrays as follows:
Array #1) Declare and initialize a 9x9 array named initialPuzzle which stores all the initial values presented to the user. A zero in the array indicates that the square is empty and the user needs to guess the value. Any number other than zero will be displayed on screen. The initialPuzzle array is what the user will see at the start of the Sudoku game and whenever s/he decides to reset the puzzle. Below is the initialPuzzle array declaration, which you can copy/paste into your project above public static void main(String[ ] args)
public static int[][] initialPuzzle = {
{9,0,0,8,0,5,0,0,6},
{1,0,3,0,0,0,5,0,4},
{0,6,0,0,2,0,0,7,0},
{0,0,7,1,0,8,6,0,0},
{4,0,0,0,0,0,0,0,9},
{0,0,9,7,0,3,1,0,0},
{0,9,0,0,1,0,0,6,0},
{3,0,2,0,0,0,4,0,7},
{5,0,0,3,0,2,0,0,1}
};
Array #2) Declare and initialize a 9x9 array named solvedPuzzle which stores the solution values (like an answer key). This array will be used to determine whether the player won. Below is the solvedPuzzle array declaration, which you can copy/paste into your project above public static void main(String[ ] args)
public static int[][] solvedPuzzle = {
{9,7,4,8,3,5,2,1,6},
{1,2,3,9,6,7,5,8,4},
{8,6,5,4,2,1,9,7,3},
{2,3,7,1,9,8,6,4,5},
{4,8,1,2,5,6,7,3,9},
{6,5,9,7,4,3,1,2,8},
{7,9,8,5,1,4,3,6,2},
{3,1,2,6,8,9,4,5,7},
{5,4,6,3,7,2,8,9,1}
};
Array #3) Declare and initialize a 9x9 array named workingPuzzle which stores the user input. At the start of the game, the workingPuzzle is filled with the values from the initialPuzzle array.This array will be used to determine whether the player won. Below is the workingPuzzle array declaration, which you can copy/paste into your project above public static void main(String[ ] args)
public static int[][] workingPuzzle = new int[ROWS][COLS];
***Note: ROWS and COLS are two constants you will need to define. Both should have the value 9***
After you have defined all three arrays, you will need to create three static methods:
Method #1) public static void resetPuzzle()
In this method, the workingPuzzle array will be "reset" back to the values in the initialPuzzle array. You can do this by looping through all the elements in the workingPuzzle and setting them equal to the elements in the initialPuzzle. This method will be called when the game first starts and when the user chooses to "Reset Puzzle (R)".
Method #2) public static void printPuzzle()
In this method, the puzzle will be printed to the console with a header for the column numbers "C 1 2 3 4 5 6 7 8 9", followed by a row header "R ---------------------------" and then each row of the workingPuzzle is displayed.
Each row is displayed first with the row number (e.g. 1), then two spaces, then the vertical divider "|", followed by each value in that row of the workingPuzzle. If the value in the workingPuzzle is 1-9, display it on screen. If the value is 0, print a period "." instead of the 0 so that the user will know it's a free space.
This method will be called when the game starts, after the user sets a square (makes a move) and when the user chooses to "Reset Puzzle (R)".
An image of what to print (sample) looks like this:
Method #3) public static boolean gameIsWon()
In this method, all the elements in the workingPuzzle array will be compared to all the elements in the solvedPuzzle array. If any of the elements differ, the method should return false. If all of the elements are the same, the method should return true.
This method will be called after the user sets a square on the board.
Below is a sample transaction of the game play (Set a square (S)):
Below is a sample transaction of winning Sudoku (after setting all the squares correctly):
Below is a sample transaction of the game play (Reset Puzzle (R)):
MY CODE STARTS HERE, I JUST NEED TO PRINT THE BOARD ONCE DURING THE LOOP
public class Sudoku {
public static final int ROWS = 9;
public static final int COLS = 9;
public static final int[][] initialPuzzle = {
{9,0,0,8,0,5,0,0,6},
{1,0,3,0,0,0,5,0,4},
{0,6,0,0,2,0,0,7,0},
{0,0,7,1,0,8,6,0,0},
{4,0,0,0,0,0,0,0,9},
{0,0,9,7,0,3,1,0,0},
{0,9,0,0,1,0,0,6,0},
{3,0,2,0,0,0,4,0,7},
{5,0,0,3,0,2,0,0,1}
};
public static final int[][] solvedPuzzle = {
{9,7,4,8,3,5,2,1,6},
{1,2,3,9,6,7,5,8,4},
{8,6,5,4,2,1,9,7,3},
{2,3,7,1,9,8,6,4,5},
{4,8,1,2,5,6,7,3,9},
{6,5,9,7,4,3,1,2,8},
{7,9,8,5,1,4,3,6,2},
{3,1,2,6,8,9,4,5,7},
{5,4,6,3,7,2,8,9,1}
};
public static void main(String[] args)
{
// Declare the working puzzle
int[][] workingPuzzle = new int[ROWS][COLS];
Scanner consoleScanner = new Scanner(System.in);
//Initialize the workingPuzzle
resetPuzzle(workingPuzzle);
//Print the puzzle(to see it)
do {
printPuzzle(workingPuzzle);
// TODO: 1) Ask the user for input (S set square, R reset, Q quit)
System.out.println("What would you like to do?"
+ " Set a square (S), Reset Puzzle (R) or Quit (Q)");
// 2) ConsoleScanner
String input = consoleScanner.nextLine().toUpperCase();
// 3) Make a switch statement with 3 cases ("S", "R" or "Q")
switch(input)
{
case "S":
System.out.println("Which row (1-9) and column (1-9) do you want to change? ");
int row = consoleScanner.nextInt()-1;
int col = consoleScanner.nextInt()-1;
System.out.println("What should the value (1-9) be?");
int value = consoleScanner.nextInt();
workingPuzzle[row][col]=value;
if (gameIsWon(workingPuzzle))
System.out.println("C*O*N*G*R*A*T*U*L*A*T*I*O*N*S, you WON Sudoku!!!");
break;
case "R":
resetPuzzle(workingPuzzle);
break;
case "Q":
System.exit(0);
break;
}
}
while (!gameIsWon(workingPuzzle));
}
// Initialize the workingPuzzle to the initialPuzzle
public static void resetPuzzle(int[][] workingPuzzle)
{
for (int i=0; i { for (int j = 0; j workingPuzzle[i][j] = initialPuzzle[i][j]; } } //Print the puzzle (formatted) public static void printPuzzle(int[][] workingPuzzle) { //First two lines are always the same System.out.print(" C 1 2 3 4 5 6 7 8 9"); System.out.println(" R --------------------------"); // Loop through all the values in the working puzzle and print them out for (int i = 0; i { System.out.print((i+1)+" | "); for (int j =0; j { if (workingPuzzle[i][j] ==0) System.out.print(". "); else System.out.print(workingPuzzle[i][j]+" "); } System.out.println(" "); } } public static boolean gameIsWon(int[][] workingPuzzle) { //Use nested loops to determine if the workingPuzzle is exactly // the same as the solvedPuzzle for (int i = 0; i { for (int j = 0; j { // If they're the same return true if (workingPuzzle[i][j] != solvedPuzzle[i][j]) return false; } } //If it makes it through all 81 values, they are the same return true; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started