示例#1
0
def evaluation(board, color):
    global myColor
    color = myColor
    global moveCount
    opponentColor = gamePlay.getOpponentColor(color)
    value = 0
    global fourth_layer
    # Loop through all board positions
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]

        # Eval on opponent pieces <= 7 or for first 10 moves(Be in attacking mode)
        if board[x][y].upper() == color.upper() and (
                countPieces(board, opponentColor) <= 7 or moveCount <= 10):
            value = value + 1
            continue
        elif board[x][y].upper() == opponentColor.upper() and (
                countPieces(board, opponentColor) <= 7 or moveCount <= 10):
            value = value - 1
            continue

# Eval on pieces in fourth layer(pieces will tend to move towards 4th outer most layer ie defensive positions)
        if board[x][y].upper() == color.upper() and piece in fourth_layer:
            value = value + 4
            continue
        elif board[x][y].upper() == opponentColor.upper(
        ) and piece in fourth_layer:
            value = value - 4
            continue

# Eval on pieces in third layer (pieces will tend to move towards 3rd layer on board)
        if board[x][y].upper() == color.upper() and piece in third_layer:
            value = value + 3
            continue
        elif board[x][y].upper() == opponentColor.upper(
        ) and piece in third_layer:
            value = value - 3
            continue

# Eval on pieces in second layer (pieces will tend to move towards 2nd layer on board
        if board[x][y].upper() == color.upper() and piece in second_layer:
            value = value + 2
            continue
        elif board[x][y].upper() == opponentColor.upper(
        ) and piece in second_layer:
            value = value - 2
            continue

# Eval on pieces in first layer  (pieces will tend to move to center two pieces)
        if board[x][y].upper() == color.upper() and piece in first_layer:
            value = value + 1
            continue
        elif board[x][y].upper() == opponentColor.upper(
        ) and piece in first_layer:
            value = value - 1
            continue

    return value
示例#2
0
def minimax(board,depth,alpha,beta,maximizingPlayer):
    global currentPlayerColor 
   
    global opponentPlayerColor
    if depth==0 or not isAnyMovePossible(board, currentPlayerColor) or not isAnyMovePossible(board,opponentPlayerColor):
        if countPieces(board,currentPlayerColor)>7:
            '''
            initial stage and opening moves trying to focus on center and attack
            '''
            return (0.75 * evaluationMovingToCenter(board))+ (0.20 * evaluationAttackFunction(board))+(0.5*evaluationColorVsOpposite(board))
        
        elif countPieces(board,currentPlayerColor)>=6 :
            '''
            middle stage have to be defensive And would also need to be attacktive
            ''' 
            return (0.75 * evaluationMovingToDefense(board))+ (0.15 * evaluationAttackFunction(board))+(0.5*evaluationColorVsOpposite(board)+(0.5*evaluationMakingItKing(board)))
        elif countPieces(board,currentPlayerColor)>4 :
            '''
            middle stage have to be get in center amd attack
             
            '''  
            return (0.20 * evaluationMovingToCenter(board))+ (0.70 * evaluationAttackFunction(board))+(0.5*evaluationColorVsOpposite(board)+(0.5*evaluationMakingItKing(board))+evaluationCanBeAttacked(board))
        else:
            '''
            when 4 or less than four pieces are remaining
            '''   
            return (0.50 * evaluationAttackFunction(board))+(0.30*evaluationColorVsOpposite(board)+(0.20*evaluationMovingToDefense(board))+evaluationCanBeAttacked(board))      
    if maximizingPlayer:
        v = -sys.maxint - 1
        moves = getAllPossibleMoves(board, opponentPlayerColor)
        for move in moves:
            newBoard = deepcopy(board)
            
            gamePlay.doMove(newBoard,move)
            #print "inside max",color
            #color=getOpponentColor(color)
            v = max(v,minimax(newBoard, depth-1, alpha, beta, False))
            alpha = max(alpha,v)
            if beta <= alpha:
                return alpha
        return v
    else:
        v = sys.maxint
        moves = getAllPossibleMoves(board, currentPlayerColor)
        for move in moves:
            newBoard = deepcopy(board)
            gamePlay.doMove(newBoard,move)
            #print "inside min",color
            #color=getOpponentColor(color)
            v = min(v,minimax(newBoard, depth-1, alpha, beta, True))
            beta = min(beta,v)
            if beta <= alpha:
                return beta
        return v
        
def evaluation(board, color):
    global myColor
    color = myColor
    global moveCount
    opponentColor = gamePlay.getOpponentColor(color)
    value = 0
    global fourth_layer
    # Loop through all board positions
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
	
	# Eval on opponent pieces <= 7 or for first 10 moves(Be in attacking mode)        
        if board[x][y].upper() == color.upper() and (countPieces(board, opponentColor) <= 7 or moveCount <= 10):
            value = value + 1
	    continue
        elif board[x][y].upper() == opponentColor.upper() and (countPieces(board, opponentColor) <= 7 or moveCount <= 10):
            value = value - 1 
	    continue
	
	# Eval on pieces in fourth layer(pieces will tend to move towards 4th outer most layer ie defensive positions)
        if board[x][y].upper() == color.upper() and piece in fourth_layer:
            value = value + 4
	    continue
        elif board[x][y].upper() == opponentColor.upper() and piece in fourth_layer:
            value = value - 4 
	    continue
	
	# Eval on pieces in third layer (pieces will tend to move towards 3rd layer on board)               
        if board[x][y].upper() == color.upper() and piece in third_layer:
            value = value + 3
	    continue
        elif board[x][y].upper() == opponentColor.upper() and piece in third_layer:
            value = value - 3 
	    continue
	
	# Eval on pieces in second layer (pieces will tend to move towards 2nd layer on board               
        if board[x][y].upper() == color.upper() and piece in second_layer:
            value = value + 2
	    continue
        elif board[x][y].upper() == opponentColor.upper() and piece in second_layer:
            value = value - 2 
	    continue
	
	# Eval on pieces in first layer  (pieces will tend to move to center two pieces)             
        if board[x][y].upper() == color.upper() and piece in first_layer:
            value = value + 1
	    continue
        elif board[x][y].upper() == opponentColor.upper() and piece in first_layer:
            value = value - 1 
	    continue
	
    return value
def iterativeDeepeningAlphaBetaPruning(board, time, maxRemainingMoves):
    # Set depth limit depending the available time
    
   


    global myColor
    global opponentColor

    # Don't call mini-max, return the best move at the game start according to the player's color. 
    if maxRemainingMoves == 150:
	if myColor == 'r':
		return [11, 15]
	else:
		return [22, 18]

    moves = getAllPossibleMoves(board, myColor)
   
    #return the only move, if any
    if len(moves) == 1:
	return moves[0]
    depth = 4
    myPieces = gamePlay.countPieces(board, myColor)
    opponentPieces = gamePlay.countPieces(board, opponentColor)

    # piece ratio for deciding the depth
    pieceRatio = myPieces/opponentPieces
    if pieceRatio < 1:
	depth = 6

    if time < 30 and pieceRatio < 1: depth = 3
    elif time < 20 and pieceRatio > 1: depth = 2
    elif time < 10: depth = 1 
    bestMove = None
    best = -sys.maxint-1
    for move in moves:
        newBoard = deepcopy(board)
        gamePlay.doMove(newBoard,move)
        #Calling mini-max with alpha-beta pruning

        moveVal = alphaBetaPruning(newBoard, depth,time)
        if best == None or moveVal > best:
            bestMove = move
            best = moveVal
    return bestMove
示例#5
0
def minimax(board, depth, alpha, beta, maximizingPlayer):
    global currentPlayerColor

    global opponentPlayerColor
    if depth == 0 or not isAnyMovePossible(
            board, currentPlayerColor) or not isAnyMovePossible(
                board, opponentPlayerColor):
        if countPieces(board, currentPlayerColor) > 7:
            '''
            initial stage and opening moves trying to focus on center and attack
            '''
            return (0.75 * evaluationMovingToCenter(board)) + (
                0.20 * evaluationAttackFunction(board)) + (
                    0.5 * evaluationColorVsOpposite(board))

        elif countPieces(board, currentPlayerColor) >= 6:
            '''
            middle stage have to be defensive And would also need to be attacktive
            '''
            return (0.75 * evaluationMovingToDefense(board)) + (
                0.15 * evaluationAttackFunction(board)) + (
                    0.5 * evaluationColorVsOpposite(board) +
                    (0.5 * evaluationMakingItKing(board)))
        elif countPieces(board, currentPlayerColor) > 4:
            '''
            middle stage have to be get in center amd attack
             
            '''
            return (0.20 * evaluationMovingToCenter(board)) + (
                0.70 * evaluationAttackFunction(board)) + (
                    0.5 * evaluationColorVsOpposite(board) +
                    (0.5 * evaluationMakingItKing(board)) +
                    evaluationCanBeAttacked(board))
        else:
            '''
            when 4 or less than four pieces are remaining
            '''
            return (0.50 * evaluationAttackFunction(board)) + (
                0.30 * evaluationColorVsOpposite(board) +
                (0.20 * evaluationMovingToDefense(board)) +
                evaluationCanBeAttacked(board))
    if maximizingPlayer:
        v = -sys.maxint - 1
        moves = getAllPossibleMoves(board, opponentPlayerColor)
        for move in moves:
            newBoard = deepcopy(board)

            gamePlay.doMove(newBoard, move)
            #print "inside max",color
            #color=getOpponentColor(color)
            v = max(v, minimax(newBoard, depth - 1, alpha, beta, False))
            alpha = max(alpha, v)
            if beta <= alpha:
                return alpha
        return v
    else:
        v = sys.maxint
        moves = getAllPossibleMoves(board, currentPlayerColor)
        for move in moves:
            newBoard = deepcopy(board)
            gamePlay.doMove(newBoard, move)
            #print "inside min",color
            #color=getOpponentColor(color)
            v = min(v, minimax(newBoard, depth - 1, alpha, beta, True))
            beta = min(beta, v)
            if beta <= alpha:
                return beta
        return v