viernes, 30 de noviembre de 2012

JUEGO DE DADOS CON RANDOM EN JFRAME

Este Ejercicio la verdad esta muy simple, hubiera echo con imágenes de dados, incluso simulando el lanzamiento del dado, pero la verdad no me alcanzo el tiempo, para otra sera.
  • EJERCICIO

Programa que simule el lanzamiento de dos dados. El programa deberá usar Math.ramdom para lanzar ambos dados, calculándose después la suma de los dos valores. Implementar un juego en el cual participen dos jugadores (uno podría ser la maquina) y lance los dados, de acuerdo al valor obtenido por los dados realizar su avance, a través de un tablero o escenario de juego.

  • RESULTADO



  • COD. FUENTE

/**
 *
 *  E-Mail : shamirdhc31@gmail.com
 *  Blog   : http://javadhc.blogspot.com
 *
 */

/**
 * programa que simule el lanzamiento de dos dados. 
 * El programa debera usar Math.ramdom para lanzar ambos dados, 
 * calculandose despues la suma de los dos valores. 
 * Implementar un juego en el cual participen dos jugadores 
 * (uno podria ser la maquina)y lance los dados, 
 * de acuerdo al valor obtenido por los dados realizar su avance,
 * a traves de un tablero o escenario de juego.
 */

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;


public class JuegoDados extends JFrame implements ActionListener
{
    public JTextArea txtJugador01 = new JTextArea(20,22);
    public JTextArea txtJugador02 = new JTextArea(20,22);
    public JComboBox cbxModoJuego = new JComboBox();
    public JButton btnJugar = new JButton("JUGAR"); 
    public JLabel lblJugador01 = new JLabel("          JUGADOR 01");
    public JLabel lblJugador02 = new JLabel("          JUGADOR 02");
    public JScrollPane spnJugador01 = new JScrollPane(txtJugador01);
    public JScrollPane spnJugador02 = new JScrollPane(txtJugador02);
    public int NumPartida = 0;

    public JuegoDados()
    {
        super("JUEGO DE DADOS");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600,500);
        this.setResizable(false);
        
        FlowLayout DISTRIBUIDOR = new FlowLayout(FlowLayout.CENTER,50,20);
        this.setLayout(DISTRIBUIDOR);
        
        this.btnJugar.addActionListener(this);
        
        this.txtJugador01.setEditable(false);
        this.txtJugador02.setEditable(false);
        
        this.cbxModoJuego.addItem("JUGADOR VS JUGADOR");
        this.cbxModoJuego.addItem("JUGADOR VS MAQUINA");
        this.txtJugador01.setSize(100,200);
        this.add(this.lblJugador01);
        this.add(this.lblJugador02);
        this.add(this.spnJugador01);
        this.add(this.spnJugador02);
        this.add(this.cbxModoJuego);
        this.add(this.btnJugar);        
    }

    @Override
    public void actionPerformed(ActionEvent AE) 
    {       
        int Resultado01,Resultado02;
        
        this.NumPartida++;
        if(this.cbxModoJuego.getSelectedIndex() == 0)
        {
            JOptionPane.showMessageDialog(this,"TURNO DE JUGADOR 01");
            Resultado01 = TirarDadoJugador(1,"JUGADOR 01");
            
            JOptionPane.showMessageDialog(this,"TURNO DE JUGADOR 02");
            Resultado02 = TirarDadoJugador(2,"JUGADOR 02");    
            
            if(Resultado01 > Resultado02)
            {
                JOptionPane.showMessageDialog(this,"GANO EL JUGADOR 01\n\n TOTAL = " + Resultado01);
            }
            else if(Resultado01 < Resultado02)
            {
                JOptionPane.showMessageDialog(this,"GANO EL JUGADOR 02\n\n TOTAL = " + Resultado02);
            }
            else if(Resultado01 == Resultado02)
            {
                JOptionPane.showMessageDialog(this,"EMPATES..¡¡¡\n\n TOTAL = " + Resultado01);
            }           
        }
        else
        {
            JOptionPane.showMessageDialog(this,"TURNO DE JUGADOR 01");
            Resultado01 = TirarDadoJugador(1,"JUGADOR 01");
            
            Resultado02 = TirarDadoJugador(3,"MAQUINA");    
            
            if(Resultado01 > Resultado02)
            {
                JOptionPane.showMessageDialog(this,"GANO EL JUGADOR 01\n\n TOTAL = " + Resultado01);
            }
            else if(Resultado01 < Resultado02)
            {
                JOptionPane.showMessageDialog(this,"GANO LA MAQUINA\n\n TOTAL = " + Resultado02);
            }
            else if(Resultado01 == Resultado02)
            {
                JOptionPane.showMessageDialog(this,"EMPATES..¡¡¡\n\n TOTAL = " + Resultado01);
            }              
        }
    }
    
    //--Jugador01 = 1, Jugador02 = 2, Maquina = 3
    public int TirarDadoJugador(int Jugador,String NomJugador)
    {
        int Dado01,Dado02,SumaDados;        
        
        Dado01 = GenerarAleatorioDado();
        Dado02 = GenerarAleatorioDado();
        SumaDados = Dado01 + Dado02;
            
        JOptionPane.showMessageDialog(this,"RESULTADO DE TIRAR DADOS PARA " + NomJugador
                                         + "\n\nDADO 01 : " + Dado01 + "\nDADO 02 : " + Dado02
                                         + "\n\n TOTAL SUMAN " + SumaDados);
        
        switch(Jugador)
        {
            case 1: this.txtJugador01.setText(this.txtJugador01.getText() + "\nJUEGO " 
                                            + this.NumPartida + ": TOTAL " + SumaDados
                                            + " DADO 01: " + Dado01 + " DADO 02: " + Dado02);break;
                
            case 2: this.txtJugador02.setText(this.txtJugador02.getText() + "\nJUEGO " 
                                            + this.NumPartida + ": TOTAL " + SumaDados
                                            + " DADO 01: " + Dado01 + " DADO 02: " + Dado02);break;
                
            case 3: this.txtJugador02.setText(this.txtJugador02.getText() + "\nJUEGO " + "\nJUEGO " 
                                            + this.NumPartida + ": TOTAL " + SumaDados
                                            + " DADO 01: " + Dado01 + " DADO 02: " + Dado02);break;    
        }
        
        return SumaDados;
    }
    public int GenerarAleatorioDado()
    {
        Random Aleatorio = new Random();
        int Dado;
        
        Dado = Aleatorio.nextInt(6)+1;
        
        return Dado;
    }
    
    public static void main(String[] ARGUMENTOS)
    {
        JuegoDados Dados = new JuegoDados();
        Dados.setVisible(true);
    }
}


DALE CLICK EN LA IMAGEN PARA BAJARTE EL PROYECTO CON LOS 2 EJERCICIOS


IMPORTANTE : "TODOS LOS CODIGOS INDICADOS AQUI SON ESCRITOS POR MI PERSONA, ASI QUE CUALQUIER DUDA O EJERCICIO QUE NO PUEDAN RESOLVER, NO DUDEN EN MANDARME UN E-MAIL A MI CORREO"
shamirdhc31@gmail.com

4 comentarios:

  1. y como le ago para que sean 4 dados y ademas se cp vs humano y humano vs humano

    ResponderEliminar
  2. excelente, sera de mucha ayuda, éxitos amigo

    ResponderEliminar
  3. interesante tu logik io realize uno parecido ... http://darwinomartics.blogspot.com/2013/08/juego21-basico-en-java.html .. buen aporte

    ResponderEliminar
  4. How to get to JT Casino by Bus or Train from Las Vegas to
    There are many ways to get from JT Casino 광주광역 출장안마 to Las Vegas. Here 속초 출장마사지 are a few 파주 출장마사지 basic steps that you can take 이천 출장안마 to earn a $500 welcome bonus. 양주 출장샵

    ResponderEliminar