Java - Juego Sudoku

 
Vista:
sin imagen de perfil

Juego Sudoku

Publicado por ismael (1 intervención) el 11/03/2014 23:07:18
he estado de hacer el juego Sudoku en java, tengo casi todo, cree una manera de chequear si los numeros se repiten, los numeros repetidos los destaca en rojo, pero si se repite un numero que el usuario escribio con uno que el programa muestra algunas veces destaca el numero que el programa muestra y no el que el usuario escribio.

Ademas no se como hacer que los datos que se escriban en el TextField sean centrados.

Ahora me encuentro trabajando para poder generar mas de un juego sudoku con la funcion math.random y switch. El programa compila pero al momento de apretar start aparece un error. Las matrices que utilice son solo para probar el programa

Estaria muy agradecido si me pudieran ayudar en este programa


el programa es el siguiente
------------------------------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.awt.event.*;
import java.awt.*;
import java.io.*;
 
public class Sudoku_1 extends Frame{
    private TextField[][] cuadrados=new TextField[9][9];
    private Button start=new Button ("Start");
    private Button check=new Button("Check");
    private Button clear=new Button("Clear");
    private Button exit=new Button("Exit");
    private int[][] numeros=new int[9][9];
    private int ramdom;
    private int auxiliar[][];
 
    public Sudoku_1(){
        setSize(500,500);
        setTitle("Sudoku");
        setLayout(new BorderLayout());
        Panel pan=new Panel();
        pan.setLayout(new GridLayout(9,9));
        add(pan, BorderLayout.CENTER);
        Font myFont = new Font("ARIALBD",Font.BOLD,30);
        for(int a=0;a<9;a++){
            for(int b=0;b<9;b++){
                cuadrados[a][b]=new TextField();
                cuadrados[a][b].setFont(myFont);
                cuadrados[a][b].setBackground(Color.pink);
                cuadrados[a][b].setForeground(Color.RED);
 
                pan.add(cuadrados[a][b]);
                if(a>=0&&a<3){
                    if((b>=0&&b<3)||(b>=6&&b<9)){
                        cuadrados[a][b].setBackground(Color.CYAN);
                    }
                }
                if(a>=3&&a<6){
                    if(b>=3&&b<6){
                        cuadrados[a][b].setBackground(Color.CYAN);
                    }
                }
                if(a>=6&&a<9){
                    if((b>=0&&b<3)||(b>=6&&b<9)){
                        cuadrados[a][b].setBackground(Color.CYAN);
                    }
                }
            }
        }
 
        Panel aux=new Panel();
        aux.setLayout(new FlowLayout());
        aux.add(start);
        aux.add(check);
        aux.add(clear);
        aux.add(exit);
        add(aux, BorderLayout.SOUTH);
 
        start.addActionListener(new EscuchadorStart());
        check.addActionListener(new EscuchadorCheck());
        clear.addActionListener(new EscuchadorClear());
        exit.addActionListener(new EscuchadorExit());
    }
 
    public static void main(String[] args) {
        Sudoku_1 x= new Sudoku_1();
        x.setVisible(true);
 
    }
 
    public class EscuchadorStart implements ActionListener{
        public void actionPerformed(ActionEvent x){
            ramdom=(int)(Math.random()*10+1);
 
            switch(ramdom){
                case(1):{
                    int[][] auxiliar={  {1, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},};
                   break;
                }
                case(2):{
                    int[][] auxiliar={  {2, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},};
                    break;
                }
                default:{
                    int[][] auxiliar={  {3, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},
                                {0, 6, 0, 1, 0, 4, 0, 5, 0},};
                                break;
                }
            }
 
 
            for(int a=0;a<9;a++){
                for(int b=0;b<9;b++){
                    if(auxiliar[a][b]!=0){
                        cuadrados[a][b].setText(""+auxiliar[a][b]);
                        cuadrados[a][b].setEditable(false);
                    }
                    else;
                }
            }
            for(int a=0;a<9;a++){
                for(int b=0;b<9;b++){
                    if(cuadrados[a][b].getText().equals("")){
                        cuadrados[a][b].setForeground(Color.BLUE);
                    }
                    else;
                }
            }
        }
    }
    public class EscuchadorCheck implements ActionListener{
        public void actionPerformed(ActionEvent x){
             for(int a=0;a<9;a++){
                for(int b=0;b<9;b++){
                    if(cuadrados[a][b].getText().equals("")){
                        numeros[a][b]=0;
                    }
                    else numeros[a][b]=Integer.parseInt(cuadrados[a][b].getText());
                }
            }
             for(int a=0;a<9;a++){
                for(int b=0;b<9;b++){
                    if(numeros[a][b]!=0){
                        for(int c=b+1;c<9;c++){
                            if(numeros[a][b]==numeros[a][c]){
                                cuadrados[a][b].setBackground(Color.RED);
                            }
                            else;
                        }
                    }
                }
            }
            for(int b=0;b<9;b++){
                for(int a=0;b<9;a++){
                    if(numeros[a][b]!=0){
                        for(int c=a+1;c<9;c++){
                            if(numeros[a][b]==numeros[c][b]){
                                cuadrados[a][b].setBackground(Color.RED);
                            }
                            else;
                        }
                    }
                }
            }
 
        }
    }
    public class EscuchadorExit implements ActionListener{
        public void actionPerformed(ActionEvent x){
            System.exit(0);
        }
    }
    public class EscuchadorClear implements ActionListener{
        public void actionPerformed(ActionEvent x){
             for(int a=0;a<9;a++){
                for(int b=0;b<9;b++){
                    cuadrados[a][b].setText("");
                }
            }
            EscuchadorStart y=new EscuchadorStart();
            y.actionPerformed(x);
 
            for(int a=0;a<9;a++){
                for(int b=0;b<9;b++){
                    if(a>=0&&a<3){
                        if((b>=0&&b<3)||(b>=6&&b<9)){
                            cuadrados[a][b].setBackground(Color.CYAN);
                        }
                    }
                    if(a>=3&&a<6){
                        if(b>=3&&b<6){
                            cuadrados[a][b].setBackground(Color.CYAN);
                        }
                    }
                    if(a>=6&&a<9){
                        if((b>=0&&b<3)||(b>=6&&b<9)){
                            cuadrados[a][b].setBackground(Color.CYAN);
                        }
                    }
                }
            }
 
        }
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder