Exemplo n.º 1
0
 public void run() {
   try {
     for (; ; ) {
       Target t = null;
       synchronized (pending) {
         while (pending.size() == 0) pending.wait();
         t = (Target) pending.removeFirst();
       }
       t.show();
     }
   } catch (InterruptedException x) {
     return;
   }
 }
Exemplo n.º 2
0
  public void run() {
    Map read;
    boolean shouldRead = go_read;
    String command = null;
    long last = 0;

    try {
      /* swallow the banner if requested to do so */
      if (swallow) {
        readResponse();
      }

      while (shouldRead) {
        synchronized (listeners) {
          if (commands.size() > 0) {
            command = (String) commands.removeFirst();
          }
        }

        if (command != null) {
          _sendString(command);
          command = null;
          lastRead = System.currentTimeMillis();
        }

        long now = System.currentTimeMillis();
        if (this.window != null && !this.window.isShowing() && (now - last) < 1500) {
          /* check if our window is not showing... if not, then we're going to switch to a very reduced
          read schedule. */
        } else {
          read = readResponse();
          if (read == null || "failure".equals(read.get("result") + "")) {
            break;
          }

          processRead(read);
          last = System.currentTimeMillis();
        }

        Thread.sleep(100);

        synchronized (listeners) {
          shouldRead = go_read;
        }
      }
    } catch (Exception javaSucksBecauseItMakesMeCatchEverythingFuckingThing) {
      javaSucksBecauseItMakesMeCatchEverythingFuckingThing.printStackTrace();
    }
  }
Exemplo n.º 3
0
    void processPendingTargets() throws IOException {
      synchronized (pending) {
        while (pending.size() > 0) {
          Target t = (Target) pending.removeFirst();
          try {

            t.channel.register(sel, SelectionKey.OP_CONNECT, t);

          } catch (IOException x) {

            t.channel.close();
            t.failure = x;
            printer.add(t);
          }
        }
      }
    }
Exemplo n.º 4
0
  public int ai_move(int[][] board) {
    if (running) {
      System.out.println(
          "This AI appears to be running multiple ai_moves simultaneously. That can't be right.");
    }
    running = true;
    try {
      // System.out.println(2 + Math.random());
      if (recording) {
        if (out == null) {
          try {
            int ind = 1;
            File f = null;
            while (true) {
              try {
                Scanner sc = new Scanner(new File("AIReplay" + ind + ".txt"));
                ind++;
              } catch (Exception e) {
                break;
              }
            }
            out = new PrintWriter(new File("AIReplay" + ind + ".txt"));
            filename = "AIReplay" + ind + ".txt";
            out.println("AI Version: " + VERSION);
          } catch (Exception e) {
            System.out.println("Could not write to file.");
          }
        }
        fprint(board);
      }
      // if (fml == null) fml = new PrintWriter (new File("fmldebug.txt"));
      if (thisAIIsCheating && max(board) < 8) {
        board[0][0] = GameGUI.win_target;
      }
      if (debug2) sc.nextLine();
      if (debug2) print(board);
      if (debug) System.out.println("New cycle.");
      if (debug) sc.nextLine();
      if (dumbai) name += "Dumby";
      turn++;
      if (!queue.isEmpty()) {
        int temp = queue.removeFirst();
        if (temp > 0) {
          running = false;
          return temp;
        }
      }
      int boardsum = 0;
      for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
          boardsum += board[i][j];
        }
      }
      boolean report = debug;
      /*if (Math.random() < 0.0001) {
      report = true;
      for (int i = 0; i < 4; i++) {
      	System.out.println(Arrays.toString(board[i]));
      }
      for (int i = 0; i < 4; i++) {
      	System.out.println(movable(board, i));
      }
      System.out.println();
      sc.nextLine();
      }*/
      if (dumbai) {
        if (!name.endsWith("Dumby")) name += "Dumby";
        System.out.println(turn);
        running = false;
        if (turn % 600 == 599) return KeyEvent.VK_DOWN;
        if (turn % 3 == 0) return KeyEvent.VK_UP;
        if (turn % 6 < 3) return KeyEvent.VK_LEFT;
        return KeyEvent.VK_RIGHT;
      } else {
        if (name.indexOf(".") < 0) name += VERSION;
      }
      // gamestart processing
      /*if(board[0][0] == 0) {
      	if (board[1][0] > board[0][1]) {
      		return KeyEvent.VK_UP;
      	}
      	if (board[1][0] < board[0][1]) {
      		return KeyEvent.VK_LEFT;
      	}
      	if (Math.random() < 0.5) return KeyEvent.VK_UP;
      	return KeyEvent.VK_LEFT;
      }*/
      long[] pref = {10, 20, 1, 1}; // LEFT, UP, RIGHT, DOWN

      // check if moving right/down is safe
      boolean occupied = true;

      for (int i = 0; i < 4; i++) {
        if (board[0][i] == 0) occupied = false;
        if (i < 3 && board[0][i] == board[0][i + 1]) occupied = false;
      }
      if (!occupied) {
        // pref[2] -= 100000000;
      }
      occupied = true;
      for (int i = 0; i < 4; i++) {
        if (board[i][0] == 0) occupied = false;
        if (i < 3 && board[i][0] == board[i + 1][0]) occupied = false;
      }
      if (!occupied) {
        // pref[3] -= 100000000;
      }

      pref[0] += 5;
      pref[1] += 5;

      // System.out.println(6 + Math.random());
      // simulate
      sum_board = sum(board);
      delta_sum_board_7over8 = delta(sum_board * 7 / 8);
      if (debug) print(board);
      max_depth = 0;
      for (int m = 0; m < 4; m++) {
        if (debug) System.out.println("Now testing move: " + m);
        int[][] sim = simulate(board, m);
        if (Arrays.deepEquals(sim, board)) {
          if (out != null) out.println("Move " + m + " invalid; skipping");
          if (GameGUI.out != null) GameGUI.out.println("Move " + m + " invalid; skipping");
          continue;
        }
        long worst = (long) 1999999999 * 1000000000;
        long avg = 0;
        int numt = 0;
        for (int i = 0; i < 4; i++) {
          for (int j = 0; j < 4; j++) {
            if (sim[i][j] > 0) continue;
            sim[i][j] = 2;
            long temp = predictor(sim, iter_max / (int) Math.pow((countBlank(sim) + 1), 1.6), 1);
            if (temp < worst) worst = temp;
            avg += 9 * temp;
            sim[i][j] = 4;
            temp = predictor(sim, iter_max / (int) Math.pow((countBlank(sim) + 1), 1.6), 1);
            if (temp < worst) worst = temp;
            avg += temp;
            sim[i][j] = 0;
            numt += 10;
          }
        }
        if (countBlank(sim) == 0) {
          long temp = predictor(sim, iter_max / (int) pow((countBlank(sim) + 1), 2), 1);
          if (temp < worst) worst = temp;
          avg += temp;
          numt++;
        }
        avg /= numt;
        worst = (worst_weight * worst + avg) / (worst_weight + 1);
        if (countBlank(sim) >= 8 && max(board) < 64) worst = avg;
        if (debug || debug2) System.out.println("Move " + m + " final eval: " + worst);
        if (out != null) out.println("Move " + m + " final eval: " + worst);
        if (GameGUI.out != null) GameGUI.out.println("Move " + m + " final eval: " + worst);
        pref[m] += worst;
      }
      if (debug2) System.out.println("Max depth: " + max_depth);
      if (out != null) out.println("Max depth: " + max_depth);
      if (GameGUI.out != null) GameGUI.out.println("Max depth: " + max_depth);

      // System.out.println(5 + Math.random());
      // process output
      int[] dir = new int[4];
      dir[0] = KeyEvent.VK_LEFT;
      dir[1] = KeyEvent.VK_UP;
      dir[2] = KeyEvent.VK_RIGHT;
      dir[3] = KeyEvent.VK_DOWN;
      if (report) System.out.println("Pref: " + Arrays.toString(pref));
      for (int i = 0; i < 4; i++) {
        int best = 0;
        for (int j = 0; j < 4; j++) {
          if (pref[j] > pref[best]) {
            best = j;
          }
        }
        pref[best] = Long.MIN_VALUE;
        if (movable(board, best)) {
          if (report) {
            report = false;
            if (debug) System.out.println("Chosen: " + best);
            if (debug) sc.nextLine();
          }
          // if (pref[best] < -50000000) queue.add(best - 2);
          running = false;
          return dir[best];
        }
        // System.out.println("Unmovable: " + best);
        // System.out.println("Pref: " + Arrays.toString(pref));
      }
      System.out.println("???");
      for (int i = 0; i < 4; i++) {
        System.out.println(Arrays.toString(board[i]));
      }
      // sc.nextLine();
    } catch (Exception e) {
      e.printStackTrace();
    }
    running = false;
    return KeyEvent.VK_LEFT;
  }