1 import java.io.*;
2
3 import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
4
5 public class Sudoku
6 {
7 public static void main(String a[]) throws IOException
8 {
9 int vector[] = new int[9];
10 int sudoku[][] = new int[9][9];
11 int juego[][] = new int[9][9];
12 int aleatorio, ok;
13
14 for(int i=0; i<9; i++)
15 {
16 // La variable ok nos dice que con el valor 0 podriamos estar
17 // habilitados para ingresar el valor aleatorio al vector.
18 ok = 0;
19
20 // El nombre int hace un cast o una conversion de un numero
21 // decimal que saca el metodo random de la clase Math.
22 // Cuando multiplico el metodo por 9 lo que hago es limitar
23 // el numero aleatorio entre el [0-8] y enseguida agregamos
24 // el 1 para que el rango ahora quede entre [0-8]+1
25 aleatorio = (int)(Math.random()*9)+1;
26
27 // Este ciclo nos
28 for(int j=0; j<9; j++)
29 {
30 if (aleatorio == vector[j]){
31 ok = 1;
32 break;
33 }
34 }
35
36 if (ok == 0){
37 vector[i] = aleatorio;
38 }
39 else{
40 i--;
41 }
42 }
43
44 int i = 0;
45 int j = -3, jt;
46
47 //Este ciclo nos genera gracias al vector aleatorio principal
48 //toda la matriz aleatoria, como resultado "El Sudoku"
49 while(i<9)
50 {
51 j = j + 3;
52 if(i==3){
53 j=1;
54 }
55
56 if(i==6){
57 j=2;
58 }
59
60 jt = j;
61 int k;
62
63 for(k=0; j<9; j++, k++){
64 sudoku[i][j] = vector[k];
65 }
66
67 for(j=0; j<jt; j++, k++){
68 sudoku[i][j] = vector[k];
69 }
70
71 j = jt;
72 i++;
73 }
74
75 //Preparamos las variables necesarias para la presentacin aleatoria
76 //del sudoku.
77 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
78 int numeros_aVer=82;
79 int x, y, coordenada_x=10, coordenada_y=10, numero_solucion=10;
80
81 //Este ciclo nos permite validar un numero adecuado para mostrar el sudoku
82 //sin resolver.
83 while(numeros_aVer > 81){
84 System.out.print("Cuantos numeros del Sudoku desea ver? ");
85 numeros_aVer = Integer.parseInt(br.readLine());
86 }
87
88 for(i=0; i<numeros_aVer; i++){
89 x = (int)(Math.random()*9);
90 y = (int)(Math.random()*9);
91
92 if(sudoku[x][y] != juego[x][y]){
93 juego[x][y] = sudoku[x][y];
94 }
95 else{i--;}
96 }
97
98 //Aqui se muestra el sudoku sin resolver
99 System.out.println("\n Y Y Y Y Y Y Y Y Y ");
100 System.out.println(" 1 2 3 4 5 6 7 8 9 \n\n -------------------");
101 for(i=0; i<9; i++){
102 System.out.print("X"+(i+1)+" ");
103 for(j=0; j<9; j++){
104 if(juego[i][j] == 0){
105 System.out.print("| ");
106 }
107 else{
108 System.out.print("|"+juego[i][j]);
109 }
110 }System.out.println("|");
111 System.out.println(" -------------------");
112 }System.out.println("\n");
113
114 for(i=0; i<81-numeros_aVer; i++){
115 //Validamos las coordenadas y el numero solucion de dicha coordenada con las
116 //preguntas propuestas.
117 System.out.print("Ingresa la cordenada 'X' donde desea ubicarse: ");
118 coordenada_x = Integer.parseInt(br.readLine());
119
120 System.out.print("Ingresa la cordenada 'Y' donde desea ubicarse: ");
121 coordenada_y = Integer.parseInt(br.readLine());
122
123 System.out.print("Que numero consideras va en dicha coordenada? ");
124 numero_solucion = Integer.parseInt(br.readLine());
125
126 if(numero_solucion == sudoku[coordenada_x-1][coordenada_y-1]){
127 juego[coordenada_x-1][coordenada_y-1] = numero_solucion;
128
129 //Aqui se muestra el sudoku como está siendo resuelto.
130 System.out.println("\n Y Y Y Y Y Y Y Y Y ");
131 System.out.println(" 1 2 3 4 5 6 7 8 9 \n\n -------------------");
132 for(int k=0; k<9; k++){
133 System.out.print("X"+(k+1)+" ");
134 for(j=0; j<9; j++){
135 if(juego[k][j] == 0){
136 System.out.print("| ");
137 }
138 else{
139 System.out.print("|"+juego[k][j]);
140 }
141 }System.out.println("|");
142 System.out.println(" -------------------");
143 }System.out.println("\n");
144 }else{i--;}
145 }
146
147 //Muestra el juego Sudoku resuelto!
148 //Runtime.getRuntime().exec("clear");
149 //Aquí quisiera limpiar pantalla pero no se como!
150 for(i=0; i<9; i++){
151 for(j=0; j<9; j++){
152 System.out.print(" "+sudoku[i][j]);
153 }
154 System.out.println();
155 }
156 }
157 }
