whiplk

[App] - Jogo da forca

Mar 31st, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. //by: Willian Luigi
  2.  
  3. import java.util.*;
  4. import javax.swing.*;
  5.  
  6. public class JogoDaForca extends JFrame {
  7.     String[] db = {
  8.         "amor",
  9.         "paz",
  10.         "burro",
  11.         "bolo",
  12.         "praia",
  13.         "lindo",
  14.         "feio"
  15.     };
  16.     Random it = new Random();
  17.     String palavraVez = null;
  18.     char[] palavra = null;
  19.     String frmMsg = null;
  20.     int vida = 4;
  21.     JTextField[] txb = new JTextField[25];
  22.     JLabel lbl;
  23.     JogoDaForca() {
  24.         super("Jogo da forca.");
  25.         this.getContentPane().setLayout(null);
  26.         palavraVez = db[it.nextInt(7)];
  27.         frmMsg = String.format("Você tem %d chances.", vida);
  28.         lbl = new JLabel(frmMsg);
  29.         lbl.setBounds(5,5, 150, 50);
  30.         this.getContentPane().add(lbl);
  31.         txb[24] = new JTextField();
  32.         txb[24].setBounds(-30, -30, 15, 25);
  33.         this.getContentPane().add(txb[24]);
  34.         createLayout(palavraVez);
  35.         txb[24].addKeyListener(new java.awt.event.KeyAdapter() {
  36.             public void keyTyped(java.awt.event.KeyEvent e) {
  37.                 if (palavraVez.contains("" + e.getKeyChar())) {
  38.                     for (int j = 0; j < palavraVez.length(); ++j) {
  39.                         if (palavra[j] == e.getKeyChar()) {
  40.                             txb[j].setText("" + e.getKeyChar());
  41.                         }
  42.                     }
  43.                 } else {
  44.                     JOptionPane.showMessageDialog(null, "Não contém essa letra.", "Aviso", JOptionPane.ERROR_MESSAGE);
  45.                     callError();
  46.                     return;                            
  47.                 }
  48.                 int v = 0;
  49.                 for (int j = 0; j < palavraVez.length(); ++j) {
  50.                     if (txb[j].getText().length() > 0) {
  51.                         ++v;
  52.                     }
  53.                 }
  54.                 if (v == palavraVez.length()) {
  55.                     JOptionPane.showMessageDialog(null, "Você acertou a palavra!", "Parabéns", JOptionPane.INFORMATION_MESSAGE);
  56.                     for (int j = 0; j < palavraVez.length(); ++j) {
  57.                         removeAll(palavraVez);
  58.                     }
  59.                     palavraVez = db[it.nextInt(5)];
  60.                     callNew(palavraVez);
  61.                 }
  62.             }
  63.         });
  64.         setResizable(false);
  65.         setSize(350, 110);
  66.         setVisible(true);
  67.     }
  68.    
  69.     private void removeAll(String str) {
  70.         for (int i = 0; i < str.length(); ++i) {
  71.             this.getContentPane().remove(txb[i]);
  72.         }
  73.     }
  74.     private void createLayout(String str) {
  75.         palavra = new char[str.length()];
  76.         for (int i = 0, x = 10; i < str.length(); ++i, x += 25) {
  77.             txb[i] = new JTextField();
  78.             txb[i].setBounds(x, 40, 15, 25);
  79.             txb[i].setEditable(false);
  80.             this.getContentPane().add(txb[i]);
  81.             palavra[i] = str.charAt(i);
  82.         }
  83.         txb[24].setText("");
  84.         txb[24].requestFocus();
  85.     }
  86.     private void callNew(String str) {
  87.         createLayout(str);
  88.         ++vida;
  89.         frmMsg = String.format("Você tem %d chances. (+1)", vida);
  90.         lbl.setText(frmMsg);
  91.     }
  92.     private void callError() {
  93.         if (vida == 0) {
  94.             JOptionPane.showMessageDialog(null, "Game over xD", "Willian wins.", JOptionPane.ERROR_MESSAGE);
  95.             dispose();
  96.             return;
  97.         }
  98.         --vida;
  99.         frmMsg = String.format("Você tem %d chances.", vida);
  100.         lbl.setText(frmMsg);
  101.     }
  102.    
  103.     public static void main(String[] args) {
  104.         JogoDaForca inst = new JogoDaForca();
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment