Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package wisielec;
  7.  
  8. import java.io.IOException;
  9. import java.util.Random;
  10. import javax.microedition.lcdui.Command;
  11. import javax.microedition.lcdui.CommandListener;
  12. import javax.microedition.lcdui.Display;
  13. import javax.microedition.lcdui.Displayable;
  14. import javax.microedition.lcdui.Form;
  15. import javax.microedition.lcdui.Image;
  16. import javax.microedition.lcdui.ImageItem;
  17. import javax.microedition.lcdui.StringItem;
  18. import javax.microedition.lcdui.TextField;
  19. import javax.microedition.midlet.*;
  20. //
  21.  
  22. /**
  23. *
  24. */
  25. public class Midlet extends MIDlet implements CommandListener {
  26.  
  27. private Form layout;
  28.  
  29. private StringItem moveCounter;
  30. private StringItem word;
  31. private TextField charField;
  32. private StringItem charHistory;
  33. private StringItem lifes;
  34. //
  35. private ImageItem imageItem;
  36. private Image aImage;
  37. //
  38. private Command cmdEnd, cmdGuess, cmdNewGame;
  39.  
  40. private Random rand;
  41. private int random = 0;
  42.  
  43. private int numberOfWords = 3;
  44. private String[] polishWords = {"pepsi", "java", "cola"};
  45.  
  46. public Midlet() {
  47. layout = new Form("Test");
  48.  
  49. cmdEnd = new Command("Koniec", Command.ITEM, 1);
  50. cmdGuess = new Command("Sprawdź", Command.ITEM, 2);
  51. cmdNewGame = new Command("Nowa gra", Command.ITEM, 0);
  52.  
  53. moveCounter = new StringItem("Próba: ", "0");
  54. word = new StringItem("Szukane słowo: ", "");
  55. charField = new TextField("Podaj literę: ", "", 1, TextField.ANY);
  56. charHistory = new StringItem("Litery: ", "");
  57. lifes = new StringItem("Pozostało prób: ", "11");
  58.  
  59. rand = new Random(System.currentTimeMillis());
  60.  
  61. //
  62. try {
  63. aImage = Image.createImage("/wisielec/0.png");
  64. } catch (Exception e) {
  65. }
  66.  
  67.  
  68. //
  69. }
  70.  
  71. public void startApp() {
  72. Display disp = Display.getDisplay(this);
  73. disp.setCurrent(layout);
  74.  
  75. layout.addCommand(cmdEnd);
  76. layout.addCommand(cmdGuess);
  77. layout.addCommand(cmdNewGame);
  78.  
  79. layout.append(moveCounter);
  80. layout.append(word);
  81. layout.append(charField);
  82. layout.append(charHistory);
  83. layout.append(imageItem);
  84.  
  85. layout.setCommandListener(this);
  86.  
  87. createNewGame();
  88.  
  89. }
  90.  
  91. public void commandAction(Command cmd, Displayable d) {
  92. if (cmd == cmdEnd) {
  93. notifyDestroyed();
  94. } else if (cmd == cmdGuess) {
  95. char ch = ' ';
  96. if (charField.getString().length() > 0) {
  97. ch = charField.getString().charAt(0);
  98. }
  99.  
  100. if (ch != ' ') {
  101. String history = charHistory.getText();
  102. history += ch + " ";
  103. charHistory.setText(history);
  104.  
  105. int moveNb = Integer.parseInt(moveCounter.getText());
  106. System.out.println(moveNb);
  107. moveNb++;
  108. moveCounter.setText("" + moveNb);
  109.  
  110. boolean found = false;
  111. for (int i = 0; i < polishWords[random].length(); i++) {
  112. if (polishWords[random].charAt(i) == ch) {
  113. found = true;
  114. String temp = word.getText().substring(0, i) + ch + word.getText().substring(i + 1, polishWords[random].length());
  115. word.setText(temp);
  116. }
  117.  
  118. }
  119. if (!found) {
  120. int tempLifes = Integer.parseInt(lifes.getText());
  121. tempLifes--;
  122. lifes.setText("" + tempLifes);
  123.  
  124.  
  125. if (tempLifes < 1) {
  126. lifes.setText("Przegrana");
  127. }
  128. }
  129. if (word.getText().equals(polishWords[random])) {
  130. lifes.setText("Wygrałeś!");
  131. }
  132. }
  133. charField.setString("");
  134. } else if (cmd == cmdNewGame) {
  135. createNewGame();
  136. }
  137. }
  138.  
  139. private void createNewGame() {
  140. moveCounter.setText("0");
  141. charField.setString("");
  142. charHistory.setText("");
  143. lifes.setText("11");
  144.  
  145. random = rand.nextInt(numberOfWords);
  146.  
  147. String dots = "";
  148. for (int i = 0; i < polishWords[random].length(); i++) {
  149. dots += "-";
  150. }
  151. word.setText(dots);
  152.  
  153. }
  154.  
  155. public void pauseApp() {
  156. }
  157.  
  158. public void destroyApp(boolean unconditional) {
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement