/**
   * The heuristic evaluation function that determines the chances of winning...
   *
   * @param brd board to be evaluated
   * @return score that indicates our chances of winning
   */
  protected static int evalBoard(BreakthroughState brd) {
    int score = 0;
    for (int r = 0; r < BreakthroughState.N; r++) {
      for (int c = 0; c < BreakthroughState.N; c++) {
        if (brd.board[r][c] == BreakthroughState.homeSym) {
          score += (r + 1);
        } else if (brd.board[r][c] == BreakthroughState.awaySym) {
          score -= (BreakthroughState.N - r);
        }
      }
    }

    if (Math.abs(score) > MAX_EVAL_SCORE) {
      System.err.println("Problem with eval");
      System.exit(0);
    }
    return score;
  }
Ejemplo n.º 2
0
  public void alphabetaTT(char[][] map, int d, double a, double b, boolean isHome, Move[] stack) {
    double ev_tem = 0;
    ev_tem = ev2(map, isHome);
    if (d == MAX_DEPTH || Math.abs(ev_tem) == MAX_SCORE) {
      stack[d].score = ev_tem;
      return;
    }
    Board board = new Board(map, isHome, "alphabetatt");
    TTEntry tte = tt.getEntry(board.getHashKey());
    if (tte != null && tte.getDepth() <= d) {
      if (tte.getType() == TTEntry.NT.EXACT) {
        stack[d].score = tte.getScore();
        return;
      }
      if (tte.getType() == TTEntry.NT.LOWERBOUND && tte.getScore() > a) a = tte.getScore();
      else if (tte.getType() == TTEntry.NT.UPPERBOUNT && tte.getScore() < b) b = tte.getScore();
      if (a >= b) {
        stack[d].score = tte.getScore();
        return;
      }
    }

    if (board.getSize() == 0) System.out.println(isHome + "\t" + d);
    Move move;
    if (isHome) {
      double v = Double.NEGATIVE_INFINITY;
      while (board.hasNext()) {
        if (FIRST_LAYER_DEBUG == 1 && d == 0)
          System.out.println("Home: 0 Total: " + board.getSize());
        move = board.next();
        map[move.row1][move.col1] = homeSym;
        map[move.row2][move.col2] = homeSym;
        if (d + 1 == MAX_DEPTH) {
          stack[d + 1].set(move.row1, move.col1, move.row2, move.col2);
        }
        alphabetaTT(map, d + 1, a, b, false, stack);
        if (stack[d + 1].score > v) {
          v = stack[d + 1].score;
          stack[d].set(move.row1, move.col1, move.row2, move.col2, v);
        }
        map[move.row1][move.col1] = emptySym;
        map[move.row2][move.col2] = emptySym;
        a = Math.max(a, stack[d].score);

        if (stack[d].score >= b || stack[d].score == MAX_SCORE) return;
      }
      if (stack[d].score < a)
        tt.store(new TTEntry(board.getHashKey(), stack[d].score, TTEntry.NT.LOWERBOUND, d));
      else if (stack[d].score > b)
        tt.store(new TTEntry(board.getHashKey(), stack[d].score, TTEntry.NT.UPPERBOUNT, d));

      return;
    } else {
      double v = Double.POSITIVE_INFINITY;
      while (board.hasNext()) {
        if (FIRST_LAYER_DEBUG == 1 && d == 0)
          System.out.println("Away: 0 Total: " + board.getSize());
        move = board.next();
        map[move.row1][move.col1] = awaySym;
        map[move.row2][move.col2] = awaySym;
        if (d + 1 == MAX_DEPTH) {
          stack[d + 1].set(move.row1, move.col1, move.row2, move.col2);
        }
        alphabetaTT(map, d + 1, a, b, true, stack);
        if (stack[d + 1].score < v) {
          v = stack[d + 1].score;
          stack[d].set(move.row1, move.col1, move.row2, move.col2, v);
        }
        map[move.row1][move.col1] = emptySym;
        map[move.row2][move.col2] = emptySym;
        b = Math.min(b, stack[d].score);

        if (stack[d].score <= a || stack[d].score == -MAX_SCORE) return;
      }
      if (stack[d].score < a)
        tt.store(new TTEntry(board.getHashKey(), stack[d].score, TTEntry.NT.LOWERBOUND, d));
      else if (stack[d].score > b)
        tt.store(new TTEntry(board.getHashKey(), stack[d].score, TTEntry.NT.UPPERBOUNT, d));
      return;
    }
  }