Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rnd_game;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import static javax.swing.JOptionPane.*;
- public class Dream extends JFrame {
- private boolean Bf = false;
- private boolean Bt = true;
- private JButton bPlay, bAgain, bCheck, bCheckLifeOk, bCheckLifeChancel;
- private JLabel lGuess, l0, life1, life2, life3, lname, lresult, lNumb, lTry;
- private JRadioButton radioButton, radioButton1, radioButton2;
- private JTextField txt;
- private Cursor RadCursor, TxtCursor, ErrorCursor;
- private int Cases, LifeNum, Random;
- public Dream(String t) {
- super(t);
- Act action = new Act();
- RadCursor = new Cursor(Cursor.HAND_CURSOR);
- TxtCursor = new Cursor(Cursor.TEXT_CURSOR);
- ErrorCursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
- String UsrName = JOptionPane.showInputDialog(null, "Print your name", "Name", 1); //%User NAMe%
- lname = new JLabel("Hello " + UsrName);
- lname.setBounds(20, 20, 540, 20);
- add(lname);
- bPlay = new JButton("Play");
- bPlay.setBounds(415, 310, 150, 30);
- bPlay.addActionListener(action);
- bPlay.setBackground(Color.GRAY);
- add(bPlay);
- bAgain = new JButton("Again");
- bAgain.setBounds(20, 310, 150, 30);
- bAgain.addActionListener(action);
- bAgain.setBackground(Color.GRAY);
- add(bAgain);
- bCheck = new JButton("Check");
- bCheck.setBounds(420, 110, 100, 22);
- bCheck.setVisible(Bf);
- bCheck.addActionListener(action);
- bCheck.setBackground(Color.GRAY);
- add(bCheck);
- bCheckLifeOk = new JButton("Ok");
- bCheckLifeOk.setBounds(20, 220, 50, 20);
- bCheckLifeOk.addActionListener(action);
- bCheckLifeOk.setBackground(Color.GRAY);
- add(bCheckLifeOk);
- bCheckLifeChancel = new JButton("Chancel");
- bCheckLifeChancel.setBounds(120, 220, 90, 20);
- bCheckLifeChancel.addActionListener(action);
- bCheckLifeChancel.setBackground(Color.GRAY);
- add(bCheckLifeChancel);
- life1 = new JLabel("4 life");
- life1.setBounds(160, 100, 90, 20);
- add(life1);
- life2 = new JLabel("3 life");
- life2.setBounds(160, 140, 90, 20);
- add(life2);
- life3 = new JLabel("2 life");
- life3.setBounds(160, 180, 90, 20);
- add(life3);
- lTry = new JLabel(); //"you have : " + LifeNum + " attempts"
- lTry.setVisible(Bf);
- lTry.setBounds(220, 280, 150, 30);
- add(lTry);
- lresult = new JLabel(); //num is more(example)
- lresult.setBounds(370, 140, 150, 20);
- lresult.setVisible(Bf);
- add(lresult);
- lNumb = new JLabel("Print unknown number");
- lNumb.setBounds(370, 80, 150, 20);
- lNumb.setVisible(false);
- add(lNumb);
- lGuess = new JLabel();
- lGuess.setText("Guess the number from 0 to 10");
- lGuess.setBounds(350, 20, 540, 20);
- lGuess.setVisible(Bf);
- add(lGuess);
- l0 = new JLabel("Choose complexity");
- l0.setBounds(45, 70, 520, 20);
- add(l0);
- radioButton = new JRadioButton("Easy");
- radioButton.setBounds(40, 100, 100, 20);
- radioButton.addActionListener(action);
- radioButton.setCursor(RadCursor);
- radioButton.setBackground(Color.LIGHT_GRAY);
- add(radioButton);
- radioButton1 = new JRadioButton("Medium");
- radioButton1.setBounds(40, 140, 100, 20);
- radioButton1.addActionListener(action);
- radioButton1.setCursor(RadCursor);
- radioButton1.setBackground(Color.LIGHT_GRAY);
- add(radioButton1);
- radioButton2 = new JRadioButton("Difficult");
- radioButton2.setBounds(40, 180, 100, 20);
- radioButton2.addActionListener(action);
- radioButton2.setCursor(RadCursor);
- radioButton2.setBackground(Color.LIGHT_GRAY);
- add(radioButton2);
- txt = new JTextField();
- txt.setBounds(370, 110, 30, 22);
- txt.setVisible(Bf);
- txt.setCursor(TxtCursor);
- add(txt);
- }
- public class Act implements ActionListener {
- private int changerValComplex,changerValAgain;
- private boolean Bug;
- public void actionPerformed(ActionEvent e) {
- String sRes = "NoName";//init necessary
- if (e.getSource() == bPlay) {
- while (Bt) { //antilogick down
- if (Bug) {
- bAgain.setCursor(ErrorCursor);
- bCheck.setEnabled(Bt);
- txt.setEnabled(Bt);
- bPlay.setEnabled(Bf);
- lNumb.setVisible(Bt);
- lGuess.setVisible(Bt);
- txt.setVisible(Bt);
- bCheck.setVisible(Bt);
- Random = (int) (Math.random() * 11);//logic
- return;
- } else {
- JOptionPane.showMessageDialog(null, "Choose complexity", "Warning", WARNING_MESSAGE);
- break;
- }
- }
- } else if (e.getSource() == bAgain) {
- UIManager.put("OptionPane.yesButtonText" , " Yes ");
- UIManager.put("OptionPane.noButtonText" , "Cancel");
- UIManager.put("OptionPane.cancelButtonText", " Exit ");
- changerValAgain = JOptionPane.showConfirmDialog(null, "Do you really want to start a new game?", "Ask", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null);
- switch (changerValAgain) {
- case 0:
- radioButton.setVisible(Bt);
- radioButton1.setVisible(Bt);
- radioButton2.setVisible(Bt);
- radioButton.setSelected(Bf);
- radioButton1.setSelected(Bf);
- radioButton2.setSelected(Bf);
- radioButton.setEnabled(Bt);
- radioButton1.setEnabled(Bt);
- radioButton2.setEnabled(Bt);
- life1.setEnabled(Bt);
- life2.setEnabled(Bt);
- life3.setEnabled(Bt);
- bPlay.setEnabled(Bt);
- life1.setVisible(Bt);
- life2.setVisible(Bt);
- life3.setVisible(Bt);
- Cases = 0;
- bCheckLifeChancel.setVisible(Bt);
- bCheckLifeOk.setVisible(Bt);
- Bug = false;
- lTry.setVisible(Bf);
- lresult.setVisible(Bf);
- lGuess.setVisible(Bf);
- txt.setVisible(Bf);
- txt.setText(null);
- bCheck.setVisible(Bf);
- lGuess.setVisible(Bf);
- lNumb.setVisible(Bf);
- txt.setVisible(Bf);
- bCheck.setVisible(Bf);
- Random = (int) (Math.random() * 11);//logic
- break;
- case 1:
- break;
- case 2:
- System.exit(1337);
- }
- } else if (e.getSource() == bCheckLifeChancel) {
- radioButton.setVisible(Bt);
- radioButton1.setVisible(Bt);
- radioButton2.setVisible(Bt);
- life1.setVisible(Bt);
- life2.setVisible(Bt);
- life3.setVisible(Bt);
- } else if (e.getSource() == bCheckLifeOk) {
- changerValComplex = JOptionPane.showConfirmDialog(null, "Choose ?", "Ask", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null);
- switch (changerValComplex) {
- case YES_OPTION:
- while (Bt) { //antilogick down
- if (radioButton.isSelected() == Bt || radioButton1.isSelected() == Bt || radioButton2.isSelected() == Bt) {
- Bug = true;
- } else {
- JOptionPane.showMessageDialog(null, "Choose complexity", "Warning", WARNING_MESSAGE);
- break;
- }
- switch (Cases) {
- case 4:
- radioButton1.setSelected(Bf);
- radioButton2.setSelected(Bf);
- radioButton1.setVisible(Bf);
- radioButton2.setVisible(Bf);
- life2.setVisible(Bf);
- life3.setVisible(Bf);
- radioButton.setEnabled(Bf);
- life1.setEnabled(Bf);
- LifeNum = 4;
- break;
- case 3:
- radioButton.setSelected(Bf);
- radioButton2.setSelected(Bf);
- radioButton.setVisible(Bf);
- radioButton2.setVisible(Bf);
- life1.setVisible(Bf);
- life3.setVisible(Bf);
- radioButton1.setEnabled(Bf);
- life2.setEnabled(Bf);
- LifeNum = 3;
- break;
- case 2:
- radioButton.setSelected(Bf);
- radioButton1.setSelected(Bf);
- radioButton.setVisible(Bf);
- radioButton1.setVisible(Bf);
- life1.setVisible(Bf);
- life2.setVisible(Bf);
- radioButton2.setEnabled(Bf);
- life3.setEnabled(Bf);
- LifeNum = 2;
- break;
- }
- bCheckLifeOk.setVisible(Bf);
- bCheckLifeChancel.setVisible(Bf);
- break;
- }
- case NO_OPTION:
- break;
- }
- } else {
- if (e.getSource() == radioButton) {
- radioButton1.setSelected(Bf);
- radioButton2.setSelected(Bf);
- radioButton1.setVisible(Bf);
- radioButton2.setVisible(Bf);
- life2.setVisible(Bf);
- life3.setVisible(Bf);
- Cases = 4;//antilogick down
- } else if (e.getSource() == radioButton1) {
- radioButton.setSelected(Bf);
- radioButton2.setSelected(Bf);
- radioButton.setVisible(Bf);
- radioButton2.setVisible(Bf);
- life1.setVisible(Bf);
- life3.setVisible(Bf);
- Cases = 3;//antilogick down
- } else if (e.getSource() == radioButton2) {
- radioButton.setSelected(Bf);
- radioButton1.setSelected(Bf);
- radioButton.setVisible(Bf);
- radioButton1.setVisible(Bf);
- life1.setVisible(Bf);
- life2.setVisible(Bf);
- Cases = 2;//antilogick down
- }
- }
- if (e.getSource() == bCheck) {//logic button
- boolean b = Bt;
- String UserNumberString;
- int UserNumber;
- lTry.setVisible(Bt);
- lresult.setVisible(Bt);
- while (LifeNum != 0) {
- UserNumberString = (txt.getText());
- UserNumber = 0;
- try {//necessary exception
- UserNumber = Integer.parseInt(UserNumberString);
- } catch (Exception Change) {
- JOptionPane.showMessageDialog(null, "Print number in field", "Warning", INFORMATION_MESSAGE);
- }
- LifeNum--;
- lTry.setText("you have : " + LifeNum + " attempts");
- if (Random == UserNumber) {
- lresult.setText("number is " + Random);
- JOptionPane.showMessageDialog(null, "You are won", "Info", PLAIN_MESSAGE);
- b = Bf;
- LifeNum = 0;
- bCheck.setEnabled(Bf);
- } else if (Random > UserNumber) {
- sRes = "more";
- lresult.setText("number is " + sRes);
- return;
- } else if(Random < UserNumber){
- sRes = "less";
- lresult.setText("number is " + sRes);
- return;
- } else if(b == Bt && LifeNum == 0){JOptionPane.showMessageDialog(null, "You are lose", "Info", PLAIN_MESSAGE);}
- }
- while (b == Bt){
- JOptionPane.showMessageDialog(null, "You are lose", "Info", PLAIN_MESSAGE);
- bCheck.setEnabled(Bf);
- txt.setEnabled(Bf);
- break;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment