Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Simple Sudoku Question

$
0
0
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);
						board[i][j].addActionListener(new ButtonListener(board[i][j]));
						if(!board[i][j].getText().equals(".")){
							board[i][j].setEnabled(false);
						}
						board[i][j].setBackground(Color.white);
						frame.add(board[i][j]);
					}
				}
			}
			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

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>