Sie sind auf Seite 1von 5

3. Crea una nueva clase en el paquete Modelo, llamalo juego.

java, es ahi donde colocamos la logica del juego 3 en Raya, el codigo es el siguiente:
package modelo; /** * @web www.jc-mouse.net/ * @author Mouse */ public class juego { private byte turno = 1;//1=>jugador 1 2=> jugador 2 private String marca_X = "X"; private String marca_O = "O"; private boolean error=false;//por si se produce algun mensaje private byte ganador_es=0;//1=>jugador 1 2=> jugador 2 3=>Empate //matriz para almacenar los movimientos del juego private String tablero[][] ={{"","",""}, {"","",""}, {"","",""} }; public juego(){} //reinicia los valores y limpia el tablero public void Jugar_otra_vez() { for ( int i = 0 ; i < tablero.length ; i++ ) for ( int j = 0 ; j < tablero.length ; j++) tablero[i][j]=""; this.error=false; this.ganador_es=0; this.turno=1; } //dado una posicion y segun el turno que corresponda //coloca la marca "X" o "O" en el tablero //Salida: La marca que se coloco en la matriz public String set_movimiento(int posicion) { String out=""; if(turno==1) { out = marcar(posicion , this.marca_X); //si no se pudo marcar => continua con su turno turno = 2; if ( gano(this.tablero, this.marca_X) ) this.ganador_es=1; else if ( empate() ) this.ganador_es=3; } else { out = marcar(posicion , this.marca_O); turno = 1; if ( gano(this.tablero, this.marca_O) ) this.ganador_es=2; else if ( empate() ) this.ganador_es=3; } return out; } /* MARCA LA CASILLA CON EL MOVIMIENTO DEL JUGADOR, */ private String marcar(int Posicion, String value)

{ String marca=""; switch (Posicion) { case 1:marca = case 2:marca = case 3:marca = case 4:marca = case 5:marca = case 6:marca = case 7:marca = case 8:marca = case 9:marca = } return marca; } //funcion privada que sigue a funcion marcar, esto para no repetir codigo //si al marcar en la matriz , existe algun error, coloca la bandera a TRUE private String sub_marcar(int x, int y, String value) { String marca=""; this.error=false; if( this.tablero[x][y].equals("") ) //se puede marcar { this.tablero[x][y] = value; marca = value; } else//ya esta marcado { marca = this.tablero[x][y]; this.error=true;//Error=>se trata de marcar casilla ya marcada } return marca; } public boolean get_error() { return this.error; } public String get_turno() { return (this.turno==1)? "Turno: X":"Turno: O"; } public byte ganador() { return this.ganador_es; } //funcion que determina quien gano la partida public boolean gano( String matriz[][], String marca ) { //busqueda de ganador por filas for ( int i = 0 ; i < matriz.length ; i++ ) { byte count=0; for ( int j = 0 ; j < matriz.length ; j++) count+=( matriz[i][j].equals(marca) )?1:0; if( count == 3) return true;

sub_marcar(0,0,value); sub_marcar(0,1,value); sub_marcar(0,2,value); sub_marcar(1,0,value); sub_marcar(1,1,value); sub_marcar(1,2,value); sub_marcar(2,0,value); sub_marcar(2,1,value); sub_marcar(2,2,value);

break; break; break; break; break; break; break; break; break;

} //busqueda de ganador por columnas for ( int j = 0 ; j < matriz.length ; j++ ) { byte count=0; for ( int i = 0 ; i < matriz.length ; i++) count+=( matriz[i][j].equals(marca) )?1:0; if( count == 3) return true; } //diagonales if( matriz[0][0].equals(marca) && matriz[1][1].equals(marca) && matriz[2][2].equals(marca) ) return true; if( matriz[0][2].equals(marca) && matriz[1][1].equals(marca) && matriz[2][0].equals(marca) ) return true; return false; } //Funcion que determina si se puede continuar jugando private boolean empate() { for ( int i = 0 ; i < tablero.length ; i++ ) for ( int j = 0 ; j < tablero.length ; j++) if( tablero[i][j].equals("")) return false; return true; } }

package controlador; import modelo.juego; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import vista.interfaz; /** * @web www.jc-mouse.net/ * @author Mouse */ public class controlador implements ActionListener { private interfaz vista; private juego juego; private String titulo = "3 en Raya MVC * jc-mouse.net "; //En el constructor inicializamos nuestros objetos public controlador( interfaz vista , juego modelo){ this.vista = vista; this.juego = modelo; } //Inicia los valores del jFrame VISTA con los datos del MODELO // tambien aadimos el ActionListener a los botones de la VISTA

public void iniciar_vista(){ vista.setTitle( this.titulo ); vista.setLocationRelativeTo(null); this.vista.casilla1.addActionListener(this); this.vista.casilla2.addActionListener(this); this.vista.casilla3.addActionListener(this); this.vista.casilla4.addActionListener(this); this.vista.casilla5.addActionListener(this); this.vista.casilla6.addActionListener(this); this.vista.casilla7.addActionListener(this); this.vista.casilla8.addActionListener(this); this.vista.casilla9.addActionListener(this); } //La accion de los botones de la VISTA es capturado, asi como los valores //dependiendo del boton pulsado, se envia la informacion al modelo //y se espera la respuesta public void actionPerformed(ActionEvent e) { Object boton = e.getSource(); if( this.juego.ganador()== 0 ) { if( boton == this.vista.casilla1 ) this.vista.casilla1.setText( this.juego.set_movimiento(1) else if(boton == this.vista.casilla2) this.vista.casilla2.setText( this.juego.set_movimiento(2) else if(boton == this.vista.casilla3) this.vista.casilla3.setText( this.juego.set_movimiento(3) else if(boton == this.vista.casilla4) this.vista.casilla4.setText( this.juego.set_movimiento(4) else if(boton == this.vista.casilla5) this.vista.casilla5.setText( this.juego.set_movimiento(5) else if(boton == this.vista.casilla6) this.vista.casilla6.setText( this.juego.set_movimiento(6) else if(boton == this.vista.casilla7) this.vista.casilla7.setText( this.juego.set_movimiento(7) else if(boton == this.vista.casilla8) this.vista.casilla8.setText( this.juego.set_movimiento(8) else if(boton == this.vista.casilla9) this.vista.casilla9.setText( this.juego.set_movimiento(9) if( this.juego.get_error()) JOptionPane.showMessageDialog(null, "Error: la casilla ya esta marcada \n Perdiste tu turno"); this.vista.setTitle( this.titulo + this.juego.get_turno()); } if( this.juego.ganador()== 1 ) mensaje(" 'X' "); else if( this.juego.ganador()== 2 ) mensaje(" 'O' "); else if( this.juego.ganador()== 3 ) mensaje(" 'Es un empate' "); } //dependiendo de la respuesta del modelo, se muestra un mensaje al usuario private void mensaje(String s) { int seleccion = JOptionPane.showOptionDialog(null,"Gano el jugador " + s + "\n Que desea hacer?", "Fin del juego", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,

); ); ); ); ); ); ); ); );

null, // null para icono por defecto. new Object[] { " Jugar otra vez ", " Salir de Programa " }, "Jugar otra vez"); if (seleccion != -1) if( (seleccion+1)==1 ) { this.juego.Jugar_otra_vez(); this.vista.setTitle(titulo); this.vista.casilla1.setText( this.vista.casilla2.setText( this.vista.casilla3.setText( this.vista.casilla4.setText( this.vista.casilla5.setText( this.vista.casilla6.setText( this.vista.casilla7.setText( this.vista.casilla8.setText( this.vista.casilla9.setText( } else System.exit(0); } }

"" "" "" "" "" "" "" "" ""

); ); ); ); ); ); ); ); );

package mvc3nraya; import vista.interfaz; import controlador.controlador; import modelo.juego; /** * @web www.jc-mouse.net/ * @author Mouse */ public class Main { public static void main(String[] args) { //nuevas instancias de clase juego modelo = new juego(); interfaz vista = new interfaz(); controlador controlador = new controlador( vista , modelo ); controlador.iniciar_vista(); vista.setVisible(true); } }

http://www.youtube.com/watch?v=nqdftTamsJ0&feature=player_embedded#!

Das könnte Ihnen auch gefallen