Bienvenido a Tecnohackers

Tecnohackers » Programacion » Area de Programacion » Programacion a Alto Nivel. Visual Basic, Java, delphi, etc.
 » 

[Java] Piedra papel o tijera



Autor Tema: [Java] Piedra papel o tijera  (Leído 2271 veces)

Desconectado CronuX

  • Veterano
  • ***
  • Mensajes: 302
  • OllyDBG
[Java] Piedra papel o tijera
« en: Noviembre 16, 2009, 04:47:59 am »
Sencillo juego con interfaz

Código: You are not allowed to view links. Register or Login
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class PiedraPapel extends JFrame{
    JButton piedra;
    JButton papel;
    JButton tijeras;
    JLabel cuadro;
    JLabel resultado;
   
    public PiedraPapel(){
          Container panel;
          panel=getContentPane();
          panel.setLayout(null);
          Evento ev;
          this.piedra=new JButton("Piedra");
          piedra.setBounds(10,50,100,20);
          panel.add(piedra);
          this.papel=new JButton("Papel");
          papel.setBounds(10,150,100,20);
          panel.add(papel);
          this.tijeras=new JButton("Tijeras");
          tijeras.setBounds(10,250,120,20);
          panel.add(tijeras);
          this.cuadro=new JLabel("");
          cuadro.setBounds(200,150,500,500);
          panel.add(cuadro);
          this.resultado=new JLabel("");
          resultado.setBounds(200,50,300,200);
          panel.add(resultado);
          setSize(1500,1500);
          ev=new Evento();
          piedra.addActionListener(ev);
          papel.addActionListener(ev);
          tijeras.addActionListener(ev);
          piedra.setActionCommand("Piedra");
          papel.setActionCommand("Papel");
          tijeras.setActionCommand("Tijeras");
         

         
        }
             public void mostrar(){
        setVisible(true);
       
    }
   
     class Evento implements ActionListener{
       public void actionPerformed(ActionEvent e){
           JButton b;
           String texto;
           int numero;
           numero=(int)(Math.random()*3);
           cuadro.setText("el numero aleatorio es:"+""+numero);
            b=(JButton) e.getSource();
           texto=b.getActionCommand();
           if (texto.compareTo("Piedra")==0){
               
             
                if(numero==2){
                    resultado.setText("Ganaste");
                }
                else if(numero==1){
                    resultado.setText("Perdiste");
                }
                else if(numero==0){
                    resultado.setText("Empate");
                }               
            }
           
             if (texto.compareTo("Papel")==0){
             
                if(numero==2){
                    resultado.setText("Perdiste");
                }
                else if(numero==1){
                    resultado.setText("Empate");
                }
               else if(numero==0){
                    resultado.setText("Ganaste");
                }               
            }
             if (texto.compareTo("Tijeras")==0){
 
                if(numero==2){
                    resultado.setText("Empate");
                }
                else if(numero==1){
                    resultado.setText("Ganaste");
                }
                else if(numero==0){
                    resultado.setText("Perdiste");
                }               
            }
            }
        }
    }

Fuente: IlegalHack
Autor: K41S3R



Tags:
Tags:

 


SMF 2.0.19 | SMF © 2016, Simple Machines
Paginas Afiliadas
Twitter - FaceBook - Daraxblog
Designed by Smf Personal