public Move move(Situation situation, int timeLeft) { resourceManager.startTurn(sideOfAI, timeLeft); // creating fallback mode; Move nextMove = moveEvaluator.getFallBackMove(situation); try { // popping out of this loop when time limiter hits the wall or nextMove is known to be Winning // game path. for (int searchDepth = 1; searchDepth < resourceManager.calculateCutDepth(timeLeft); searchDepth++) { nextMove = moveEvaluator.getBesMove(situation, searchDepth); if (moveEvaluator.isPathOfWinningMove(nextMove)) { return nextMove; } } } catch (SoftTimeLimitException e) { // catch Halting condition, at the moment nextMove has best possible move (naturally // incomplete stateSearch is discarded). return nextMove; } catch (Exception e) { e.printStackTrace(); return moveEvaluator.getFallBackMove(situation); } // To get here we have been searched all the game states, and then some but not found winning // condition... so. return nextMove; }
public Itself(Random rnd) { // create heuristics heuristics = new SimpleHeuristics(); heuristics.setWeights(WeigthOwner.own, weigths(2, 10, 250, 100)); heuristics.setWeights(WeigthOwner.enemy, weigths(0, 20, 500, 0)); // create move evaluator moveEvaluator = new AlphaBetaPruning(); moveEvaluator.setRandomEngine(rnd); moveEvaluator.setHeuristics(heuristics); // create Resource Manager resourceManager = new ResourceManager(TOTAL_GAMETIME, MAX_TURNS); }
public void start(Engine engine, Side side) { sideOfAI = side; // init move Evaluator moveEvaluator.setAISide(side); moveEvaluator.setEngine(engine); moveEvaluator.setHaltingCondition( new IHaltingCondition() { @Override public void isTimeLimitReached() throws SoftTimeLimitException { if (resourceManager.timeLimitReached() || resourceManager.memoryLimitReached()) { throw new SoftTimeLimitException(); } } }); // init heuristics heuristics.setSide(side); heuristics.setEngine(engine); heuristics.setMode(IHeuristics.Mode.aggressive); heuristics.init(); }