Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Move Search(State state)
- {
- timeNow = System.currentTimeMillis();
- Move best = null;
- try
- {
- for(int depth = 0;;depth++)
- {
- best = RootSearch(state, depth);
- }
- }
- catch (TimeIsUpException msg)
- {
- return best;
- }
- }
- private Move RootSearch(State state, int depth) throws TimeIsUpException
- {
- if(timeNow + maxTime < System.currentTimeMillis())
- {
- throw new TimeIsUpException("mamma'in");
- }
- int alpha = -100;
- int beta = 100;
- Move best = null;
- ArrayList<Move> legalMoves;
- legalMoves = state.getLegalMoves(state.getCurrentPlayer());
- for(Move m : legalMoves)
- {
- int value = -ChildSearch(state.getStateByAction(m), -alpha, -beta, depth - 1);
- if(value > alpha) best = m;
- if(alpha >= beta) break;
- }
- return best;
- }
- private int ChildSearch(State state, int alpha, int beta, int depth) throws TimeIsUpException
- {
- if(timeNow + maxTime < System.currentTimeMillis())
- {
- throw new TimeIsUpException("mamma'in");
- }
- if(depth <= 0 || state.isGameOver(state.getCurrentPlayer())) return state.evaluateScore(state.getCurrentPlayer());
- ArrayList<Move> legalMoves;
- legalMoves = state.getLegalMoves(state.getCurrentPlayer());
- for(Move m : legalMoves)
- {
- int value = -ChildSearch(state.getStateByAction(m), -alpha, -beta, depth - 1);
- if(value > alpha) alpha = value;
- if(alpha >= beta) break;
- }
- return alpha;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement