You are on page 1of 7

Objetivo del Proyecto

Crear un juego en el cual el usuario mediante uso del ratn presionar botones para adivinar las frases ocultas dentro del
programa.

Diagrama de Clases

Cdigo Fuente

El cdigo fuente utilizado para este programa es el siguiente:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Diana
*/
public class Main extends javax.swing.JFrame {

/**
* Creates new form Main
*/

private ImageIcon imgs[];


private JButton btns[];
private String msgs[];
private int ran;
private int err;
private String res[];

public Main() {
initComponents();
imgs = new ImageIcon[6];
btns = new JButton[27];
msgs = new String[10];

//VECTOR CON LAS IMGENES DEL AHORCADO


imgs[0] = new ImageIcon(getClass().getResource("/Ahorcado/im1.jpg"));
imgs[1] = new ImageIcon(getClass().getResource("/Ahorcado /im2.jpg"));
imgs[2] = new ImageIcon(getClass().getResource("/Ahorcado /im3.jpg"));
imgs[3] = new ImageIcon(getClass().getResource("/Ahorcado /im4.jpg"));
imgs[4] = new ImageIcon(getClass().getResource("/Ahorcado /im5.jpg"));
imgs[5] = new ImageIcon(getClass().getResource("/Ahorcado /im6.jpg"));

//VECTOR CON LOS BOTONES DE LAS LETRAS


btns[1] = jButton2;
btns[2] = jButton3;
btns[3] = jButton4;
btns[4] = jButton5;
btns[5] = jButton6;
btns[6] = jButton7;
btns[7] = jButton8;
btns[8] = jButton9;
btns[9] = jButton10;
btns[10] = jButton11;
btns[11] = jButton12;
btns[12] = jButton13;
btns[13] = jButton14;
btns[14] = jButton15;
btns[15] = jButton16;
btns[16] = jButton17;
btns[17] = jButton18;
btns[18] = jButton19;
btns[19] = jButton20;
btns[20] = jButton21;
btns[21] = jButton22;
btns[22] = jButton23;
btns[23] = jButton24;
btns[24] = jButton25;
btns[25] = jButton26;
btns[26] = jButton27;

//MENSAJES PARA ADIVINAR


msgs[0] = "Si algo se puede soar se puede programar".toUpperCase();
msgs[1] = "Ingenieria en Sistemas Electronicos Industriales".toUpperCase();
msgs[2] = "Espero pasar la materia de Orientada a Objetos".toUpperCase();
msgs[3] = "El hardware es aquello a lo que puedes dar patadas Software es aquello a lo que solo
puedes insultar".toUpperCase();
msgs[4] = "Nada Humano me es Ajeno".toUpperCase();
msgs[5] = "Copiar todo o parte de un programa es tan natural para un programador como respirar
ademas es productivo Deberia ser libre.".toUpperCase();
msgs[6] = "Los fines de semana fueron hechos para la programacion".toUpperCase();
msgs[7] = "Los programadores nunca mueren tan solo se pierden en el Proceso".toUpperCase();
msgs[8] = "Cuando tengas duda usa la fuerza bruta".toUpperCase();
msgs[9] = "Dime y lo olvido ensname y lo recuerdo involcrame y lo aprendo".toUpperCase();

//A CADA LETRA LE ASGINNAMOS UN EVENTO PARA REVISAR CADA QUE SE PRESIONA
for (int i = 1; i < 27; i++) {
btns[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checarLetra(e);
}
});
}
iniciar();
}

//FUNCIN INICIA TODOS LOS PARAMETROS DEL JUEGO


private void iniciar(){
//ERRORES EN 0
err = 0;
jButton1.setIcon(imgs[0]);
jTextPane1.setText("");
//ACTIVAMOS TODAS LAS LETRAS
for (int i = 1; i < 27; i++) {
btns[i].setEnabled(true);
}
//GENERAMOS UN MENSAJE RANDOM
ran =(int) 0 + (int)(Math.random() * ((msgs.length - 1) + 1));
//SEPARAMOS EL MENSAJE POR PALABRAS
String pal[] = msgs[ran].split(" ");
res = new String[msgs[ran].length()+1];
int j = 0;
//VAMOS IMPRIMIENDO EL NMERO DE CARACTERES DEL MENSAGE CON _
for (String pal1 : pal) {
for (int i = 0; i < pal1.length(); i++) {
jTextPane1.setText(jTextPane1.getText() + "_ ");
res[j++] = "_";
}
jTextPane1.setText(jTextPane1.getText() + "\n");
res[j++] = " ";
}
}

//CADA QUE SE PRESIONA UNA LETRA SE COMPRUEBA EL ESTADO DEL JUEGO


private void checarLetra(ActionEvent e){
JButton bt = (JButton)e.getSource();
char c[];
//SE BUSCA LA LETRA QUE SE PRESIONO
for (int i = 1; i < 27; i++) {
if(bt == btns[i]){
//SE SACA LA LETRA
c = Character.toChars(64+i);
//BUSCAMOS QUE LA LETRA SE ENCUENTRE EN LA FRASE
boolean esta = false;
for (int j = 0; j < msgs[ran].length(); j++) {
if(c[0] == msgs[ran].charAt(j)){
res[j] = c[0]+"";
esta = true;
}
}
//SI LA LETRA ESTA EN EL MENSAJE SE MUESTRA EN EL TEXTPANEL
if(esta){
jTextPane1.setText("");
for (String re : res) {
if(" ".equals(re))jTextPane1.setText(jTextPane1.getText() + "\n");
else jTextPane1.setText(jTextPane1.getText() + re +" ");
}
//SE COMPRUEBA QUE SE GANO CUANDO YA NO HAYA _ EN EL MENSAGE
boolean gano = true;
for (String re : res) {
if(re.equals("_")){
gano = false;
break;
}
}
//SI SE GANA SE MANDA UN MENSAGE Y SE REINIICIA EL JUEGO
if(gano){
JOptionPane.showMessageDialog(this, "FELICIDADES HAS GANADO!");
iniciar();
return;
}
//SI LA LETRA NO ESTA EN EL MENSAGE, SE INCREMENTA EL ERROR Y SE CAMBIA LA IMAGEN
}else{
jButton1.setIcon(imgs[++err]);
//SI LLEGAMOS A LOS 5 ERRORES ENTONCES SE PERDIO EL JUEGO Y SE MANDA EL
MENSAGE
if(err == 5){
JOptionPane.showMessageDialog(this, "BUUU PERDISTE :|, LA FRASE ERA:\n"+msgs[ran]);
iniciar();
return;
}
}
//CADA QUE SE PRESIONA UNA LETRA SE DESACTIVA PARA QUE NO SE PUEDA PREISONAR OTRA
VEZ
bt.setEnabled(false);
break;
}
}

Uso del Programa

Se abre el programa desde Netbeans de ORACLE y presionamos el botn RUN

Se abrir nuestra interfaz


El usuario deber seleccionar los botones de las letras con el mouse para que vayan completndose las frases

Al seleccionar una tecla, esta permanece deshabilitada ya que el programa detecta todas las letras iguales en la
frase.

Cada que se coloca una letra que no est en la frase ir formndose el Ahorcado
Si se llega a formar el Ahorcado aparecer el siguiente mensaje y la frase que se deba adivinar

Al momento de dar click en Aceptar automticamente se reinicia el juego.

En caso de que adivines la frase aparecer el mensaje:


Al dar click de nuevo en Aceptar se reinicia el juego. En cualquier momento puedes presionar el botn de reiniciar
si ya no quieres jugar con esa frase.

Las frases que aparecen en la interfaz y que estn previamente cargadas le aparecen al usuario de forma aleatoria.

Para cerrar la interfaz slo debes de dar click en el botn

En un inicio se haba planeado realizar un Juego de la vida, pero por dificultades del estudiante se opt por realizar
este juego.

You might also like