It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

HELP with Sudoku puzzle!!!

HELP with Sudoku puzzle!!!

Postby EKutarni@student.fsc.edu on Tue Dec 19, 2006 10:36 pm

Hello i am having a bitch of a time with this program, i now i have to come up with an algorithm that checks every blank block and if it has only one possible answer, turn the background green. If it has no possible answers turn the background red......I am drawing a serious blank here people and am about to loose my mind! javascript:emoticon(':x')
Mad
any ideas thoughts etc. would be greatly apppreciated!!!!!!

oh if you are not familiar with the Sudoku game check out this link:
http://www.eddaardvark.co.uk/sudokusolver.html

Here is my code so far:
Code: Select all
import java.applet.Applet;
import java.awt.TextArea;
import java.awt.Color;
import java.awt.TextComponent;
import java.awt.event.TextListener;
import java.awt.event.TextEvent;
import java.awt.GridLayout;

public class Sudoku extends Applet {

   TextArea [][] numbers = new TextArea[9][9];
   public void init(){
        this.setLayout(new GridLayout(9,9));
   for (int i=0; i<numbers.length; i++){
           for (int j=0; j<numbers[i].length; j++){
              numbers[i][j] = new TextArea();
              this.add(numbers[i][j]);
              numbers[i][j].addTextListener(new Performer(i,j));
           }
        }
   }

   class Performer implements TextListener {
      int row;
      int col;
      Performer(int r, int c){
         row = r;
         col = c;
      }
/*
* bogus returns false if there is no problem putting a number
* at a position
*/
      public boolean bogus(int row, int col, String snum){

         // scan row to see if there's another number the same
         for (int i=0; i<numbers[row].length; i++){
            if (i != col && snum.equals(numbers[row][i].getText())){
               return true;
            }

         }
         // scan col to see if there's another number the same
         for (int i=0; i<numbers[row].length; i++){
            if (i != row && snum.equals(numbers[i][col].getText())){
               return true;
            }

         }
         // scan the box to see ...
         int [] istartA = {0,0,0,3,3,3,6,6,6};
         int istart =istartA[row];
         int [] jstartA = {0,0,0,3,3,3,6,6,6};
         int jstart =jstartA[col];
         for (int i=istart; i<=istart+2; i++){
            for (int j=jstart; j<=jstart+2; j++){
               System.out.print("rc="+row+","+col+"ij="+i+","+j);
               System.out.println(" "+snum+","+numbers[i][j].getText());

               if (i!=row && numbers[i][j].getText().equals(snum)){
                 return true;
                 // numbers[row][col].setText("");
               }           
            }
         }

         return false;
      }

      public void textValueChanged(TextEvent t ){
         // get the value which just changed
         String snum = numbers[row][col].getText();

         if (bogus(row,col,snum)){
            numbers[row][col].setBackground(Color.RED);
         }
         

         // don't paint blanks red!
         if (snum.equals("")) {
            numbers[row][col].setBackground(Color.WHITE);
         }
         if (snum.equals(null)){
             
         }
         
         // check every blank block, and
         //if it has only one
         // possible answer, turn the background green.
         //if it has no possible answer
         // turn the background red

      }
   }
}
:x :x
EKutarni@student.fsc.edu
 
Posts: 0
Joined: Tue Dec 19, 2006 9:04 pm

Who is online

Users browsing this forum: No registered users and 1 guest