Código de Java - Juego de X y O

Imágen de perfil
Val: 19
Ha disminuido su posición en 3 puestos en Java (en relación al último mes)
Gráfica de Java

Juego de X y Ográfica de visualizaciones


Java

Publicado el 23 de Octubre del 2015 por Guajardo (21 códigos)
7.293 visualizaciones desde el 23 de Octubre del 2015
Juego de X y O para Windows en JAVA, espero les guste y les sean útil las funciones y manejo de opciones, saludos

Requerimientos

Eclipce

1.0
estrellaestrellaestrellaestrellaestrella(2)

Publicado el 23 de Octubre del 2015gráfica de visualizaciones de la versión: 1.0
7.294 visualizaciones desde el 23 de Octubre del 2015
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

11
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package pack;
 
import java.awt.Button;
import java.awt.Color;
import java.awt.EventQueue;
 
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
 
public class juego {
	int opcion[]=new int[9];
	 String texto, x="x",o="o";
	private JFrame frame;
 
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
 
			public void run() {
				try {
 
					juego window = new juego();
					window.frame.setVisible(true);
 
 
 
 
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
 
	/**
	 * Create the application.
	 */
	public juego() {
		initialize();
	}
 
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		JButton bt1 = new JButton("");
		bt1.setFont(new Font("Tahoma", Font.BOLD, 25));
		JButton bt2 = new JButton("");
		bt2.setFont(new Font("Tahoma", Font.BOLD, 25));
		JButton bt3 = new JButton("");
		bt3.setFont(new Font("Tahoma", Font.BOLD, 25));
		JButton bt4 = new JButton("");
		bt4.setFont(new Font("Tahoma", Font.BOLD, 25));
		JButton bt5 = new JButton("");
		bt5.setFont(new Font("Tahoma", Font.BOLD, 25));
		JButton bt6 = new JButton("");
		bt6.setFont(new Font("Tahoma", Font.BOLD, 25));
		JButton bt7 = new JButton("");
		bt7.setFont(new Font("Tahoma", Font.BOLD, 25));
		JButton bt8 = new JButton("");
		bt8.setFont(new Font("Tahoma", Font.BOLD, 25));
		JButton bt9 = new JButton("");
		bt9.setFont(new Font("Tahoma", Font.BOLD, 25));
 
 
 
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 398, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setLocationRelativeTo(null);
 
		bt1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
 
 
					while (true) {
						texto= JOptionPane.showInputDialog("Introduzca X , O ?: ").toString().toLowerCase();
						if (texto.equals(x)) {
					         //   System.out.print(" son iguales en X");
					            bt1.setText(texto);
					            opcion[0]=1;
					            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
					            break;
						  }
						 if (texto.equals(o)) {
					          //  System.out.print(" son iguales en O");
					            bt1.setText(texto);
					            opcion[0]=2;
					            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
					            break;
					      }
 
					}
 
					desactiva(bt1);
			}
		});
		bt1.setBounds(23, 28, 89, 65);
		frame.getContentPane().add(bt1);
 
 
		bt2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desactiva(bt2);
				while (true) {
					texto= JOptionPane.showInputDialog("Introduzca X , O ?: ").toString().toLowerCase();
					if (texto.equals(x)) {
				         //   System.out.print(" son iguales en X");
				            bt2.setText(texto);
				            opcion[1]=1;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
 
				            break;
					  }
					 if (texto.equals(o)) {
				          //  System.out.print(" son iguales en O");
				            bt2.setText(texto);
				            opcion[1]=2;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
				      }
 
				}
 
			}
		});
		bt2.setBounds(152, 28, 89, 65);
		frame.getContentPane().add(bt2);
 
		bt3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desactiva(bt3);
				while (true) {
					texto= JOptionPane.showInputDialog("Introduzca X , O ?: ").toString().toLowerCase();
					if (texto.equals(x)) {
				         //   System.out.print(" son iguales en X");
				            bt3.setText(texto);
				            opcion[2]=1;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            bt3.setEnabled(false);
				            break;
					  }
					 if (texto.equals(o)) {
				          //  System.out.print(" son iguales en O");
				            bt3.setText(texto);
				            opcion[2]=2;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
				      }
 
				}
 
			}
		});
		bt3.setBounds(268, 28, 89, 65);
		frame.getContentPane().add(bt3);
 
		bt4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desactiva(bt4);
				while (true) {
					texto= JOptionPane.showInputDialog("Introduzca X , O ?: ").toString().toLowerCase();
					if (texto.equals(x)) {
				         //   System.out.print(" son iguales en X");
				            bt4.setText(texto);
				            opcion[3]=1;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
					  }
					 if (texto.equals(o)) {
				          //  System.out.print(" son iguales en O");
				            bt4.setText(texto);
				            opcion[3]=2;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
				      }
 
				}
 
			}
		});
		bt4.setBounds(23, 104, 89, 65);
		frame.getContentPane().add(bt4);
 
		bt5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desactiva(bt5);
				while (true) {
					texto= JOptionPane.showInputDialog("Introduzca X , O ?: ").toString().toLowerCase();
					if (texto.equals(x)) {
				         //   System.out.print(" son iguales en X");
				            bt5.setText(texto);
				            opcion[4]=1;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
					  }
					 if (texto.equals(o)) {
				          //  System.out.print(" son iguales en O");
				            bt5.setText(texto);
				            opcion[4]=2;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
				      }
 
				}
 
			}
		});
		bt5.setBounds(152, 104, 89, 65);
		frame.getContentPane().add(bt5);
 
		bt6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desactiva(bt6);
				while (true) {
					texto= JOptionPane.showInputDialog("Introduzca X , O ?: ").toString().toLowerCase();
					if (texto.equals(x)) {
				         //   System.out.print(" son iguales en X");
				            bt6.setText(texto);
				            opcion[5]=1;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
					  }
					 if (texto.equals(o)) {
				          //  System.out.print(" son iguales en O");
				            bt6.setText(texto);
				            opcion[5]=2;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
				      }
 
				}
 
			}
		});
		bt6.setBounds(268, 104, 89, 65);
		frame.getContentPane().add(bt6);
 
		bt7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desactiva(bt7);
				while (true) {
					texto= JOptionPane.showInputDialog("Introduzca X , O ?: ").toString().toLowerCase();
					if (texto.equals(x)) {
				         //   System.out.print(" son iguales en X");
				            bt7.setText(texto);
				            opcion[6]=1;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
					  }
					 if (texto.equals(o)) {
				          //  System.out.print(" son iguales en O");
				            bt7.setText(texto);
				            opcion[6]=2;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
				      }
 
				}
 
			}
		});
		bt7.setBounds(23, 181, 89, 70);
		frame.getContentPane().add(bt7);
 
		bt8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desactiva(bt8);
				while (true) {
					texto= JOptionPane.showInputDialog("Introduzca X , O ?: ").toString().toLowerCase();
					if (texto.equals(x)) {
				         //   System.out.print(" son iguales en X");
				            bt8.setText(texto);
				            opcion[7]=1;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
					  }
					 if (texto.equals(o)) {
				          //  System.out.print(" son iguales en O");
				            bt8.setText(texto);
				            opcion[7]=2;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
				      }
 
				}
 
			}
		});
		bt8.setBounds(152, 180, 89, 71);
		frame.getContentPane().add(bt8);
 
		bt9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desactiva(bt9);
				while (true) {
					texto= JOptionPane.showInputDialog("Introduzca X , O ?: ").toString().toLowerCase();
					if (texto.equals(x)) {
				         //   System.out.print(" son iguales en X");
				            bt9.setText(texto);
				            opcion[8]=1;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
					  }
					 if (texto.equals(o)) {
				          //  System.out.print(" son iguales en O");
				            bt9.setText(texto);
				            opcion[8]=2;
				            validar(bt1, bt2, bt3, bt4, bt5, bt6,bt7, bt8, bt9);
				            break;
				      }
 
				}
 
			}
		});
		bt9.setBounds(268, 180, 89, 71);
		frame.getContentPane().add(bt9);
	}
 
 
	void validar(JButton bt1,JButton bt2,JButton bt3,JButton bt4,JButton bt5,JButton bt6,JButton bt7,JButton bt8,JButton bt9){
	 //Ganar en X
			if(opcion[0]==1 && opcion[1]==1 && opcion[2]==1){JOptionPane.showMessageDialog(null,"Ganaste maldito X");BotonColorRed(bt1, bt2, bt3); }
			if(opcion[0]==1 && opcion[4]==1 && opcion[8]==1){JOptionPane.showMessageDialog(null,"Ganaste maldito X");BotonColorRed(bt1, bt5, bt9); }
			if(opcion[0]==1 && opcion[3]==1 && opcion[6]==1){JOptionPane.showMessageDialog(null,"Ganaste maldito X");BotonColorRed(bt1, bt4, bt7); }
 
			if(opcion[1]==1 && opcion[4]==1 && opcion[7]==1){JOptionPane.showMessageDialog(null,"Ganaste maldito X");BotonColorRed(bt2, bt5, bt8); }
 
			if(opcion[2]==1 && opcion[4]==1 && opcion[6]==1){JOptionPane.showMessageDialog(null,"Ganaste maldito X");BotonColorRed(bt3, bt5, bt7); }
			if(opcion[2]==1 && opcion[5]==1 && opcion[8]==1){JOptionPane.showMessageDialog(null,"Ganaste maldito X");BotonColorRed(bt3, bt6, bt9); }
 
			if(opcion[3]==1 && opcion[4]==1 && opcion[6]==1){JOptionPane.showMessageDialog(null,"Ganaste maldito X");BotonColorRed(bt4, bt5, bt6); }
 
			if(opcion[6]==1 && opcion[7]==1 && opcion[8]==1){JOptionPane.showMessageDialog(null,"Ganaste maldito X");BotonColorRed(bt7, bt8, bt9); }
 
   //Ganar en O		
			if(opcion[0]==2 && opcion[1]==2 && opcion[2]==2){JOptionPane.showMessageDialog(null,"Ganaste maldito O");BotonColorGreen(bt1, bt2, bt3); }
			if(opcion[0]==2 && opcion[4]==2 && opcion[8]==2){JOptionPane.showMessageDialog(null,"Ganaste maldito O");BotonColorGreen(bt1, bt5, bt9); }
			if(opcion[0]==2 && opcion[3]==2 && opcion[6]==2){JOptionPane.showMessageDialog(null,"Ganaste maldito O");BotonColorGreen(bt1, bt4, bt7); }
 
			if(opcion[1]==2 && opcion[4]==2 && opcion[7]==2){JOptionPane.showMessageDialog(null,"Ganaste maldito O");BotonColorGreen(bt2, bt5, bt8); }
 
			if(opcion[2]==2 && opcion[4]==2 && opcion[6]==2){JOptionPane.showMessageDialog(null,"Ganaste maldito O");BotonColorGreen(bt3, bt5, bt7); }
			if(opcion[2]==2 && opcion[5]==2 && opcion[8]==2){JOptionPane.showMessageDialog(null,"Ganaste maldito O");BotonColorGreen(bt3, bt6, bt9); }
 
			if(opcion[3]==2 && opcion[4]==2 && opcion[6]==2){JOptionPane.showMessageDialog(null,"Ganaste maldito O");BotonColorGreen(bt4, bt5, bt6); }
 
			if(opcion[6]==2 && opcion[7]==2 && opcion[8]==2){JOptionPane.showMessageDialog(null,"Ganaste maldito O");BotonColorGreen(bt7, bt8, bt9); }
 
		}
 
	void desactiva(JButton bt){
		  bt.setEnabled(false);
	}
	void BotonColorRed(JButton bt1,JButton bt2,JButton bt3){
		bt1.setBackground(Color.RED);
		bt2.setBackground(Color.RED);
		bt3.setBackground(Color.RED);
	}
	void BotonColorGreen(JButton bt1,JButton bt2,JButton bt3){
		bt1.setBackground(Color.GREEN);
		bt2.setBackground(Color.GREEN);
		bt3.setBackground(Color.GREEN);
	}
}



Comentarios sobre la versión: 1.0 (2)

Imágen de perfil
28 de Octubre del 2015
estrellaestrellaestrellaestrellaestrella
Muchas gracias
Responder
Imágen de perfil
29 de Octubre del 2015
estrellaestrellaestrellaestrellaestrella
por nada , saludos
Responder

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s3317