Jogo de adivinhação de números

Publicado por Rodrigo Zanuzzo (última atualização em 03/09/2009)

[ Hits: 12.340 ]

Homepage: @rodrigozanuzzo

Download JogoAdivinha.jar




Jogo muito simples feito no Netbeans. Basicamente você terá de acertar o número sorteado, se você errar ele manda digitar um número maior ou menor e vai contando as tentativas até você acertar.

Sugestões...
8D

  



Esconder código-fonte

/*
 * Adivinha.java
 *
 * Created on 2 de Setembro de 2009, 09:09
 */
/**
 *
 * @author  Rodrigo Zanuzzo
 */
import javax.swing.JOptionPane;
public class Adivinha extends javax.swing.JFrame {
     int resp,sorteio,tentativas,qnt;

    /** Creates new form Adivinha */
    public Adivinha() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lJogador = new javax.swing.JLabel();
        lTentativa = new javax.swing.JLabel();
        sNumero = new javax.swing.JSpinner();
        bIniciar = new javax.swing.JButton();
        bVerificar = new javax.swing.JButton();
        bJogador = new javax.swing.JButton();
        bNovo = new javax.swing.JButton();
        bFechar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Jogo de adivinha");

        jPanel1.setLayout(null);

        jLabel1.setText("Jogador:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 10, 60, 17);

        jLabel2.setText("Tentativas:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 30, 80, 17);

        lJogador.setText("XXXXXXXXXXX");
        jPanel1.add(lJogador);
        lJogador.setBounds(70, 10, 100, 17);

        lTentativa.setText("00");
        jPanel1.add(lTentativa);
        lTentativa.setBounds(90, 30, 30, 17);
        jPanel1.add(sNumero);
        sNumero.setBounds(20, 50, 26, 24);

        bIniciar.setText("Iniciar");
        bIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bIniciarActionPerformed(evt);
            }
        });
        jPanel1.add(bIniciar);
        bIniciar.setBounds(210, 10, 90, 27);

        bVerificar.setText("Verificar");
        bVerificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bVerificarActionPerformed(evt);
            }
        });
        jPanel1.add(bVerificar);
        bVerificar.setBounds(210, 50, 90, 27);

        bJogador.setText("Jogador");
        bJogador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bJogadorActionPerformed(evt);
            }
        });
        jPanel1.add(bJogador);
        bJogador.setBounds(0, 100, 90, 27);

        bNovo.setText("Novo Jogo");
        bNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bNovoActionPerformed(evt);
            }
        });
        jPanel1.add(bNovo);
        bNovo.setBounds(90, 100, 120, 27);

        bFechar.setText("Fechar");
        bFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bFecharActionPerformed(evt);
            }
        });
        jPanel1.add(bFechar);
        bFechar.setBounds(210, 100, 90, 27);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-308)/2, (screenSize.height-189)/2, 308, 189);
    }// </editor-fold>

private void bNovoActionPerformed(java.awt.event.ActionEvent evt) {
resp=JOptionPane.showConfirmDialog(null,"Deseja mudar o nome do jogador?","NOME",JOptionPane.YES_NO_OPTION);
        if (resp==0){
            lJogador.setText(JOptionPane.showInputDialog(null,"Digite o nome do Jogador"));
            sorteio=(int)(Math.random()*10);
            bJogador.setEnabled(true);
            bVerificar.setEnabled(true);
            sNumero.setEnabled(true);
        }else{
            sorteio=(int)(Math.random()*10);
            bJogador.setEnabled(true);
            bVerificar.setEnabled(true);
            sNumero.setEnabled(true);
        }
}

private void bIniciarActionPerformed(java.awt.event.ActionEvent evt) {
 lJogador.setText(JOptionPane.showInputDialog(null,"Digite o nome do Jogador"));
sorteio=(int)(Math.random()*10);
bIniciar.setEnabled(false);
bJogador.setEnabled(true);
bVerificar.setEnabled(true);
bNovo.setEnabled(true);
sNumero.setEnabled(true);
}

private void bVerificarActionPerformed(java.awt.event.ActionEvent evt) {
    qnt++;
        lTentativa.setText(String.valueOf(qnt));
        tentativas=sNumero.getValue().hashCode();
        
        if (sorteio>tentativas){
            JOptionPane.showMessageDialog(null,"tente um numero maior");
        }
        if (sorteio<tentativas){
            JOptionPane.showMessageDialog(null,"Tente um numero menor");
        }
        if (sorteio==tentativas){
            JOptionPane.showMessageDialog(null,"Parabéns");
            resp=JOptionPane.showConfirmDialog(null,"Deseja encerrar o programa?","FECHAR",JOptionPane.YES_NO_OPTION);
       qnt=0;     
            if (resp==0){
                System.exit(0);
            }else{
                bJogador.setEnabled(false);
                bVerificar.setEnabled(false);
                sNumero.setEnabled(false);
                tentativas=0;
                lTentativa.setText(String.valueOf(tentativas));
            }
        }
        sNumero.requestFocus();
}

private void bJogadorActionPerformed(java.awt.event.ActionEvent evt) {
lJogador.setText(JOptionPane.showInputDialog(null,"Digite o nome do Jogador"));

}

private void bFecharActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Adivinha().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton bFechar;
    private javax.swing.JButton bIniciar;
    private javax.swing.JButton bJogador;
    private javax.swing.JButton bNovo;
    private javax.swing.JButton bVerificar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lJogador;
    private javax.swing.JLabel lTentativa;
    private javax.swing.JSpinner sNumero;
    // End of variables declaration
    
}

Scripts recomendados

Calculadora em java

Jogo do Azar

Soma Gratificação Menos o IR.

Comando if

Programa de sorteio


  

Comentários
[1] Comentário enviado por jlvidals em 07/09/2009 - 14:36h

Legal! Bom para o aprendizado Java

[2] Comentário enviado por nilsob em 02/12/2009 - 09:17h

Tá bonzinho ! Agora só falta começar a usar o eclipse, ou nao


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts