0% encontró este documento útil (0 votos)
44 vistas6 páginas

Código Java MICHI

Descargar como docx, pdf o txt
Descargar como docx, pdf o txt
Descargar como docx, pdf o txt
Está en la página 1/ 6

Ingeniera de Sistemas Fundamentos de Programacin

Cdigo Java Michi

package michi;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Michi extends JFrame implements ActionListener{

JButton iniciar ;
JButton tablero [][];
String Jugador1, Jugador2;
int turno=-1;
JLabel mensaje;
Color colorB;

public Michi(){
this.setLayout(null);
mensaje = new JLabel("Bienvenidos al Juego");
mensaje.setBounds(150,40,200,30);
this.add(mensaje);
iniciar = new JButton("Iniciar Juego");
iniciar.setBounds(175,350,150,30);
iniciar.addActionListener(this);

1
Ingeniera de Sistemas Fundamentos de Programacin

this.add(iniciar);
tablero = new JButton [3][3];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
tablero[i][j] = new JButton();
tablero[i][j].setBounds((i+1)*80+50,(j+1)*80,80,80);
this.add(tablero[i][j]);
tablero[i][j].addActionListener(this);
}
}
colorB = tablero[0][0].getBackground();
}

public static void main(String[] args) {


Michi ventana = new Michi ();
ventana.setDefaultCloseOperation(3);
ventana.setSize(500, 550);
ventana.setLocationRelativeTo(null);
ventana.setResizable(false);
ventana.setTitle("Juego del Michi por TonyStyle");
ventana.setVisible(true);

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==iniciar){
turno = 0;
JOptionPane.showMessageDialog(this,"Iniciandoel juego...");

2
Ingeniera de Sistemas Fundamentos de Programacin

Jugador1 = JOptionPane.showInputDialog(this,"Escribe el nombre del primer


Jugador");
Jugador2 = JOptionPane.showInputDialog(this,"Escribe el nombre del segundo
Jugador");
mensaje.setText("Turno del Jugador " + Jugador1);
limpiar();
}else{
JButton boton = (JButton) e.getSource();
if(turno == 0){
if(boton.getText().equals("")){
boton.setBackground(Color.LIGHT_GRAY);
boton.setText("X");
boton.setEnabled(false);
turno = 1;
mensaje.setText("Turno del Jugador " + Jugador2);
}
}else{
if(turno==1){
if(boton.getText().equals("")){
boton.setBackground(Color.white);
boton.setText("O");
boton.setEnabled(false);
turno = 0;
mensaje.setText("Turno del Jugador " + Jugador1);

}
}
}
verificar();
}

3
Ingeniera de Sistemas Fundamentos de Programacin

}
//Cdigo para verificar el ganador de las filas, columnas y diagonales
public void verificar(){
int ganador = 0;
for (int i = 0; i < 3; i++){
if(tablero[0][i].getText().equals("X") && tablero[1][i].getText().equals("X")
&& tablero[2][i].getText().equals("X")){
ganador = 1;
}
if(tablero[i][0].getText().equals("X") && tablero[i][1].getText().equals("X")
&& tablero[i][2].getText().equals("X")){
ganador = 1;
}
}
if(tablero[0][0].getText().equals("X") && tablero[1][1].getText().equals("X")
&& tablero[2][2].getText().equals("X")){
ganador = 1;
}
if(tablero[0][2].getText().equals("X") && tablero[1][1].getText().equals("X")
&& tablero[2][0].getText().equals("X")){
ganador = 1;
}
for (int i = 0; i < 3; i++){
if(tablero[0][i].getText().equals("O") && tablero[1][i].getText().equals("O")
&& tablero[2][i].getText().equals("O")){
ganador = 2;
}
if(tablero[i][0].getText().equals("O") && tablero[i][1].getText().equals("O")
&& tablero[i][2].getText().equals("O")){

4
Ingeniera de Sistemas Fundamentos de Programacin

ganador = 2;
}
}
if(tablero[0][0].getText().equals("O") && tablero[1][1].getText().equals("O")
&& tablero[2][2].getText().equals("O")){
ganador = 2;
}
if(tablero[0][2].getText().equals("O") && tablero[1][1].getText().equals("O")
&& tablero[2][0].getText().equals("O")){
ganador = 2;
}
// Donde termina el cdigo de fila, columnas y diagonales
if(ganador==1){
JOptionPane.showMessageDialog(this,"Felicidades Jugador " + Jugador1 + "
Ganaste!!!" );
bloquear();
}

if(ganador==2){
JOptionPane.showMessageDialog(this,"Felicidades Jugador " + Jugador2 + "
Ganaste!!!" );
bloquear();
}
}
public void bloquear(){
for (int i=0; i<3; i++){
for (int j = 0; j < 3; j++){
tablero[i][j].setEnabled(false);
}
}

5
Ingeniera de Sistemas Fundamentos de Programacin

public void limpiar(){


for (int i=0; i<3; i++){
for (int j = 0; j < 3; j++){
tablero[i][j].setEnabled(true);
tablero[i][j].setText("");
tablero[i][j].setBackground(colorB);
}
}
}

También podría gustarte