Vous êtes sur la page 1sur 5

package uniandes.cupi2.triqui.interfaz; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import uniandes.cupi2.triqui.mundo.

JugadorPC; import uniandes.cupi2.triqui.mundo.Triqui; /** * Interfaz grfica del juego del triqui */ public class InterfazTriqui extends JFrame { // ----------------------------------------------------------------// Constantes // ----------------------------------------------------------------/** * Marca para el jugador humano */ private final static String MARCA_JUGADOR = "X"; /** * Marca para el jugador PC */ private final static String MARCA_PC = "O"; // ----------------------------------------------------------------// Atributos de Interfaz // ----------------------------------------------------------------/** * Panel triqui */ private PanelTriqui triquiPanel; /** * Panel botones */ private PanelBotones botonesPanel; /** * Panel barra de mensajes */ private PanelMensajes mensajesPanel; // ----------------------------------------------------------------// Atributos // ----------------------------------------------------------------/** * Juego de triqui */ private Triqui juegoTriqui; /** * Jugador PC */ private JugadorPC jugadorPC;

// ----------------------------------------------------------------// Constructores // ----------------------------------------------------------------/** * Construye la interfaz del triqui * @param titulo Nombre que tendr el Frame que se despliega. ttulo != null. */ public InterfazTriqui( String titulo ) { // Tablero del Triqui triquiPanel = new PanelTriqui( this ); // Botones botonesPanel = new PanelBotones( this ); // Mensajes mensajesPanel = new PanelMensajes( ); setPreferredSize( new Dimension( 300, 300 ) ); add( mensajesPanel, BorderLayout.NORTH ); add( triquiPanel, BorderLayout.CENTER ); add( botonesPanel, BorderLayout.SOUTH ); setTitle( titulo ); pack( ); setResizable( false ); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mensajesPanel.desplegarMensaje( "Listo para jugar!" ); // Juego Triqui juegoTriqui = new Triqui( ); // Jugador PC del Triqui jugadorPC = new JugadorPC( juegoTriqui, MARCA_PC ); } // ----------------------------------------------------------------// Mtodos // ----------------------------------------------------------------/** * Prepara para iniciar otro juego */ public void nuevoJuego( ) { // Limpia el tablero del triqui juegoTriqui.limpiarTablero( ); JButton botonReiniciar; // Prepara la interfaz del tablero del triqui for( int i = 1; i <= 9; i++ ) { botonReiniciar = ( JButton )triquiPanel.getComponent( i - 1 ); botonReiniciar.setEnabled( true ); botonReiniciar.setText( "" ); } }

/** * Procesa la jugada del humano. Si es vlido invita a jugar al PC * @param casillaJugada Jugada por el humano. casillaJugada != null y casill aJugada != "". */ public void procesaJugada( String casillaJugada ) { int casilla; // Obtiene el nmero de casilla casilla = Integer.valueOf( casillaJugada ).intValue( ); // Indica al juego del triqui la casilla sealada por el humano juegoTriqui.marcarCasilla( casilla, MARCA_JUGADOR ); // Verifica si el humano gan el juego if( juegoTriqui.ganoJuego( MARCA_JUGADOR ) ) { mensajesPanel.desplegarMensaje( "Ganaste!" ); JButton botonReiniciar; // Al ganar se apaga el tablero for( int i = 1; i <= 9; i++ ) { botonReiniciar = ( JButton )triquiPanel.getComponent( i - 1 ); botonReiniciar.setEnabled( false ); } } else // Si no gan el humano invita a jugar al PC { mensajesPanel.desplegarMensaje( "Mi turno!" ); // si el PC pudo realizar una jugada if( jugadorPC.jugar( ) ) { // Verifica si gan el jugador PC if( juegoTriqui.ganoJuego( MARCA_PC ) ) { mensajesPanel.desplegarMensaje( "Gan!" ); JButton botonReiniciar; // Al ganar se apaga el tablero for( int i = 1; i <= 9; i++ ) { botonReiniciar = ( JButton )triquiPanel.getComponent( i - 1 ); botonReiniciar.setEnabled( false ); } } else // si no gan el PC cede el turno mensajesPanel.desplegarMensaje( "Tu turno" ); } else { // Si el jugador PC no pudo realizar la jugada es porque el // juego ha terminado mensajesPanel.desplegarMensaje( "Se acab el juego..." );

} } } /** * Pinta el tablero para reflejar las jugadas hechas */ public void repintarTriqui( ) { String marca; JButton botonReiniciar; for( int i = 1; i <= 9; i++ ) { botonReiniciar = ( JButton )triquiPanel.getComponent( i - 1 ); // La marca de cada casilla no vaca la toma del juego if( !juegoTriqui.casillaVacia( i ) ) { marca = juegoTriqui.obtenerMarcaCasilla( i ); botonReiniciar.setText( marca ); botonReiniciar.setEnabled( false ); } } } // ----------------------------------------------------------------// Puntos de Extensin // ----------------------------------------------------------------/** * Ejecuta la operacin 1.<br> * <b>post: </b>Se retorna el valor de la operacin 1 y se muestra en pantalla . */ public void reqFuncOpcion1( ) { String respuesta = juegoTriqui.metodo1( ); JOptionPane.showMessageDialog( this, respuesta, "Respuesta", JOptionPane .INFORMATION_MESSAGE ); } /** * Ejecuta la operacin 2.<br> * <b>post: </b> Se retorna el valor de la operacin 2 y se muestra en pantall a. */ public void reqFuncOpcion2( ) { String respuesta = juegoTriqui.metodo2( ); JOptionPane.showMessageDialog( this, respuesta, "Respuesta", JOptionPane .INFORMATION_MESSAGE ); } /** * Mtodo principal de ejecucin

* @param args Argumentos de entrada para el programa. No se utilizan. */ public static void main( String[] args ) { InterfazTriqui gui = new InterfazTriqui( "Juego de Triqui" ); gui.setVisible( true ); } }

Vous aimerez peut-être aussi