def updatePoints(board, points, turn): op = mm.getOposition(turn) for i in range(len(board)): if i >= 10 and i < 14: if board[i] != 0: points = initialU(points) if i >= 50 and i < 54: if board[i] != 0: points = initialD(points) if i in [17, 25, 33, 41]: if board[i] != 0: points = initialL(points) if i in [22, 30, 38, 46]: if board[i] != 0: points = initialR(points) if board[0] == turn: points = corner1(points) if board[7] == turn: points = corner2(points) if board[56] == turn: points = corner3(points) if board[63] == turn: points = corner4(points) if board[0] == op: points = corner12(points) if board[7] == op: points = corner22(points) if board[56] == op: points = corner32(points) if board[63] == op: points = corner42(points) points = strat(board, turn, points) return points
def minimax(board, turn, points, depth, isMaximizing): # mm.analize(board) if depth == 0: score = bp.countPoints(board, turn, points) return score moves = mm.analize(board, turn) if isMaximizing: t1, t2 = [], [] opp = mm.getOposition(turn) maxScore = -1000000 for i in moves: newMaxScore, mover = minimax(ff.flipAll( board, turn, i), opp, points, depth - 1, False) t1.append(newMaxScore) t2.append(mover) maxScore = max(maxScore, newMaxScore) mymove = t1.index(maxScore) mymove = moves[mymove] return(maxScore, mymove) else: minScore = 1000000 t3, t4 = [], [] for i in moves: newMinScore, mover = minimax(ff.flipAll( board, turn, i), turn, points, depth - 1, True) t3.append(newMinScore) t4.append(mover) minScore = min(minScore, newMinScore) mymove = t3.index(minScore) mymove = moves[mymove] return(minScore, mymove)
def countPoints(board, turn, points): opposite = mm.getOposition(turn) my, op = 0, 0 for i in range(len(board)): if board[i] == turn: my += points[i] elif board[i] == opposite: op += points[i] return my, op
def turnLeft(board, turn, position): oposite = mm.getOposition(turn) posible = True i = 1 j = [position] while posible: if board[position - i] == '@': return False if board[position - i] == 0: return False j.append(position - i) i += 1 if board[position - i] == turn and board[position - i + 1] == oposite: return j
def turnUp(board, turn, position): oposite = mm.getOposition(turn) posible = True i = 8 j = [position] while posible: if board[position - i] == '@': return False if board[position - i] == 0: return False j.append(position - i) i += 8 if position - i < 0: return False if board[position - i] == turn and board[position - i + 8] == oposite: return j
def strat(board, turn, points): opposite = mm.getOposition(turn) for i in range(len(board)): if i in border: if i in border2[0]: if board[i] == opposite: if board[i + 1] == 0: points[i + 1] += -30 if board[i - 1] == 0: points[i - 1] += -30 if board[i + 2] == 0: points[i + 2] += 20 if board[i - 2] == 0: points[i - 2] += 20 elif i in border2[1]: if board[i] == opposite: if board[i + 8] == 0: points[i + 8] += -30 if board[i - 8] == 0: points[i - 8] += -30 if board[i + 16] == 0: points[i + 16] += 20 if board[i - 16] == 0: points[i - 16] += 20 elif i in border2[2]: if board[i] == opposite: if board[i + 8] == 0: points[i + 8] += -30 if board[i - 8] == 0: points[i - 8] += -20 if board[i + 16] == 0: points[i + 16] += 20 if board[i - 16] == 0: points[i - 16] += 20 elif i in border2[3]: if board[i] == opposite: if board[i + 1] == 0: points[i + 1] += -30 if board[i - 1] == 0: points[i - 1] += -30 if board[i + 2] == 0: points[i + 2] += 20 if board[i - 2] == 0: points[i - 2] += 20 return points
def turnD4(board, turn, position): oposite = mm.getOposition(turn) posible = True i = 7 j = [position] while posible: if position + i > 63: return False if board[position + i] == '@': return False if board[position + i] == 0: return False j.append(position + i) i += 7 if position + i > 63: return False if board[position + i] == turn and board[position + i - 7] == oposite: return j
def minimax2(board, turn, points, depth, isMaximizing, alpha, beta): moves = mm.analize(board, turn) if depth == 0 or len(moves) == 0: score = bp.countPoints(board, turn, points) return score if isMaximizing: t1, t2 = [], [] opp = mm.getOposition(turn) maxScore = -math.inf for i in moves: newBoard = ff.flipAll(board, turn, i) newMaxScore, mover = minimax2( newBoard, opp, points, depth - 1, False, alpha, beta) t1.append(newMaxScore) t2.append(mover) maxScore = max(maxScore, newMaxScore) alpha = max(alpha, maxScore) if beta <= alpha: break mymove = t1.index(maxScore) mymove = moves[mymove] return(maxScore, mymove) else: minScore = math.inf t3, t4 = [], [] for i in moves: newBoard = ff.flipAll(board, turn, i) newMinScore, mover = minimax2( newBoard, turn, points, depth - 1, True, alpha, beta) t3.append(newMinScore) t4.append(mover) minScore = min(minScore, newMinScore) beta = min(beta, minScore) if beta <= alpha: break mymove = t3.index(minScore) mymove = moves[mymove] return(minScore, mymove)