def offense(board, color):
    opposideEnd = None;
    opponentColor = gamePlay.getOpponentColor(color)
    distanceMeasure = 0.0
    count1 = 0
    count2 = 0
    if color == 'r':
   	for i in range(1, 33):
		xy = gamePlay.serialToGrid(i)
		x = xy[0]
		y = xy[1]
		if board[x][y] == color:
			count1 += x
		elif board[x][y] == opponentColor:
			count2 += (7-x)
    elif color == 'w':	
	for i in range(1,33):
		xy = gamePlay.serialToGrid(i)
                x = xy[0]
                y = xy[1]
		if board[x][y] == color:
			count1 += (7-x)
		elif board[x][y] == opponentColor:
                        count2 += x


    scoreMap['offense'] = count1 - count2
def activeDefenseForKings(board, color):
        opponentColor = gamePlay.getOpponentColor(color)
        val1 = 0
	val2 = 0
        forwardRight = (1, -1)
        forwardLeft = (1, 1)
        backwardRight = (-1, -1)
        backwardLeft = (-1, 1)
	postNeighbors = list()
        postNeighbors = [backwardRight, backwardLeft]

	for i in range(1,33):
                xy = gamePlay.serialToGrid(i)
                x = xy[0]
                y = xy[1]
                if board[x][y] == color:
			#for post neighbors of kings, see if there are same colored pawns to rate defensiveness
                        for item in postNeighbors:
                                coord = tuple(sum(x) for x in zip(xy, item))
                                prev_xy = gridToSerial(coord[0], coord[1])
                                if prev_xy in range (1, 33):
                                        new_coord = gamePlay.serialToGrid(prev_xy)
                                        newx = new_coord[0]
                                        newy = new_coord[1]
                                        if board[newx][newy] == color:
                                                val1 += 1


                elif board[x][y] == opponentColor.upper():
                        val2 += 1


	return val1*val2
Esempio n. 3
0
def isFirstMove(board, color):
    
    opponentColor = gamePlay.getOpponentColor(color)
    
    value1 = 0
    value2 = 0
    # Loop through the middle two rows
    for piece in range(13, 21):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
                
        if board[x][y].upper() == color.upper():
            value1 = value1 + 1
        elif board[x][y].upper() == opponentColor.upper():
            value1 = value1 + 1
    
    # Loop through all the rows
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
                
        if board[x][y].upper() == color.upper():
            value2 = value2 + 1
        elif board[x][y].upper() == opponentColor.upper():
            value2 = value2 + 1
    # If it is first move middle two rows have one or less piece
    if value2 == 24 and value1 <= 1:
        return True
    else:
        return False 
def movePawnsToOppositeSide(board, color):
    opposideEnd = None;
    distanceMeasure = 0.0
    
    #get distance measure for moving the pawns to opposite side to make them kings
    if color == 'w':
        oppositeEnd = [1,2,3,4]
        for i in range(5,33):
                xy = gamePlay.serialToGrid(i)
                x = xy[0]
                y = xy[1]
                if board[x][y] == 'w':

		    #using euclidean distance as game works with diagonal movements
                    distanceMeasure += euclideanDistance(xy, oppositeEnd)
    else:
        oppositeEnd = [29,30,31,32]
        for i in range(1,28):
                xy = gamePlay.serialToGrid(i)
                x = xy[0]
                y = xy[1]
                if board[x][y] == 'r':
                    distanceMeasure += euclideanDistance(xy, oppositeEnd)
                    
    scoreMap['makeKings'] = distanceMeasure
Esempio n. 5
0
def evaluation6(board, color):
    
    # If my color is R, baseline is 1,2,3,4, otherwise 29,30,31,32
    list1 = [1, 2, 3, 4]
    list2 = [29, 30, 31, 32]
    
    value = 0
    # When my color is R, base line is list1
    if color.upper() == 'R':
        for piece in list1:
            xy = gamePlay.serialToGrid(piece)
            x = xy[0]
            y = xy[1]
                
            if board[x][y].upper() == color.upper():
                value = value + 1
    # When my color is W, base line is list2
    else:
        for piece in list2:
            xy = gamePlay.serialToGrid(piece)
            x = xy[0]
            y = xy[1]
                
            if board[x][y].upper() == color.upper():
                value = value + 1

    # Lets give weightage 5
    #return value * 5
    return value 
Esempio n. 6
0
def evaluation7(board, color):
    
    opponentColor = gamePlay.getOpponentColor(color)
    list1 = [6, 7, 8, 9, 10, 11, 14, 15, 16, 17, 18, 19, 22, 23, 24, 25, 26, 27]  
    
    value = 0
    # Loop through all board positions
    for piece in list1:
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
                
        if board[x][y].upper() == color.upper():
            if board[x-1][y-1].upper() == color.upper():
                value = value + 1
            elif board[x-1][y+1].upper() == color.upper():
                value = value + 1
            elif board[x+1][y-1].upper() == color.upper():
                value = value + 1
            elif board[x+1][y+1].upper() == color.upper():
                value = value + 1
            
        elif board[x][y].upper() == opponentColor.upper():
            if board[x-1][y-1].upper() == opponentColor.upper():
                value = value - 1
            elif board[x-1][y+1].upper() == opponentColor.upper():
                value = value - 1
            elif board[x+1][y-1].upper() == opponentColor.upper():
                value = value - 1
            elif board[x+1][y+1].upper() == opponentColor.upper():
                value = value - 1
    
    # Let us give weightage 10
    #return value * 10
    return value 
Esempio n. 7
0
def evaluation5(board, color):
    
    opponentColor = gamePlay.getOpponentColor(color)
    # 0th element is for top row from top; 7th element is the bottom row in the board
    list = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24], [25, 26, 27, 28], [29, 30, 31, 32]]
    
    value = 0
    # Loop through all board positions
    for i in range(0, 8):
        for j in range(0, 4):
            piece = list[i][j]
            xy = gamePlay.serialToGrid(piece)
            x = xy[0]
            y = xy[1]
                
            if board[x][y].upper() == color.upper():
                if color.upper() == 'R':
                    # When R pieces reach bottom base line get score 7
                    value = value + i
                elif color.upper() == 'W':
                    # When W pieces reach top base line get score 7
                    value = value + (7-i)
       
            elif board[x][y].upper() == opponentColor.upper():
                if opponentColor.upper() == 'R':
                    # When opponent's R pieces reach bottom base line get score -7
                    value = value - i
                elif opponentColor.upper() == 'W':
                    # When opponent's W pieces reach top base line get score -7
                    value = value - (7-i)

    # Lets give weightage 2
    #return value * 2
    return value 
Esempio n. 8
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
def evaluation3(board, color):
  # Evaluation Function 3(matrix): Corners on all sides are given utmost importance 
  # and hence my pawns will always tend to go towards the corner so that the pawns cannot be captured.
  evalMatrix = [
               [ 0, 4, 0, 4, 0, 4, 0, 4],
               [ 4, 0, 3, 0, 3, 0, 3, 0],
               [ 0, 3, 0, 2, 0, 2, 0, 4], 
               [ 4, 0, 2, 0, 1, 0, 3, 0],
               [ 0, 3, 0, 1, 0, 2, 0, 4],
               [ 4, 0, 2, 0, 2, 0, 3, 0],
               [ 0, 3, 0, 3, 0, 3, 0, 4],
               [ 4, 0, 4, 0, 4, 0, 4, 0]                        
               ]
  opponentColor = gamePlay.getOpponentColor(color)
  value = 0
  for piece in range(1, 33):
    xy = gamePlay.serialToGrid(piece)
    x = xy[0]
    y = xy[1]
            
    if board[x][y].upper() == color.upper():
      value = value + evalMatrix[x][y]
    elif board[x][y].upper() == opponentColor.upper():
      value = value - evalMatrix[x][y]
  return value
def manhattanDistance(tup1, listOfGoals):
    x = tup1[0]
    y = tup1[1]
    hueristicsSum = 0
    for item in listOfGoals:
        (l,m) = gamePlay.serialToGrid(item)
        hueristicsSum += abs(x-l) + abs(y-m)
    return hueristicsSum
Esempio n. 11
0
def evaluation(board):
    global mycolor
    color = mycolor
    opponentColor = gamePlay.getOpponentColor(color)    
    pawn, king, corner, edge, front, defence, defended_pawns = 0, 0, 0, 0, 0, 0, 0
    
    for piece in range(1, 33):
        x, y = gamePlay.serialToGrid(piece)
        coin = board[x][y]

#pawns and kings
        if coin.upper() == color.upper():
            pawn += 1
            if coin == color.upper():
                king += 1
        else:
            pawn -= 1
            if coin == opponentColor.upper():
                king -= 1                

#corner  - safe against attacks              
        if piece in (4,29):
            if color == coin:  corner += 1
            else:              corner -= 1

#edge - edges are safe, so coins must try to go there           
        if piece in (5,13,21,12,20,28):
            if color == coin:   edge += 1
            else:               edge -= 1

#front - if a pawn is <= 3 steps from becoming a king      
        if coin == 'r':
            if piece > 20:
                if color == 'r':     front += 1
                else:                front -= 1
        else:
            if piece < 13:
                if color == 'w':     front += 1
                else:                front -= 1
                
#defended_pawns - if a pawn cannot be attacked
        if coin == 'r' and x > 0 and y > 0 and y < 7 and board[x-1][y+1].upper() == coin.upper() and board[x-1][y-1].upper() == coin.upper():
            if color == 'r':    defended_pawns += 1
            else:               defended_pawns -= 1   
        elif x < 7 and y > 0 and y < 7 and board[x+1][y+1].upper() == coin.upper() and board[x+1][y-1].upper() == coin.upper():
            if color == 'w':    defended_pawns += 1
            else:               defended_pawns -= 1   

#my_defence - the bottom row must not be moved unless opponent arrives, provides the best defence
    for piece in range(1, 5):
        if color == coin == 'r': defence += 1
    for piece in range(29, 33):
        if color == coin == 'w': defence += 1

# Order of importance(descending) is 1.last layer defence 2.number of kings,
#3.number of pawns, 4.defend most pawns, 5.corner, 6.edges, 7.attack(front)
    return (3*pawn + 7*king + 0.34*corner + 0.33*edge + 0.2*front + 25*defence + 0.5*defended_pawns)
def init():
    global xyPositions 
    global cornerPositions
    corners = [1, 2, 3, 4, 5, 12, 13, 20, 21, 28, 29, 30, 31, 32] 
    for c in range(1,33):
        xy = gamePlay.serialToGrid(c)
        xyPositions[c] = (xy[0], xy[1]) 
        if c in corners:
            cornerPositions[c] = True       
Esempio n. 13
0
def evaluationMovingToDefense(board):
    '''
    after the pieces have come to the center and hopefully after some attack trying to move to the defense locations
    '''
    value = 0
    for pos in [12,13]:
        xy = gamePlay.serialToGrid(pos)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == currentPlayerColor.upper() and currentPlayerColor.upper()== 'R' :
            value = value+1
    for pos in [20,21]:
        xy = gamePlay.serialToGrid(pos)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == currentPlayerColor.upper() and currentPlayerColor.upper()== 'W' :
            value = value+1        
    return value
Esempio n. 14
0
def nextMove(board, color, time, movesRemaining):
    moves = getAllPossibleMoves(board, color)
    if len(moves) == 1:
        return moves[0]
    opponentColor = gamePlay.getOpponentColor(color)
    equalMoves = []
    best = None
    alpha = None
    beta = float("inf")
    # If the time remaining < 3 seconds, then just apply simpleGreedy and increase depth according to time
    if time < 3:
        depth = 1
    elif time < 10:
        depth = 2
    elif time < 30:
        depth = 4
    else:
        if movesRemaining > 40:
            depth = 8
        else:
            depth = 6

    for move in moves: # this is the max turn(1st level of minimax), so next should be min's turn
        newBoard = deepcopy(board)
        gamePlay.doMove(newBoard,move)
        #Beta is always inf here as there is no parent MIN node. So no need to check if we can prune or not.
        moveVal = evaluation(newBoard, color, depth, 'min', opponentColor, alpha, beta)
        if best == None or moveVal > best:
            bestMove = move
            best = moveVal
            equalMoves = []
            equalMoves.append(move)
        elif moveVal == best:
            equalMoves.append(move)
        if best > alpha:
            alpha = best
    #So the equalMoves consists of all the moves that have ended up with same value after Minimax evaluation
    if len(equalMoves) > 1:
        #The below logic tries to see if there is any next move that will form a defensive structure from the
        #equalMoves list and returns it.
        for move in equalMoves:
            l = len(move)
            xy = gamePlay.serialToGrid(move[l-1])
            x = xy[0]
            y = xy[1]
            if (x+1) <= 7:
                if (y+1) <= 7 and board[x+1][y+1].lower() == color.lower():
                    return move
                if (y-1) >= 0 and board[x+1][y-1].lower() == color.lower():
                    return move
            if (x-1) >= 0:
                if (y+1) <= 7 and board[x-1][y+1].lower() == color.lower():
                    return move
                if (y-1) >= 0 and board[x-1][y-1].lower() == color.lower():
                    return move
    return bestMove
Esempio n. 15
0
def evaluationMovingToCenter(board):
    '''
    its a good practice to move the coins in the center of the game at opening
    '''
    value = 0
    
    for pos in [14,15,16]:
        xy = gamePlay.serialToGrid(pos)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == currentPlayerColor.upper() and currentPlayerColor.upper()== 'R' :
            value = value+1
    for pos in [17,18,19]:
        xy = gamePlay.serialToGrid(pos)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == currentPlayerColor.upper() and currentPlayerColor.upper()== 'W' :
            value = value+1        
    return value
Esempio n. 16
0
def evaluationMovingToDefense(board):
    '''
    after the pieces have come to the center and hopefully after some attack trying to move to the defense locations
    '''
    value = 0
    for pos in [12, 13]:
        xy = gamePlay.serialToGrid(pos)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == currentPlayerColor.upper(
        ) and currentPlayerColor.upper() == 'R':
            value = value + 1
    for pos in [20, 21]:
        xy = gamePlay.serialToGrid(pos)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == currentPlayerColor.upper(
        ) and currentPlayerColor.upper() == 'W':
            value = value + 1
    return value
def centerPieces(board, color):
	value = 0
	opponentColor = gamePlay.getOpponentColor(color)
	if color == 'w':
		centralLocations = [18,19]
		for item in centralLocations:
			xy = gamePlay.serialToGrid(item)
	                x = xy[0]
        	        y = xy[1]
                	if board[x][y] == 'w':
				value += 1
	else:
		centralLocations = [14,15]
                for item in centralLocations:
                        xy = gamePlay.serialToGrid(item)
                        x = xy[0]
                        y = xy[1]
                        if board[x][y] == 'r':
                                value += 1
	return value
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 activeDefenseForPawns(board, color):
	opponentColor = gamePlay.getOpponentColor(color)
	value = 0
	forwardRight = (1, -1)
        forwardLeft = (1, 1)
        backwardRight = (-1, -1)
        backwardLeft = (-1, 1)
	prevNeighbors = list()
	if color == 'r':
		prevNeighbors = [forwardRight, forwardLeft]
	else:
		prevNeighbors = [backwardRight, backwardLeft]

	for i in range(1,33):
		xy = gamePlay.serialToGrid(i)
                x = xy[0]
                y = xy[1]
		if board[x][y] == color:
   			#for post neighbors of pawns, see if there are same colored pawns to rate defensiveness
			for item in prevNeighbors:
				coord = tuple(sum(x) for x in zip(xy, item))
				prev_xy = gridToSerial(coord[0], coord[1])
				if prev_xy in range (1, 33):
                                        new_coord = gamePlay.serialToGrid(prev_xy)
                                        newx = new_coord[0]
                                        newy = new_coord[1]
                                        if board[newx][newy] == color:
                                                value += 1
                #subtract for opponent                   
		elif board[x][y] == opponentColor:
			for item in prevNeighbors:
				coord = tuple(sum(x) for x in zip(xy, item))
                                prev_xy = gridToSerial(coord[0], coord[1])

                                if prev_xy in range (1, 33):
                                	new_coord = gamePlay.serialToGrid(prev_xy)
                                        newx = new_coord[0]
                                        newy = new_coord[1]
                                        if board[newx][newy] == opponentColor:
                                        	value -= 1
	return value
Esempio n. 20
0
def evaluationCanBeAttacked(board):
    '''
    current player can be attacked
    '''
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if isMyCapturePossibleFromPosition(board, x, y):
            value = -100
    return value
Esempio n. 21
0
def evaluationMovingToCenter(board):
    '''
    its a good practice to move the coins in the center of the game at opening
    '''
    value = 0

    for pos in [14, 15, 16]:
        xy = gamePlay.serialToGrid(pos)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == currentPlayerColor.upper(
        ) and currentPlayerColor.upper() == 'R':
            value = value + 1
    for pos in [17, 18, 19]:
        xy = gamePlay.serialToGrid(pos)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == currentPlayerColor.upper(
        ) and currentPlayerColor.upper() == 'W':
            value = value + 1
    return value
def movePawnsToLoners(board, color):
    listOfGoals = getOpponentsLonerPieces(board, color)
    distanceMeasure = 0.0

    #get all pawns to attack the loner pieces
    for i in range(1,33):
                    xy = gamePlay.serialToGrid(i)
                    x = xy[0]
                    y = xy[1]
                    if board[x][y] == color or board[x][y] == color.upper():
                        distanceMeasure += euclideanDistance(xy, listOfGoals)
    scoreMap['killLoners'] =  distanceMeasure        
def getOpponentsLonerPieces(board, color):
    listOfLoners = list()
    opponentColor = gamePlay.getOpponentColor(color)
    
    #find loners of opponent
    for i in range(1,33):
        xy = gamePlay.serialToGrid(i)
        x = xy[0]
        y = xy[1]
        if board[x][y] == opponentColor.upper() or board[x][y] == opponentColor:
            listOfLoners.append(i)
    return listOfLoners
Esempio n. 24
0
def evaluationCanBeAttacked(board):
    '''
    current player can be attacked
    '''  
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if isMyCapturePossibleFromPosition(board, x, y):
            value = -100
    return value   
Esempio n. 25
0
def evaluation(board, color, depth, turn, opponentColor, alpha, beta):
    if depth > 1: #Comes here depth-1 times and goes to else for leaf nodes.
        depth -= 1
        opti = None
        if turn == 'max':
            moves = getAllPossibleMoves(board, color) #Gets all possible moves for player
            for move in moves:
                nextBoard = deepcopy(board)
                gamePlay.doMove(nextBoard,move)
                if beta > opti:
                    value = evaluation(nextBoard, color, depth, 'min', opponentColor, alpha, beta)
                    if value > opti: #None is less than everything and anything so we don't need opti == None check
                        opti = value
                    if opti > alpha:
                        alpha = opti

        elif turn == 'min':
            moves = getAllPossibleMoves(board, opponentColor) #Gets all possible moves for the opponent
            for move in moves:
                nextBoard = deepcopy(board)
                gamePlay.doMove(nextBoard,move)
                if alpha == None or opti == None or alpha < opti: #None conditions are to check for the first times
                    value = evaluation(nextBoard, color, depth, 'max', opponentColor, alpha, beta)
                    if opti == None or value < opti: #opti = None for the first time
                        opti = value
                    if opti < beta:
                        beta = opti

        return opti # opti will contain the best value for player in MAX turn and worst value for player in MIN turn

    else: #Comes here for the last level i.e leaf nodes
        value = 0
        for piece in range(1, 33):
            xy = gamePlay.serialToGrid(piece)
            x = xy[0]
            y = xy[1]
            #Below, we count the number of kings and men for each color.
            #A player king is 1.5 times more valuable than a player man.
            #An opponent king is 1.5 times worse for the player than an opponent man.
            #By assigning more weight on kings, the AI will prefer killing opponent kings to killing opponent men.
            #It will also prefer saving player kings to saving player men when the situation demands.
            #If a player king is double the value of a man, then AI may choose to sacrifice a man to make a king.
            #To avoid this, a factor of 1.5 has been chosen.
            if board[x][y] == color.lower():
                value += 2
            elif board[x][y] == opponentColor.lower():
                value -= 2
            elif board[x][y] == color.upper():
                value += 3
            elif board[x][y] == opponentColor.upper():
                value -= 3
        return value
Esempio n. 26
0
def evaluation(board, color, depth, turn, opponentColor, alpha, beta):
    if depth > 1: #Comes here depth-1 times and goes to else for leaf nodes.
        depth -= 1
        opti = None
        if turn == 'max':
            moves = getAllPossibleMoves(board, color) #Gets all possible moves for player
            for move in moves:
                nextBoard = deepcopy(board)
                gamePlay.doMove(nextBoard,move)
                if beta > opti:
                    value = evaluation(nextBoard, color, depth, 'min', opponentColor, alpha, beta)
                    if value > opti: #None is less than everything and anything so we don't need opti == None check
                        opti = value
                    if opti > alpha:
                        alpha = opti

        elif turn == 'min':
            moves = getAllPossibleMoves(board, opponentColor) #Gets all possible moves for the opponent
            for move in moves:
                nextBoard = deepcopy(board)
                gamePlay.doMove(nextBoard,move)
                if alpha == None or opti == None or alpha < opti: #None conditions are to check for the first times
                    value = evaluation(nextBoard, color, depth, 'max', opponentColor, alpha, beta)
                    if opti == None or value < opti: #opti = None for the first time
                        opti = value
                    if opti < beta:
                        beta = opti

        return opti # opti will contain the best value for player in MAX turn and worst value for player in MIN turn

    else: #Comes here for the last level i.e leaf nodes
        value = 0
        for piece in range(1, 33):
            xy = gamePlay.serialToGrid(piece)
            x = xy[0]
            y = xy[1]
            #Below, we count the number of kings and men for each color.
            #A player king is 1.5 times more valuable than a player man.
            #An opponent king is 1.5 times worse for the player than an opponent man.
            #By assigning more weight on kings, the AI will prefer killing opponent kings to killing opponent men.
            #It will also prefer saving player kings to saving player men when the situation demands.
            #If a player king is double the value of a man, then AI may choose to sacrifice a man to make a king.
            #To avoid this, a factor of 1.5 has been chosen.
            if board[x][y] == color.lower():
                value += 2
            elif board[x][y] == opponentColor.lower():
                value -= 2
            elif board[x][y] == color.upper():
                value += 3
            elif board[x][y] == opponentColor.upper():
                value -= 3
        return value
def safeKings(board, color):
    opponentColor = gamePlay.getOpponentColor(color)
    corner = [5, 1, 32, 28, 29, 4]
    value = 0
    for king in corner:
	xy = gamePlay.serialToGrid(king)
	x = xy[0]
	y = xy[1]
	if board[x][y] == color.upper():
		value += 1
	
    
    return value
Esempio n. 28
0
def evaluation1(board):
    global currentPlayerColor    
    global opponentPlayerColor
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == currentPlayerColor.upper():
            value = value + 1
        elif board[x][y].upper() == opponentPlayerColor.upper():
            value = value - 1
    return value
Esempio n. 29
0
def evaluation1(board):
    global currentPlayerColor
    global opponentPlayerColor
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == currentPlayerColor.upper():
            value = value + 1
        elif board[x][y].upper() == opponentPlayerColor.upper():
            value = value - 1
    return value
def cornerSquares(board, color):
	drawPositions = [5, 1, 32, 28]
	value = 0
	opponentColor = gamePlay.getOpponentColor(color)
	for item in drawPositions:
		xy = gamePlay.serialToGrid(item)
		x = xy[0]
		y = xy[1]
		if board[x][y] == color or board[x][y] == color.upper():
			value += 1
		elif board[x][y] == opponentColor or board[x][y] == opponentColor.upper():
			value -= 1
	scoreMap['cornerSquare'] = value
def unoccupiedFieldsOnPromotion(board, color):
	opponentColor = gamePlay.getOpponentColor(color)
	value = 0
	if color == 'r':
		oppRow = [29,30,31,32]
	else:
		oppRow = [1,2,3,4]
	for item in oppRow:
		xy = gamePlay.serialToGrid(item)
                x = xy[0]
                y = xy[1]
                if board[x][y] == opponentColor or board[x][y] == opponentColor.upper():
                        value -= 1
	scoreMap['unoccpromo'] = value
Esempio n. 32
0
def evaluation1(board, color):
  # Evaluation Function 1: Counting the number of pawns each player has in the current state
  opponentColor = gamePlay.getOpponentColor(color)
  value = 0
  for piece in range(1, 33):
    xy = gamePlay.serialToGrid(piece)
    x = xy[0]
    y = xy[1]
            
    if board[x][y] == color:
      value = value + 1
    elif board[x][y] == opponentColor:
      value = value - 1
  return value
Esempio n. 33
0
def king_evaluation(board, color, opponentColor):
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]

        if board[x][y] == color.upper():
            value += 1
        elif board[x][y] == opponentColor.upper():
            value -= 1

    value = (value)*5
    return value
Esempio n. 34
0
def evaluation(board, color,opponentColor):
    new_value = 0
    # Loop through all board positions
    for piece in range(1,33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if board[x][y] == color:
            new_value = new_value + 1
        if board[x][y] == opponentColor:
            new_value = new_value - 1
    #accumulated_value = new_value + king_evaluation(board, color, opponentColor) + center_evaluation(board, color, opponentColor) + doublecorner_evaluation(board, color, opponentColor)
    accumulated_value = new_value + king_evaluation(board,color,opponentColor) + protecting_backrowcoins_evaluation(board,color,opponentColor)   
    return accumulated_value
Esempio n. 35
0
def evaluation2(board,color):
  #Evaluation Function 2: Counting the number of kings of each player in the current state
  opponentColor = gamePlay.getOpponentColor(color)
  value = 0
  for piece in range(1, 33):
    xy = gamePlay.serialToGrid(piece)
    x = xy[0]
    y = xy[1]
    if board[x][y] == "R" or board[x][y] == "W":
      if board[x][y] == color:
        value = value + 1.5
      elif board[x][y] == opponentColor:
        value = value - 1.5
  return value
Esempio n. 36
0
def evaluationMakingItKing(board):
    '''
    numbers of kings wrt to opponents kings
    '''
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
    if board[x][y]== board[x][y].upper() and  currentPlayerColor ==  currentPlayerColor.upper():   
        value = value+1
    elif board[x][y]== board[x][y].upper() and  opponentPlayerColor ==  opponentPlayerColor.upper():
        value = value+1
    return value    
def pawnsOnDiagonal(board, color):
    diagonal = [9,14,18,23]
    opponentColor = gamePlay.getOpponentColor(color)
    value = 0

    #loop through pieces on diagonals and find out pawns which are at the risk of being captured
    for item in diagonal:
        xy = gamePlay.serialToGrid(item)
        x = xy[0]
        y = xy[1]
        if board[x][y] == color or board[x][y] == color.upper():
                value += 1
        elif board[x][y] == opponentColor or board[x][y] == opponentColor.upper():
                value -= 1
    scoreMap['pawnsOnDiagonal'] = value
def promotionRowItems(board, color):
	value = 0
	opponentColor = gamePlay.getOpponentColor(color)
	if color == 'r':
		myRow = [1,2,3,4]
		oppRow = [29,30,31,32]
	else:
		myRow = [29,30,31,32]
		oppRow = [1,2,3,4]

	for i in myRow:
		xy = gamePlay.serialToGrid(i)
		x = xy[0]
		y = xy[1]
		if board[x][y] == color or board[x][y] == color.upper():
			value += 1
	
	for i in oppRow:
		xy = gamePlay.serialToGrid(i)
                x = xy[0]
                y = xy[1]
                if board[x][y] == opponentColor or board[x][y] == opponentColor.upper():
                	value -= 1
	scoreMap['promotion'] = value
Esempio n. 39
0
def evaluationColorVsOpposite(board):
    '''number of pieces compared to opponents'''
    global currentPlayerColor    
    global opponentPlayerColor
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        
        if board[x][y].upper() == currentPlayerColor.upper():  
            value = value+1
        elif board[x][y].upper() == opponentPlayerColor.upper():
            value = value - 1    
    return value
Esempio n. 40
0
def evaluation4(board, color):
    
    opponentColor = gamePlay.getOpponentColor(color)
    
    value = 0
    # Loop through all board positions in the list with weightage 4, 3, 2, 1
    # The follwoing positions are at edge across the square board
    #list1 = [1, 2, 3, 4, 5, 12, 13, 20, 21, 28, 29, 30, 31, 32]
    # This is 2 level deep from the edge across the square board
    #list2 = [6, 7, 8, 9, 16, 17, 24, 25, 26, 27]
    # This is 3 level deep from the edge across the square board
    list3 = [10, 11, 14, 19, 22, 23]
    # This is 4 level deep from the edge across the square board
    list4 = [15, 18]

    for piece in list3:
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == color.upper():
            value = value + 1
        elif board[x][y].upper() == opponentColor.upper():
            value = value - 1

    for piece in list4:
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if board[x][y].upper() == color.upper():
            value = value + 2
        elif board[x][y].upper() == opponentColor.upper():
            value = value - 2
    
    # Lets give weightage 3
    #return value * 3 
    return value  
Esempio n. 41
0
def evaluationAttackFunction(board):
    '''
    number of pieces the board can capture
    '''
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if isCapturePossibleFromPosition(board, x, y):
            if board[x][y].upper() == currentPlayerColor.upper():  
                value = value+1
            elif board[x][y].upper() == opponentPlayerColor.upper():
                value = value - 1
    return value         
Esempio n. 42
0
def evaluationColorVsOpposite(board):
    '''number of pieces compared to opponents'''
    global currentPlayerColor
    global opponentPlayerColor
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]

        if board[x][y].upper() == currentPlayerColor.upper():
            value = value + 1
        elif board[x][y].upper() == opponentPlayerColor.upper():
            value = value - 1
    return value
Esempio n. 43
0
def evaluationAttackFunction(board):
    '''
    number of pieces the board can capture
    '''
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if isCapturePossibleFromPosition(board, x, y):
            if board[x][y].upper() == currentPlayerColor.upper():
                value = value + 1
            elif board[x][y].upper() == opponentPlayerColor.upper():
                value = value - 1
    return value
Esempio n. 44
0
def evaluationMakingItKing(board):
    '''
    numbers of kings wrt to opponents kings
    '''
    value = 0
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
    if board[x][y] == board[x][y].upper(
    ) and currentPlayerColor == currentPlayerColor.upper():
        value = value + 1
    elif board[x][y] == board[x][y].upper(
    ) and opponentPlayerColor == opponentPlayerColor.upper():
        value = value + 1
    return value
Esempio n. 45
0
def evaluation2(board, color):
    
    opponentColor = gamePlay.getOpponentColor(color)
    
    value = 0
    # Loop through all board positions
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
                
        if board[x][y] == color.upper():
            value = value + 1
        elif board[x][y] == opponentColor.upper():
            value = value - 1
    
    # Let us give extra weightage 10 
    #return value * 10 
    return value  
Esempio n. 46
0
def center_evaluation(board,color,opponentColor):
    weightage = 0
    player_coins = player_kingcoins = opponent_coins = opponent_kingcoins = 0
    center=[14,15,18,19]
    for piece in center:
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if board[x][y] == color:
            player_coins += 1
        if board[x][y] == color.upper():
                player_kingcoins += 1
        if board[x][y] == opponentColor:
            opponent_coins += 1
        if board[x][y] == opponentColor.upper():
            opponent_kingcoins += 1
    weightage += (player_coins - opponent_coins)*2
    weightage += (player_kingcoins - opponent_kingcoins)*5
    return weightage
def evaluation(board, color):
    # Evaluation function 1
    # Count how many more pieces I have than the opponent

    opponentColor = gamePlay.getOpponentColor(color)

    value = 0
    # Loop through all board positions
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]

        if board[x][y].upper() == color.upper():
            value = value + 1
        elif board[x][y].upper() == opponentColor.upper():
            value = value - 1

    return value
Esempio n. 48
0
def evaluation3(board, color):
    
    opColor = gamePlay.getOpponentColor(color)
    
    value = 0
    
    # Loop through all board positions
    for piece in range(1, 33):
        # Check if I can capture two
        # Check whether a jump possible to all four directions
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
        if gamePlay.canMoveToPosition(board, x, y, x-2, y-2) == True:
            if gamePlay.isCapturePossibleFromPosition(board, x-2, y-2):
                if board[x][y].upper() == color.upper():
                    value = value + 1
                elif board[x][y].upper() == opColor.upper():
                    value = value - 1
        if gamePlay.canMoveToPosition(board, x, y, x-2, y+2) == True:
            if gamePlay.isCapturePossibleFromPosition(board, x-2, y+2):
                if board[x][y].upper() == color.upper():
                    value = value + 1
                elif board[x][y].upper() == opColor.upper():
                    value = value - 1
        if gamePlay.canMoveToPosition(board, x, y, x+2, y-2) == True:
            if gamePlay.isCapturePossibleFromPosition(board, x+2, y-2):
                if board[x][y].upper() == color.upper():
                    value = value + 1
                elif board[x][y].upper() == opColor.upper():
                    value = value - 1
        if gamePlay.canMoveToPosition(board, x, y, x+2, y+2) == True:
            if gamePlay.isCapturePossibleFromPosition(board, x+2, y+2):
                if board[x][y].upper() == color.upper():
                    value = value + 1
                elif board[x][y].upper() == opColor.upper():
                    value = value - 1

    # Lets give weightage 10
    # Let us give extra weightage 10 
    return value 
Esempio n. 49
0
def getAllPossibleMoves(board, color):
    # Get a list of all possible moves of <color>

    moves = []

    isCapturePossible = gamePlay.isCapturePossible(board, color)

    # Loop through all board positions
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]

        # Check whether this board position is our color
        if board[x][y].upper() == color.upper():

            l, isCapture = getAllPossibleMovesAtPosition(board, x, y)

            if isCapturePossible == isCapture:
                for m in l:
                    moves.append(m)
    return moves
Esempio n. 50
0
def gamePhase(board, color):
    
    opponentColor = gamePlay.getOpponentColor(color)
    
    value2 = 0
    
    # Loop through all the rows
    for piece in range(1, 33):
        xy = gamePlay.serialToGrid(piece)
        x = xy[0]
        y = xy[1]
                
        if board[x][y].upper() == color.upper():
            value2 = value2 + 1
        elif board[x][y].upper() == opponentColor.upper():
            value2 = value2 + 1
    if value2 > 18 and value2 <= 24:
        return 1
    elif value2 > 10 and value2 <= 18:
        return 2 
    else:
        return 3