Example #1
0
  // what's the first step from now to get to bestSituation?
  private String getNextStepToSituation(Situation situation_) {
    System.out.println("retrieving first step to win...");
    int lastStepRecorded = -1;

    while (situation_._parentSituation != null) {
      lastStepRecorded = situation_._actionLedToThisSituation;
      situation_ = situation_._parentSituation;
      System.out.println("going one step back...");
    }

    return AITools.columnToAction(lastStepRecorded);
  }
Example #2
0
    public void computeNextSituations(int maxOrder_) {
      if (_order >= maxOrder_) {
        return;
      }

      int nextPlayer = _playerNumberMadeThisMove == 1 ? 2 : 1;

      // check all possible actions
      for (int col = 0; col < AITools.GRID_COLUMNS; col++) {
        if (callback != null) {
          callback.callback("beginning col " + col);
        }

        // next free row for this column
        int nextFreeIndex = AITools.coordsToIndex(col, AITools.GRID_ROWS - 1);
        while (_grid[nextFreeIndex] != 0) {
          nextFreeIndex = AITools.getGridIndexOnTopOf(nextFreeIndex);
          if (nextFreeIndex < 0) // can't throw in this row. no possible situation
          {
            break;
          }
        }
        if (nextFreeIndex < 0) // can't throw in this row. no possible situation
        {
          continue;
        }

        // copy grid and apply new action
        int[] newGrid = _grid.clone();
        newGrid[nextFreeIndex] = nextPlayer;

        if (_nextSituations == null) {
          _nextSituations = new ArrayList<Situation>();
        }
        Situation newSituation = new Situation(col, nextPlayer, newGrid, _order + 1, this);
        _nextSituations.add(newSituation);
        newSituation._score = 0;

        int gameStatus = AITools.getGameStatus(newGrid);

        // compute follow Situations only if not a an end point
        if (gameStatus == -1) {
          newSituation.computeNextSituations(maxOrder_);
        } else if (gameStatus == playerNumber) {

          this._followingSituationsAreWins++;

          // check: if this situation has the order 1 (next move) and is a win, then take it!
          if (newSituation._order == 1) {
            bestSituation = newSituation;
            System.out.println("TAKE THE WIN: next move will finish match!");
            AITools.visualizeGrid(newGrid);

            break;
          }

          if (bestSituation._parentSituation == null
              || this._followingSituationsAreWins
                  > bestSituation._parentSituation._followingSituationsAreWins) {
            // safety check: up to that situation, there may be no situation where the opponent has
            // a winning chance!
            if (!thereIsASafePathToWin(newSituation)) {
              continue;
            }

            bestSituation = newSituation;
            System.out.println(
                "new best Situation with "
                    + this._followingSituationsAreWins
                    + " win chances with one piece.");
            AITools.visualizeGrid(newGrid);
          }

        } else if (gameStatus != 0) // not draw game -> opponent wins
        {
          this._followingSituationsAreLosses++;

          // System.out.println("found situation with score: " + newSituation._score);
          // AITools.visualizeGrid(newGrid);
        }
      }
    }