Guest User

Untitled

a guest
Dec 10th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.43 KB | None | 0 0
  1. package rnd_game;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import static javax.swing.JOptionPane.*;
  8.  
  9. public class Dream extends JFrame {
  10. private boolean Bf = false;
  11. private boolean Bt = true;
  12. private JButton bPlay, bAgain, bCheck, bCheckLifeOk, bCheckLifeChancel;
  13. private JLabel lGuess, l0, life1, life2, life3, lname, lresult, lNumb, lTry;
  14. private JRadioButton radioButton, radioButton1, radioButton2;
  15. private JTextField txt;
  16. private Cursor RadCursor, TxtCursor, ErrorCursor;
  17. private int Cases, LifeNum, Random;
  18.  
  19. public Dream(String t) {
  20. super(t);
  21. Act action = new Act();
  22.  
  23. RadCursor = new Cursor(Cursor.HAND_CURSOR);
  24. TxtCursor = new Cursor(Cursor.TEXT_CURSOR);
  25. ErrorCursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
  26.  
  27. String UsrName = JOptionPane.showInputDialog(null, "Print your name", "Name", 1); //%User NAMe%
  28. lname = new JLabel("Hello " + UsrName);
  29. lname.setBounds(20, 20, 540, 20);
  30. add(lname);
  31.  
  32.  
  33.  
  34. bPlay = new JButton("Play");
  35. bPlay.setBounds(415, 310, 150, 30);
  36. bPlay.addActionListener(action);
  37. bPlay.setBackground(Color.GRAY);
  38. add(bPlay);
  39.  
  40. bAgain = new JButton("Again");
  41. bAgain.setBounds(20, 310, 150, 30);
  42. bAgain.addActionListener(action);
  43. bAgain.setBackground(Color.GRAY);
  44. add(bAgain);
  45.  
  46. bCheck = new JButton("Check");
  47. bCheck.setBounds(420, 110, 100, 22);
  48. bCheck.setVisible(Bf);
  49. bCheck.addActionListener(action);
  50. bCheck.setBackground(Color.GRAY);
  51. add(bCheck);
  52.  
  53. bCheckLifeOk = new JButton("Ok");
  54. bCheckLifeOk.setBounds(20, 220, 50, 20);
  55. bCheckLifeOk.addActionListener(action);
  56. bCheckLifeOk.setBackground(Color.GRAY);
  57. add(bCheckLifeOk);
  58.  
  59. bCheckLifeChancel = new JButton("Chancel");
  60. bCheckLifeChancel.setBounds(120, 220, 90, 20);
  61. bCheckLifeChancel.addActionListener(action);
  62. bCheckLifeChancel.setBackground(Color.GRAY);
  63. add(bCheckLifeChancel);
  64.  
  65. life1 = new JLabel("4 life");
  66. life1.setBounds(160, 100, 90, 20);
  67. add(life1);
  68.  
  69. life2 = new JLabel("3 life");
  70. life2.setBounds(160, 140, 90, 20);
  71. add(life2);
  72.  
  73. life3 = new JLabel("2 life");
  74. life3.setBounds(160, 180, 90, 20);
  75. add(life3);
  76.  
  77. lTry = new JLabel(); //"you have : " + LifeNum + " attempts"
  78. lTry.setVisible(Bf);
  79. lTry.setBounds(220, 280, 150, 30);
  80. add(lTry);
  81.  
  82. lresult = new JLabel(); //num is more(example)
  83. lresult.setBounds(370, 140, 150, 20);
  84. lresult.setVisible(Bf);
  85. add(lresult);
  86.  
  87. lNumb = new JLabel("Print unknown number");
  88. lNumb.setBounds(370, 80, 150, 20);
  89. lNumb.setVisible(false);
  90. add(lNumb);
  91.  
  92. lGuess = new JLabel();
  93. lGuess.setText("Guess the number from 0 to 10");
  94. lGuess.setBounds(350, 20, 540, 20);
  95. lGuess.setVisible(Bf);
  96. add(lGuess);
  97.  
  98. l0 = new JLabel("Choose complexity");
  99. l0.setBounds(45, 70, 520, 20);
  100. add(l0);
  101.  
  102. radioButton = new JRadioButton("Easy");
  103. radioButton.setBounds(40, 100, 100, 20);
  104. radioButton.addActionListener(action);
  105. radioButton.setCursor(RadCursor);
  106. radioButton.setBackground(Color.LIGHT_GRAY);
  107. add(radioButton);
  108.  
  109. radioButton1 = new JRadioButton("Medium");
  110. radioButton1.setBounds(40, 140, 100, 20);
  111. radioButton1.addActionListener(action);
  112. radioButton1.setCursor(RadCursor);
  113. radioButton1.setBackground(Color.LIGHT_GRAY);
  114. add(radioButton1);
  115.  
  116. radioButton2 = new JRadioButton("Difficult");
  117. radioButton2.setBounds(40, 180, 100, 20);
  118. radioButton2.addActionListener(action);
  119. radioButton2.setCursor(RadCursor);
  120. radioButton2.setBackground(Color.LIGHT_GRAY);
  121. add(radioButton2);
  122.  
  123. txt = new JTextField();
  124. txt.setBounds(370, 110, 30, 22);
  125. txt.setVisible(Bf);
  126. txt.setCursor(TxtCursor);
  127. add(txt);
  128. }
  129. public class Act implements ActionListener {
  130. private int changerValComplex,changerValAgain;
  131. private boolean Bug;
  132.  
  133. public void actionPerformed(ActionEvent e) {
  134. String sRes = "NoName";//init necessary
  135. if (e.getSource() == bPlay) {
  136.  
  137. while (Bt) { //antilogick down
  138. if (Bug) {
  139. bAgain.setCursor(ErrorCursor);
  140. bCheck.setEnabled(Bt);
  141. txt.setEnabled(Bt);
  142. bPlay.setEnabled(Bf);
  143. lNumb.setVisible(Bt);
  144. lGuess.setVisible(Bt);
  145. txt.setVisible(Bt);
  146. bCheck.setVisible(Bt);
  147. Random = (int) (Math.random() * 11);//logic
  148. return;
  149. } else {
  150. JOptionPane.showMessageDialog(null, "Choose complexity", "Warning", WARNING_MESSAGE);
  151. break;
  152. }
  153. }
  154. } else if (e.getSource() == bAgain) {
  155. UIManager.put("OptionPane.yesButtonText" , " Yes ");
  156. UIManager.put("OptionPane.noButtonText" , "Cancel");
  157. UIManager.put("OptionPane.cancelButtonText", " Exit ");
  158. changerValAgain = JOptionPane.showConfirmDialog(null, "Do you really want to start a new game?", "Ask", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null);
  159. switch (changerValAgain) {
  160. case 0:
  161. radioButton.setVisible(Bt);
  162. radioButton1.setVisible(Bt);
  163. radioButton2.setVisible(Bt);
  164. radioButton.setSelected(Bf);
  165. radioButton1.setSelected(Bf);
  166. radioButton2.setSelected(Bf);
  167.  
  168. radioButton.setEnabled(Bt);
  169. radioButton1.setEnabled(Bt);
  170. radioButton2.setEnabled(Bt);
  171. life1.setEnabled(Bt);
  172. life2.setEnabled(Bt);
  173. life3.setEnabled(Bt);
  174. bPlay.setEnabled(Bt);
  175. life1.setVisible(Bt);
  176. life2.setVisible(Bt);
  177. life3.setVisible(Bt);
  178. Cases = 0;
  179.  
  180. bCheckLifeChancel.setVisible(Bt);
  181. bCheckLifeOk.setVisible(Bt);
  182.  
  183. Bug = false;
  184.  
  185. lTry.setVisible(Bf);
  186. lresult.setVisible(Bf);
  187.  
  188. lGuess.setVisible(Bf);
  189. txt.setVisible(Bf);
  190. txt.setText(null);
  191. bCheck.setVisible(Bf);
  192. lGuess.setVisible(Bf);
  193. lNumb.setVisible(Bf);
  194. txt.setVisible(Bf);
  195. bCheck.setVisible(Bf);
  196. Random = (int) (Math.random() * 11);//logic
  197. break;
  198. case 1:
  199. break;
  200. case 2:
  201. System.exit(1337);
  202. }
  203. } else if (e.getSource() == bCheckLifeChancel) {
  204. radioButton.setVisible(Bt);
  205. radioButton1.setVisible(Bt);
  206. radioButton2.setVisible(Bt);
  207. life1.setVisible(Bt);
  208. life2.setVisible(Bt);
  209. life3.setVisible(Bt);
  210. } else if (e.getSource() == bCheckLifeOk) {
  211. changerValComplex = JOptionPane.showConfirmDialog(null, "Choose ?", "Ask", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null);
  212. switch (changerValComplex) {
  213. case YES_OPTION:
  214. while (Bt) { //antilogick down
  215. if (radioButton.isSelected() == Bt || radioButton1.isSelected() == Bt || radioButton2.isSelected() == Bt) {
  216. Bug = true;
  217. } else {
  218. JOptionPane.showMessageDialog(null, "Choose complexity", "Warning", WARNING_MESSAGE);
  219. break;
  220. }
  221. switch (Cases) {
  222. case 4:
  223. radioButton1.setSelected(Bf);
  224. radioButton2.setSelected(Bf);
  225. radioButton1.setVisible(Bf);
  226. radioButton2.setVisible(Bf);
  227. life2.setVisible(Bf);
  228. life3.setVisible(Bf);
  229. radioButton.setEnabled(Bf);
  230. life1.setEnabled(Bf);
  231. LifeNum = 4;
  232. break;
  233. case 3:
  234. radioButton.setSelected(Bf);
  235. radioButton2.setSelected(Bf);
  236. radioButton.setVisible(Bf);
  237. radioButton2.setVisible(Bf);
  238. life1.setVisible(Bf);
  239. life3.setVisible(Bf);
  240. radioButton1.setEnabled(Bf);
  241. life2.setEnabled(Bf);
  242. LifeNum = 3;
  243. break;
  244. case 2:
  245. radioButton.setSelected(Bf);
  246. radioButton1.setSelected(Bf);
  247. radioButton.setVisible(Bf);
  248. radioButton1.setVisible(Bf);
  249. life1.setVisible(Bf);
  250. life2.setVisible(Bf);
  251. radioButton2.setEnabled(Bf);
  252. life3.setEnabled(Bf);
  253. LifeNum = 2;
  254. break;
  255. }
  256. bCheckLifeOk.setVisible(Bf);
  257. bCheckLifeChancel.setVisible(Bf);
  258. break;
  259. }
  260. case NO_OPTION:
  261. break;
  262. }
  263. } else {
  264. if (e.getSource() == radioButton) {
  265. radioButton1.setSelected(Bf);
  266. radioButton2.setSelected(Bf);
  267. radioButton1.setVisible(Bf);
  268. radioButton2.setVisible(Bf);
  269. life2.setVisible(Bf);
  270. life3.setVisible(Bf);
  271. Cases = 4;//antilogick down
  272. } else if (e.getSource() == radioButton1) {
  273. radioButton.setSelected(Bf);
  274. radioButton2.setSelected(Bf);
  275. radioButton.setVisible(Bf);
  276. radioButton2.setVisible(Bf);
  277. life1.setVisible(Bf);
  278. life3.setVisible(Bf);
  279. Cases = 3;//antilogick down
  280. } else if (e.getSource() == radioButton2) {
  281. radioButton.setSelected(Bf);
  282. radioButton1.setSelected(Bf);
  283. radioButton.setVisible(Bf);
  284. radioButton1.setVisible(Bf);
  285. life1.setVisible(Bf);
  286. life2.setVisible(Bf);
  287. Cases = 2;//antilogick down
  288. }
  289. }
  290. if (e.getSource() == bCheck) {//logic button
  291. boolean b = Bt;
  292. String UserNumberString;
  293. int UserNumber;
  294. lTry.setVisible(Bt);
  295. lresult.setVisible(Bt);
  296.  
  297. while (LifeNum != 0) {
  298. UserNumberString = (txt.getText());
  299. UserNumber = 0;
  300. try {//necessary exception
  301. UserNumber = Integer.parseInt(UserNumberString);
  302. } catch (Exception Change) {
  303. JOptionPane.showMessageDialog(null, "Print number in field", "Warning", INFORMATION_MESSAGE);
  304. }
  305. LifeNum--;
  306. lTry.setText("you have : " + LifeNum + " attempts");
  307.  
  308. if (Random == UserNumber) {
  309. lresult.setText("number is " + Random);
  310. JOptionPane.showMessageDialog(null, "You are won", "Info", PLAIN_MESSAGE);
  311. b = Bf;
  312. LifeNum = 0;
  313. bCheck.setEnabled(Bf);
  314. } else if (Random > UserNumber) {
  315. sRes = "more";
  316. lresult.setText("number is " + sRes);
  317. return;
  318. } else if(Random < UserNumber){
  319. sRes = "less";
  320. lresult.setText("number is " + sRes);
  321. return;
  322. } else if(b == Bt && LifeNum == 0){JOptionPane.showMessageDialog(null, "You are lose", "Info", PLAIN_MESSAGE);}
  323. }
  324. while (b == Bt){
  325. JOptionPane.showMessageDialog(null, "You are lose", "Info", PLAIN_MESSAGE);
  326. bCheck.setEnabled(Bf);
  327. txt.setEnabled(Bf);
  328. break;
  329. }
  330. }
  331. }
  332. }
  333. }
Add Comment
Please, Sign In to add comment