def minimax(victory_cell, cur_state, you, depth, isMax, alpha, beta): game = Board() game.update(cur_state) color = 'W' if you == "BLACK" else 'B' final = evaluated(victory_cell, game, color) if final == you: return 9999 elif final != 'DRAW' and final != None: return -9999 elif final == 'DRAW': return 0 if depth == 0 and game.isPlayable(color): if isMax: return heuristic(victory_cell, game, you, True) else: return heuristic(victory_cell, game, you, False) if isMovesLeft(game, color) == False: return 0 if isMax: best = -9999 for (x, y) in itertools.product(list('12345678'), list('abcdefgh')): if game.isPlaceable(y + x, color): new_game = Board() new_game.update(cur_state) new_game.place(y + x, color) new_state = new_game.getCellLineList() best = max( best, minimax(victory_cell, new_state, you, depth - 1, False, alpha, beta)) alpha = max(alpha, best) if beta <= alpha: break # print('max:', best) return best else: best = 9999 for (x, y) in itertools.product(list('12345678'), list('abcdefgh')): if game.isPlaceable(y + x, color): new_game = Board() new_game.update(cur_state) new_game.place(y + x, color) new_state = new_game.getCellLineList() best = min( best, minimax(victory_cell, new_state, you, depth - 1, True, alpha, beta)) beta = min(beta, best) if beta <= alpha: break # print('min:', best) return best
def findBestMove(state, depth): global best global color global victory_cells game = Board() game.update(state) bestMoveVal = -999999 listPosiblePositions = posiblePositions(game, color) bestStep = listPosiblePositions[0] for step in listPosiblePositions: global best best = 0 # create new game newGame = Board() newGame.update(state) newGame.place(step, color) # get state newState = newGame.getCellLineList() # get score of move moveVal = minimax(newState, depth, -999999, 999999, True) # update step if (moveVal > bestMoveVal): bestMoveVal = moveVal bestStep = step print('step:', bestStep) return bestStep
def minBot(victory_cell, cell_line, you, alpha, beta, depth): cell = Board() cell.update(cell_line) color = 'W' if you == "BLACK" else 'B' result = is_end(victory_cell, cell, color) if depth == 0 or result != None and result != you: return Heuristic(victory_cell, cell, color, False) # elif result != None and result != you: # return 999998, None, None elif result == you: return -999998, None, None minv = 999999 px = None py = None cur_weight = getWeightSquares(cell, color) for (x, y) in itertools.product(list('12345678'), list('abcdefgh')): if cell.isPlaceable(y + x, color): new_cell = Board() new_cell.update(cell_line) new_cell.place(y + x, color) new_state = new_cell.getCellLineList() competitior = 'BLACK' if you != "BLACK" else 'WHITE' (m, max_x, max_y) = maxBot(victory_cell, new_state, competitior, alpha, beta, depth - 1) m -= cur_weight if m < minv: minv = m px = x py = y if minv <= alpha: return (minv, px, py) if minv < beta: beta = minv return minv, px, py
def BestMove(victory_cell, cur_state, you, depth): global best game = Board() game.update(cur_state) color = 'W' if you == "BLACK" else 'B' px = None py = None bestVal = -9999 for (x, y) in itertools.product(list('12345678'), list('abcdefgh')): if game.isPlaceable(y + x, color): global best best = 0 new_game = Board() new_game.update(cur_state) new_game.place(y + x, color) new_state = new_game.getCellLineList() moveVal = minimax(victory_cell, new_state, you, depth, True, -9999, 9999) + prior_spot(y + x) / 2 if moveVal > bestVal: px = x py = y bestVal = moveVal return px, py
def minimax(curState, depth, alpha, beta, isMax): global bot global color global competitor global c_color game = Board() game.update(curState) result = evaluated(game) if (result != 'CONTINUE'): if (result == bot): return 999999 elif result == 'DRAW': return 0 else: return -999999 score = 0 if (depth == 0): b_nstep, c_nstep = numberOfStep(game, color) t_p, b_p, c_p = positionScore(game, color) score = findScoreCalculator(game, color) v_score = viectoryCellScore(game, color) if (b_p > c_p): score += 50 if (t_p < 20): if (b_nstep > c_nstep): score += 150 else: score += 50 else: if (b_nstep > c_nstep): score += 200 else: score += 100 else: # c_p > t_p score -= 50 if (t_p < 20): if (b_nstep > c_nstep): score -= 50 else: score -= 150 else: if (b_nstep > c_nstep): score -= 100 else: score -= 200 score += v_score if isMax: return score else: return -score if isMax: best = -999999 listPosiblePositions = posiblePositions(game, color) for step in listPosiblePositions: newGame = Board() newGame.update(curState) newGame.place(step, color) newState = newGame.getCellLineList() best = max(best, minimax(newState, depth - 1, alpha, beta, False)) alpha = max(alpha, best) if (beta <= alpha): break # print('max:', best) return best else: best = 999999 listPosiblePositions = posiblePositions(game, c_color) for step in listPosiblePositions: newGame = Board() newGame.update(curState) newGame.place(step, c_color) newState = newGame.getCellLineList() best = min(best, minimax(newState, depth - 1, alpha, beta, True)) beta = min(beta, best) if (beta <= alpha): break # print('min:', best) return best