Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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.
- */
- package wisielec;
- import java.io.IOException;
- import java.util.Random;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Form;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.ImageItem;
- import javax.microedition.lcdui.StringItem;
- import javax.microedition.lcdui.TextField;
- import javax.microedition.midlet.*;
- //
- /**
- *
- */
- public class Midlet extends MIDlet implements CommandListener {
- private Form layout;
- private StringItem moveCounter;
- private StringItem word;
- private TextField charField;
- private StringItem charHistory;
- private StringItem lifes;
- //
- private ImageItem imageItem;
- private Image aImage;
- //
- private Command cmdEnd, cmdGuess, cmdNewGame;
- private Random rand;
- private int random = 0;
- private int numberOfWords = 3;
- private String[] polishWords = {"pepsi", "java", "cola"};
- public Midlet() {
- layout = new Form("Test");
- cmdEnd = new Command("Koniec", Command.ITEM, 1);
- cmdGuess = new Command("Sprawdź", Command.ITEM, 2);
- cmdNewGame = new Command("Nowa gra", Command.ITEM, 0);
- moveCounter = new StringItem("Próba: ", "0");
- word = new StringItem("Szukane słowo: ", "");
- charField = new TextField("Podaj literę: ", "", 1, TextField.ANY);
- charHistory = new StringItem("Litery: ", "");
- lifes = new StringItem("Pozostało prób: ", "11");
- rand = new Random(System.currentTimeMillis());
- //
- try {
- aImage = Image.createImage("/wisielec/0.png");
- } catch (Exception e) {
- }
- //
- }
- public void startApp() {
- Display disp = Display.getDisplay(this);
- disp.setCurrent(layout);
- layout.addCommand(cmdEnd);
- layout.addCommand(cmdGuess);
- layout.addCommand(cmdNewGame);
- layout.append(moveCounter);
- layout.append(word);
- layout.append(charField);
- layout.append(charHistory);
- layout.append(imageItem);
- layout.setCommandListener(this);
- createNewGame();
- }
- public void commandAction(Command cmd, Displayable d) {
- if (cmd == cmdEnd) {
- notifyDestroyed();
- } else if (cmd == cmdGuess) {
- char ch = ' ';
- if (charField.getString().length() > 0) {
- ch = charField.getString().charAt(0);
- }
- if (ch != ' ') {
- String history = charHistory.getText();
- history += ch + " ";
- charHistory.setText(history);
- int moveNb = Integer.parseInt(moveCounter.getText());
- System.out.println(moveNb);
- moveNb++;
- moveCounter.setText("" + moveNb);
- boolean found = false;
- for (int i = 0; i < polishWords[random].length(); i++) {
- if (polishWords[random].charAt(i) == ch) {
- found = true;
- String temp = word.getText().substring(0, i) + ch + word.getText().substring(i + 1, polishWords[random].length());
- word.setText(temp);
- }
- }
- if (!found) {
- int tempLifes = Integer.parseInt(lifes.getText());
- tempLifes--;
- lifes.setText("" + tempLifes);
- if (tempLifes < 1) {
- lifes.setText("Przegrana");
- }
- }
- if (word.getText().equals(polishWords[random])) {
- lifes.setText("Wygrałeś!");
- }
- }
- charField.setString("");
- } else if (cmd == cmdNewGame) {
- createNewGame();
- }
- }
- private void createNewGame() {
- moveCounter.setText("0");
- charField.setString("");
- charHistory.setText("");
- lifes.setText("11");
- random = rand.nextInt(numberOfWords);
- String dots = "";
- for (int i = 0; i < polishWords[random].length(); i++) {
- dots += "-";
- }
- word.setText(dots);
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement