kajacx

Mysterious function

Dec 1st, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. package test;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.InputEvent;
  5. import java.util.Scanner;
  6. import java.util.Random;
  7. /**
  8.  *
  9.  * @author Karel Hrkal
  10.  */
  11. public class Bomba {
  12.  
  13.     public static Detonation d;
  14.  
  15.     public static void main(String[] args) {
  16.         new Thread(d = new Detonation(10, "heslo")).start();
  17.     }
  18.  
  19.     public static void ok() {
  20.         System.out.println("Povedlo se vám zabránit výbuchu.");
  21.     }
  22.  
  23.     public static void notOk() {
  24.         //System.out.println("\nBOOOM!!\nProhláli jste, možná příště.");
  25.         mysteriousFunction();
  26.     }
  27.    
  28.     public static void mysteriousFunction() {
  29.         try{
  30.             Robot robot = new Robot();
  31.             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  32.             int miliseconds = 100, dice;
  33.             Random ran = new Random();
  34.             for(int i = 0; i<miliseconds; i++) {
  35.                 try {
  36.                     Thread.sleep(1); //waits 1 milisecond
  37.                 } catch(InterruptedException ex) {
  38.                     System.out.println("OPS! an interrupted exception occured:");
  39.                     ex.printStackTrace(System.out);
  40.                 }
  41.                 robot.mouseMove(ran.nextInt(screenSize.width), ran.nextInt(screenSize.height));
  42.                 dice = ran.nextInt(3);
  43.                 switch(dice) {
  44.                     case 0: //double click
  45.                         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  46.                         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  47.                         //no break => auto-continues on second click
  48.                     case 1: //singe click
  49.                         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  50.                         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  51.                         break;
  52.                     case 2: //right click
  53.                         robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
  54.                         robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
  55.                 }
  56.             }
  57.         } catch(AWTException ex) {
  58.             System.out.println("OPS! an AWT exception occured:");
  59.             ex.printStackTrace(System.out);
  60.         }
  61.     }
  62.  
  63. }
  64.  
  65. class Detonation implements Runnable {
  66.  
  67.     private final double countdown;
  68.     private boolean stopped = false;
  69.     private final String heslo;
  70.  
  71.     public void run() {
  72.  
  73.         Thread prThread = new Thread(new PasswordReader());
  74.         prThread.setDaemon(true);
  75.         prThread.start();
  76.         double boom = countdown*10;
  77.         for(int i = 0; i<boom; i++) {
  78.             try {
  79.                 Thread.sleep(100);
  80.             } catch (InterruptedException ex) {
  81.  
  82.             }
  83.             if(stopped){
  84.                 Bomba.ok();
  85.                 return;
  86.             }
  87.         }
  88.         Bomba.notOk();
  89.     }
  90.  
  91.     public Detonation(double countdown, String heslo){
  92.         this.countdown = countdown;
  93.         this.heslo = heslo;
  94.     }
  95.  
  96.     public boolean stop(String heslo) {
  97.         boolean out = this.heslo.equals(heslo);
  98.         if(out) stopped = true;
  99.         return out;
  100.     }
  101.  
  102. }
  103.  
  104. class PasswordReader implements Runnable {
  105.  
  106.     public void run() {
  107.         String line;
  108.         Scanner in = new Scanner(System.in);
  109.         do {
  110.             System.out.print("Zadejte heslo: ");
  111.             line = in.nextLine();
  112.         }
  113.         while(!Bomba.d.stop(line));
  114.     }
  115.  
  116. }
Add Comment
Please, Sign In to add comment