Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JSudokuResolver - JSudokuResolver.java

JSudokuResolver.java

Caricato da: R0gerblack
Scarica il programma completo

  1. /* Program: JSudokuResolver | Class: SudokuResolver
  2.     Copyright (C) 2014  Ruggiero Altini
  3.  
  4.     This program is free software: you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation, either version 3 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. */
  17.  
  18. package com.ithsoft.sudoku;
  19. import java.io.*;
  20.  
  21. /**
  22.  * <b>Classe</b> JSudokuResolver. <br>
  23.  * <p>Questa e' la classe principale del programma. Si occupa di prendere in input la directory di un file,
  24.  * di creare un oggetto {@code Board}, un oggetto {@code Solver} e di mandare in outut la soluzione (se c'è ne una).</p>
  25.  * Manda inoltre in input tutti i push e i pops utilizzati nello stack per arrivare alla soluzione, utile per vedere
  26.  * quanto è complesso il problema.
  27.  * @author      Ruggiero Altini
  28.  * @version     1.0
  29.  */
  30. public class JSudokuResolver
  31. {
  32.         /**
  33.         * Indica la posizione del Sudoku presa in input
  34.         */
  35.         static int[] pos;
  36.         /**
  37.         * Indica la directory del file di testo da leggere
  38.         */
  39.         static String filePath;
  40.        
  41.        
  42.         /**
  43.         * La procedura {@code readFile} si occupa di leggere il file da testo preso in input nel {@code main} e di assegnarlo alla variabile pos.
  44.         * Per fare cio', legge riga per riga il file, lo spezza in char e li converte in integer, assegnandoli man mano a ogni elemento
  45.         * dell'array.
  46.         * @since 1.0
  47.         */
  48.         private static void readFile()
  49.         {
  50.                 try
  51.                 {
  52.                
  53.                         BufferedReader reader = new BufferedReader(new FileReader(filePath));
  54.                         String line;
  55.                         int count = 0;
  56.                         while ((line = reader.readLine()) != null) {
  57.                                 char[] cArray = line.toCharArray();
  58.                                 for(char e : cArray)
  59.                                 {
  60.                                         if(e == '-') e = '0';
  61.                                         pos[count] = Character.getNumericValue(e);
  62.                                         count++;
  63.                                 }// ...
  64.                         }
  65.                 }catch(FileNotFoundException exc)
  66.                 {
  67.                         System.out.println("File non trovato");
  68.                         return;
  69.                 }
  70.                 catch(UnsupportedEncodingException exc)
  71.                 {
  72.                         System.out.println("Errore nella lettura del file");
  73.                         return;
  74.                 }catch(IOException exc)
  75.                 {
  76.                         System.out.println("Errore nella lettura del file");
  77.                         return;
  78.                 }      
  79.         }
  80.  
  81.         /**
  82.         * La procedura {@code outputLicense} manda un breve accenno alla licenza del programma.
  83.         * @since 1.0
  84.         */
  85.         public static void outputLicense()
  86.         {
  87.                     System.out.println("\nSudokuResolver Copyright (C) 2014 Ruggiero Altini");
  88.                         System.out.println("This program comes with ABSOLUTELY NO WARRANTY. ");
  89.                         System.out.println("This is free software, and you are welcome to redistribute it");
  90.                         System.out.println("under certain conditions (see GNU GPL v3). \n");
  91.         }
  92.        
  93.         /**
  94.         * Viene eseguita come prima procedura all'avvio del programma.
  95.         * @param args Non utilizzato
  96.         * @since 1.0
  97.         */
  98.         public static void main(String[] args)
  99.         {
  100.                 outputLicense();
  101.                 pos = new int[81];
  102.                 // Hard-coded board
  103.                 /*int[] pos = {0,6,0,4,0,0,0,0,7,
  104.                                          5,0,0,0,0,0,0,0,1,
  105.                                          0,9,0,0,0,3,0,0,0,
  106.                                          6,0,5,3,2,1,4,8,0,
  107.                                          0,1,0,6,0,7,0,2,0,
  108.                                          0,2,3,5,9,4,1,0,6,
  109.                                          0,0,0,8,0,0,0,3,0,
  110.                                          7,0,0,0,0,0,0,0,5,
  111.                                          1,0,0,0,0,2,0,6,0};*/
  112.                                          
  113.                 System.out.println("Inserisci la directory del file (quit per uscire): ");
  114.                 try{
  115.                         BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
  116.                         filePath = bufferRead.readLine();
  117.                         if(filePath.equals("quit")) return;
  118.                 }
  119.                 catch(IOException e)
  120.                 {
  121.                         e.printStackTrace();
  122.                 }
  123.                 readFile();
  124.                 Board b = new Board(pos);
  125.                 // Dichiara un oggetto di tipo resolver e assegnagli la classe anonima di Board con la posizione caricata
  126.                 // Il parametro false simboleggia la vmode. Se vero, verrà mandata in output la situazione dello stack in ogni momento
  127.                 Resolver r = new Resolver(b, false);
  128.                 System.out.println("Griglia di partenza");
  129.                 System.out.print(r);
  130.                
  131.                 if(r.Solve())
  132.                 {       System.out.println("Griglia risolta");
  133.                         System.out.print(r + "\n");
  134.                         System.out.println("Pushes: " + b.getPushes());
  135.                         System.out.println("Pops: " + b.getPops());
  136.                 }else
  137.                 {
  138.                         System.out.println("Griglia non risolta");
  139.                 }
  140.                
  141.         }
  142. }