# Simple Sudoku Question

Page 1 of 1

## 3 Replies - 5857 Views - Last Post: 15 December 2012 - 12:26 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303995&amp;s=c2b03319f0508c5fa43dc1fc6b8542b4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bteeple

Reputation: 0
• Posts: 33
• Joined: 01-May 11

# Simple Sudoku Question

Posted 14 December 2012 - 09:17 PM

Hello. As for practice for exams coming up, I have decided to take a lab out of the book and try it. It basically is creating a simple Sudoku game. So far I have it so it sets up the interface with a configured text file for the lay out.

Essentially what I need it to do is:

-If the number is an illegal move, the box turns red. If all else, it stays white.
-If the number is in the same column/row as the same number (two 4's in the same row/column), they both will turn red.
-If the board is completed and everything box has a valid number, the entire thing will turn green.

```import javax.swing.*;

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class Game {

// properties
protected JButton [][] board = new JButton[9][9];

/**
* Set up GUI.
*/
public Game() {

try{
File initial = new File("initial.txt");
Scanner x = new Scanner(initial);

JFrame frame = new JFrame();
frame.setLayout(new GridLayout(9,9,2,2));
Container window = frame.getContentPane();
window.setBackground(Color.black);

for(int i = 0; i < board.length; i++){
for(int j = 0; j < board.length; j++){
if(x.hasNext()){
board[i][j] = new JButton(x.next());
board[i][j].setVisible(true);
if(!board[i][j].getText().equals(".")){
board[i][j].setEnabled(false);
}
board[i][j].setBackground(Color.white);
}
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
catch(Exception e){
try {
throw e;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

} // end of constructor
public static void Red(){ //Set the background of the tile to red.

}
public static void Green(){ //Set the board to green.

}

public class ButtonListener implements ActionListener{ //Create the listeners for the buttons.

JButton x = null;

public ButtonListener(JButton board){
x = board;
}

public void actionPerformed(ActionEvent event) {

if(x.getText().equals(".")){
x.setText("1");
}
else if(x.getText().equals("1")){
x.setText("2");
}
else if(x.getText().equals("2")){
x.setText("3");
}
else if(x.getText().equals("3")){
x.setText("4");
}
else if(x.getText().equals("4")){
x.setText("5");
}
else if(x.getText().equals("5")){
x.setText("6");
}
else if(x.getText().equals("6")){
x.setText("7");
}
else if(x.getText().equals("7")){
x.setText("8");
}
else if(x.getText().equals("8")){
x.setText("9");
}
else if(x.getText().equals("9")){
x.setText(".");
}

}
}

} // end of Game class

```

```import javax.swing.UIManager;

/**
* Set look and feel and start up game.
*
*/
public class Start {

/**
* Set look and feel, create Game object
*
* @param args Unused.
*/
public static void main(String [] args) {

try {
// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) {
System.out.println("Look and Feel error");
System.exit(1);
}
new Game();
} // end of main method

} // end of Start class

```

```5 3 . . 7 . . . .
6 . . 1 9 5 . . .
. 9 8 . . . . 6 .
8 . . . 6 . . . 3
4 . . 8 . 3 . . 1
7 . . . 2 . . . 6
. 6 . . . . 2 8 .
. . . 4 1 9 . . 5
. . . . 8 . . 7 9
```

So basically after you have all of those, you should be able to open my project. The last set of code goes into a text file and gets uploaded via my Game method to be the default background for my JButtons.

Now I created my two methods to turn the buttons red and green. My question is, what would be the best way to go about testing for those two?

My solutions:

To turn the tile red I would -

* Test the entire row to see if the same number is in it. (via a double for loop)
* Test the entire 3X3 block to see if the same number is in it. (i am assuming something similar to above)
* Test the entire column to see if the same number is in it. (i am also assuming something similar to above, except limit where the loops check and how far they check)

To turn the tiles green I would -

*Check if there are no red tiles.

So basically I have a general understanding of how I should go about it. But I am unsure if that would be the best move or if it is redundant or I am over working the program. Any help is much appreciated. Thanks.

~Blake

Is This A Good Question/Topic? 0

## Replies To: Simple Sudoku Question

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Simple Sudoku Question

Posted 14 December 2012 - 10:46 PM

Who who who.... what an horror !!!

try/catch are for handling unexpected events NOT bad coding practice

A good Sudoku game should have a class to define Cell
then a Whatever class that holds 9 Cell. These can be a Column a Row or a Region

You do not want to write a method to check for duplicate in Column, Row, Region
write a method that check duplicate in Whatever

So a Sudoku game will have 27 Whatever that represent 9 Column, 9 Row, 9 Region all of them having a different diustribution of the same 81 Cell

Happy coding

### #3 baavgai

• Dreaming Coder

Reputation: 7199
• Posts: 15,008
• Joined: 16-October 07

## Re: Simple Sudoku Question

Posted 15 December 2012 - 05:01 AM

Get rid of the static.

While having a completely non GUI layer for board is preferred, you can do what you're doing. However, I'd extend JButton and give it a few more brains.

### #4 bteeple

Reputation: 0
• Posts: 33
• Joined: 01-May 11

## Re: Simple Sudoku Question

Posted 15 December 2012 - 12:26 PM

pbl, on 14 December 2012 - 10:46 PM, said:

Who who who.... what an horror !!!

try/catch are for handling unexpected events NOT bad coding practice

A good Sudoku game should have a class to define Cell
then a Whatever class that holds 9 Cell. These can be a Column a Row or a Region

You do not want to write a method to check for duplicate in Column, Row, Region
write a method that check duplicate in Whatever

So a Sudoku game will have 27 Whatever that represent 9 Column, 9 Row, 9 Region all of them having a different diustribution of the same 81 Cell

Happy coding

Alright so basically what I am understanding from you is that this is a mess. And that I need to create different methods for each row? Why would that benefit me more than what I already have?

baavgai, on 15 December 2012 - 05:01 AM, said:

Get rid of the static.

While having a completely non GUI layer for board is preferred, you can do what you're doing. However, I'd extend JButton and give it a few more brains.

I am assuming you mean the static on my Red() and Green() methods?