All pastes #544872 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-06-06 16:59 UTC
rendered paste body
/*
 *
 * sudokuka
 *
 * Copyright (C) 2007 Laurent RICHARD
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the Licensen.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * Resolution.java
 *
 * Created on 4 juin 2007, 16:49
 *
 */

package sudokuka;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import java.util.ArrayList;

public class Resolution 
    {
 
    public int[] values;
    public int[] solution;
    
    /** Creates a new instance of Resolution */
    public Resolution(JTextField[][] grilleTT) 
        {
	values = new int[81];
	solution = new int[81];

        int compteur=0;
        
        for (int i=0;i<9;i++)
            {
            for (int j=0;j<9;j++)
                {
                try
                    {
                    this.values[compteur]=Integer.parseInt(grilleTT[i][j].getText());
                    compteur++;
                    }
                 catch(Exception e)
                    {
                    this.values[compteur] = 0;
                    compteur++;
                    }
                }
            }

        }
    
    
    protected void afficher (int [] vecteur)
    {
    for (int i=0; i<81; i++)
        {
        System.out.print (vecteur[i]);
        }
    }

    protected int index_debut_ligne (int nombre)
        {
        return (nombre - (nombre%9));
        }

    protected int index_debut_colonne (int nombre)
        {
        return(nombre%9);
        }

    protected int index_debut_bloc (int nombre)
        {
        return((27*(ligne(nombre)/3))+(3*(colonne(nombre)/3)));
        }
    
    protected int ligne (int nombre)
        {
        return (nombre/9);
        }

    protected int colonne (int nombre)
        {
        return(nombre%9);
        }


    protected boolean ok_ligne (int[] grille, int cible, int debut)
        {
        int temp1;
        temp1 = index_debut_ligne(debut);
        for (int i=temp1;i<temp1+9;i++)
            {
            if (grille[i]==cible) 
                {
                return false;
                }
            }
        return true;
        }
    
    protected boolean ok_colonne (int[] grille, int cible, int debut)
        {
        int temp1;
        temp1 = index_debut_colonne(debut);
        for (int i=temp1;i<temp1+81;i=i+9)
            {
            if (grille[i]==cible)
                {
         
                return false;
                }
            
            }
        return true;
        }
    
    protected boolean ok_bloc (int[] grille, int cible, int debut)
        {
        int temp1;
        temp1=index_debut_bloc(debut);
        for (int i=0;i<3;i++)
            {
            for (int j=0;j<3;j++)
                {
                if (grille[temp1+(9*i)+j]==cible) 
                    {
                    return false;
                    }
                }
            }
        return true;
        }

    protected boolean ok (int[] grille, int cible, int debut)
        {
        if (debut==81) return false;
        else return (ok_ligne(grille,cible, debut) && ok_colonne(grille,cible, debut) && ok_bloc(grille,cible, debut)); 
        }
    
    protected int suivant (int[] grille, int cible)
        {
        int temp1;
        temp1=cible;
        while (grille[temp1] != 0)
            {
            if (temp1<80) temp1++;
            else return 81;
            }
        return temp1;
        }
    
    protected ArrayList index_fixes (int[] grille)
        {
        ArrayList monArray = new ArrayList();
        for (int i=0;i<81;i++)
            {
            if (grille[i]>0) monArray.add(i);             
            }
        return (monArray);
        }
    
    protected void solution (int[] grille, JFrame fenetre)
        {
        int index=0;
        int valeur=0;
        ArrayList monArray = new ArrayList();
        monArray = index_fixes(grille);
        
        for (int i=0; i<81;i++)
            {
            this.solution[i]=this.values[i];
            }
        while (index < 81)
            {
            index=suivant(grille,index);
            valeur++;
            
            if (ok(grille,valeur,index))
                {
                grille[index]=valeur;
                valeur=0;
                }
            else
                {
                if (valeur==9)
                    {
                    valeur=0;
                    grille[index]=0;
                    index=index-1;
                    while (grille[index]==9)
                        {
                        valeur=0;
                        grille[index]=0;
                        index=index-1;
                       }
                    while (valeurDansArray(index,monArray))
                        {
                        index=index-1;                        
                        }
                    if (index<0)
                        {
                        JOptionPane.showMessageDialog(fenetre, "Aucune solution possible", "Pas de solution", JOptionPane.ERROR_MESSAGE);
                        return;
                        }
                    if (grille[index]<9)
                        {
                        valeur=grille[index];
                        grille[index]=0;                          
                        }
                    }
                }
            }
        
        }
    
    private boolean valeurDansArray(int valeur, ArrayList aarray)    
        {
        for (int i=0; i<aarray.size();i++)
            {
            if (valeur==aarray.get(i)) return true;
            }
        return false;
        }

}