/* * * 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;i0) 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