Codigo para juego el ahorcado para NetBeans 8.1
package SistemaEscolar;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
* @web <a href="http://www.facebook.com/oskr2013
" title="http://www.facebook.com/oskr2013
">http://www.facebook.com/oskr2013
</a> * @author Oskr
*/
public class Ahorcado {
JTextField jt;
JLabel lb;
JLabel lb2;
private boolean play =false;
private String[] diccionario = {"CANTAR","BAILAR","REIR","APLAUDIR","APRENDER","GANAR","ESCRIBIR","APUNTAR","ACEPTAR","ACTUAR","PERMITIR","PREGUNTAR","EVITAR","CAMBIAR","LIMPIAR","RECOLECTAR","VENIR","COMPRAR","COMER","RECLAMAR","CERRAR","MONTAR","TREPAR","ELEGIR","PODER","LLAMAR","CONSTRUIR"};
private char[] palabra_secreta;
private char[] palabra;
int intentos = 0;
boolean cambios=false;
public ahorcado(){}
public ahorcado(JTextField texto, JLabel emo, JLabel emo2){
System.out.println("Juego del ahorcado por Oskr");
this.palabra_secreta = Random().toCharArray();
String s="";
//llena un string con "_"
for(int i=0;i<=this.palabra_secreta.length-1;i++){
s = s + "_";
System.out.print(this.palabra_secreta[i]);
}
this.palabra = s.toCharArray();
this.jt=texto;
this.lb= emo;
this.lb2= emo2;
jt.setText(s);
lb.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/emo0.jpg")));
lb2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/ahorcado_0.jpg")));
this.play=true;
}
//evalua el juego de acuerdo a los caracteres que se le pase
public void evaluar(char word){
if(play){
String p="";
//controla que aun se pueda jugar
if(this.intentos==6){
JOptionPane.showMessageDialog(null,"GAME OVER, MENSO!!!---JUEGO TERMINADO, MENSO!!!");
}
else{
//evalua caracter por caracter
for(int j=0;j<=this.palabra_secreta.length-1;j++){
//si el caracter se encuentra en la palabra secreta
if(this.palabra_secreta[j]==word){
this.palabra[j]= word;//se asigna para que se pueda ver en pantalla
this.cambios=true;
}
p = p + this.palabra[j];
}//fin for
//si no se produjo ningun cambio, quiere decir que el jugador se equivoco
if(this.cambios==false){
this.intentos+=1; //se incrementa
lb.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/emo"+this.intentos+".jpg")));
lb2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/ahorcado_"+this.intentos+".jpg")));
if(this.intentos<6){
JOptionPane.showMessageDialog(null,"MENSO!, te quedan " + (6-this.intentos) + " intentos más");
}
}else{
this.cambios=false;
}
this.jt.setText(p);
//comprobamos el estado del juego
gano();
}
}
}
private void gano(){
boolean win=false;
for(int i=0;i<=this.palabra_secreta.length-1;i++){
if(this.palabra[i]==this.palabra_secreta[i]){
win=true;
}else{
win=false;
break;
}
}
if(win){
JOptionPane.showMessageDialog(null,"Ganaste, Felicidades!!! \n espera tu premio");
lb2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/win.jpg")));
}
}
private String Random(){
int num = (int)(Math.random()*(diccionario.length));
return diccionario[num];
}
}
Codigo sencillo para NetBeans 8.1, saludo para telefono Hola mundo (hello Wordl)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author ajpdsoft
*/
public class HelloMIDlet extends MIDlet implements CommandListener {
private boolean midletPaused = false;
//
private Command exitCommand;
private Form form;
private StringItem stringItem;
//
/**
* The HelloMIDlet constructor.
*/
public HelloMIDlet() {
}
//
//
//
/**
* Initilizes the application.
* It is called only once when the MIDlet is
* started. The method is called before the startMIDlet method.
*/
private void initialize() {
// write pre-initialize user code here
// write post-initialize user code here
}
//
//
/**
* Performs an action assigned to the
* Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {
// write pre-action user code here
switchDisplayable(null, getForm());
// write post-action user code here
}
//
//
/**
* Performs an action assigned to the
* Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {
// write pre-action user code here
// write post-action user code here
}
//
//
/**
* Switches a current displayable in a display.
* The display instance is taken from getDisplay
* method. This method is used by all actions
* in the design for switching displayable.
* @param alert the Alert which is temporarily
* set to the display; if null,
* then nextDisplayable is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//
//
/**
* Called by a system to indicated that a command
* has been invoked on a particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
// write pre-action user code here
if (displayable == form) {
if (command == exitCommand) {
// write pre-action user code here
exitMIDlet();
// write post-action user code here
}
}
// write post-action user code here
}
//
//
/**
* Returns an initiliazed instance of exitCommand component.
* @return the initialized component instance
*/
public Command getExitCommand() {
if (exitCommand == null) {
// write pre-init user code here
exitCommand = new Command("Exit", Command.EXIT, 0);
// write post-init user code here
}
return exitCommand;
}
//
//
/**
* Returns an initiliazed instance of form component.
* @return the initialized component instance
*/
public Form getForm() {
if (form == null) {
// write pre-init user code here
form = new Form("Welcome", new Item[] { getStringItem() });
form.addCommand(getExitCommand());
form.setCommandListener(this);
// write post-init user code here
}
return form;
}
//
//
/**
* Returns an initiliazed instance of stringItem component.
* @return the initialized component instance
*/
public StringItem getStringItem() {
if (stringItem == null) {
// write pre-init user code here
stringItem = new StringItem("AjpdSoft - Primer programa m\u00F3vil", "AjpdSoft Hola mundo");
// write post-init user code here
}
return stringItem;
}
//
/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}
/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already
* started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}
/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has
* to be unconditionally terminated and all
* resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}
}
0 comentarios:
Publicar un comentario