def alphabetamax(self, position, alpha, beta, depthleft): global nposition, isrunning, nalphacut, nbetacut nposition += 1 if not isrunning: raise StopSearchSystemExit if depthleft == 0: evaluator = evm.Evaluator(position.listpiece) position.value = evaluator() msg = position.outputmoves() testfile.write(msg + "\n") return illegalmoves = [] for move in position.moves: position.applymove(move) if self.listpiece.is_white_king_in_check(): position.undomove(move) illegalmoves.append(move) continue child = position.enemy_game_position_func(position.listpiece, position) try: child.moves.sort(key=child.moveorderingkeybypriority, reverse=True) self.alphabetamin(child, alpha, beta, depthleft - 1) except StopSearchSystemExit: position.undomove(move) raise StopSearchSystemExit if child.value >= beta: position.value = beta msg = position.outputmoves() testfile.write(msg + "---------- beta cut-off -----------" + "\n") position.undomove(move) nbetacut += 1 return if child.value > alpha: alpha = child.value position.children.append(child) position.undomove(move) for move in illegalmoves: position.removemove(move) if position.imincheckmate(): position.value = position.imincheckmatevalue msg = position.outputmoves() testfile.write(msg + "********** CHECKMATE - GAME ENDED **********\n") return if position.isstalemate(): position.value = 0 msg = position.outputmoves() testfile.write(msg + "********** DRAW - GAME ENDED **********\n") return position.value = alpha return
def minimaxformin(self, position, depthleft): global nposition nposition += 1 if not isrunning: raise StopSearchSystemExit if depthleft == 0: evaluator = evm.Evaluator(self.listpiece) position.value = evaluator() msg = position.outputmoves() testfile.write(msg + "\n") return illegalmoves = [] for move in position.moves: position.applymove(move) if self.listpiece.is_black_king_in_check(): position.undomove(move) illegalmoves.append(move) continue child = position.enemy_game_position_func(position.listpiece, position) try: self.minimaxformax(child, depthleft - 1) except StopSearchSystemExit: position.undomove(move) raise StopSearchSystemExit position.children.append(child) position.undomove(move) for move in illegalmoves: position.removemove(move) if position.imincheckmate(): position.value = position.imincheckmatevalue msg = position.outputmoves() testfile.write(msg + "****** CHECKMATE - GAME ENDED ******\n") return if position.isstalemate(): position.value = 0 msg = position.outputmoves() testfile.write(msg + "****** DRAW - GAME ENDED ******\n") return minchild = None for child in position.children: if minchild is None: minchild = child else: if minchild.value > child.value: minchild = child position.value = minchild.value return
def alphabetamin(self, position, alpha, beta, depthleft): global nposition, isrunning, nalphacut, nbetacut, nmatch nposition += 1 if not isrunning: raise StopSearchSystemExit record = position.getrecord() if record is not None: position.value = record.score position.bestmove = record.bestmove msg = position.outputmoves() testfile.write( msg + "________ Transposition table match ______________" + "\n") nmatch += 1 return if depthleft == 0: evaluator = evm.Evaluator(position.listpiece) position.value = evaluator() position.updatetranspositiontable(False, False, depthleft, None, True) msg = position.outputmoves() testfile.write(msg + "\n") return illegalmoves = [] for move in position.moves: position.applymove(move) if self.listpiece.is_white_king_in_check(): position.undomove(move) illegalmoves.append(move) continue child = position.enemy_game_position_func(position.listpiece, transpositiontable, position) try: child.moves.sort(key=child.moveorderingkeybypriority, reverse=False) self.alphabetamax(child, alpha, beta, depthleft - 1) except StopSearchSystemExit: position.undomove(move) raise StopSearchSystemExit if child.value <= alpha: position.value = alpha position.updatetranspositiontable(True, False, depthleft, None, False) msg = position.outputmoves() testfile.write(msg + "---------- alpha cut-off -----------" + "\n") position.undomove(move) nalphacut += 1 return if child.value < beta: beta = child.value position.children.append(child) position.undomove(move) for move in illegalmoves: position.removemove(move) if position.imincheckmate(): position.value = position.imincheckmatevalue position.updatetranspositiontable(False, False, depthleft, None, False) msg = position.outputmoves() testfile.write(msg + "********** CHECKMATE - GAME ENDED **********\n") return if position.isstalemate(): position.value = 0 position.updatetranspositiontable(False, False, depthleft, None, False) msg = position.outputmoves() testfile.write(msg + "********** DRAW - GAME ENDED **********\n") return position.value = beta position.updatetranspositiontable(False, False, depthleft, None, False) return