import SearchEngine from '../SearchEngine' ; import Board from '../../common/Board' ; import Move from '../../common/Move' ; export default class AlphaBeta extends SearchEngine { constructor ( ) { super ( ) ; } ; searchAGoodMove ( board , timeLimit ) { let timeCount = ( new Date ( ) ) . getTime ( ) ; this . bestMove = null ; this . currentBoard = board . clone ( ) ; let score = this . alphabeta ( this . maxDepth , this . evaluator . LOST , this . evaluator . WIN ) ; this . setStatus ( score ) ; console . log ( this . currentBoard . toString ( ) ) ; console . log ( "score:" + score ) ; console . log ( ( new Date ( ) . getTime ( ) - timeCount ) . toString ( ) + "ms" ) ; console . log ( "time limit:" + timeLimit + "ms" ) ; return this . bestMove ; } private alphabeta ( depth , alpha , beta ) { let score = this . quickEvaluate ( this . maxDepth - depth ) ; if ( score != 0 ) return score ; if ( depth === 0 ) return this . evaluator . evaluate ( this . currentBoard ) ; let possible = this . generator . getPossibleMoves ( this . currentBoard ) ; if ( possible . length == 0 ) { return this . stalemate ( this . maxDepth - depth ) ; } for ( let move of possible ) { this . doMove ( move ) ; score = - this . alphabeta ( depth - 1 , - beta , - alpha ) ; this . undoMove ( ) ; if ( score >= beta ) return beta ; if ( score > alpha ) { alpha = score ; if ( depth === this . maxDepth ) { this . bestMove = move . clone ( ) ; } } } return alpha ; } } ;