Example #1
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
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
Example #3
0
def nextMove(board, color, time, movesRemaining):
    # I will have different depth level as per time remaining
    if time > 120: 
        depth = 8
    elif time > 60: 
        depth = 6
    elif time > 5: 
        depth = 4
    else:
        depth = 2
    #print "DBG", phase, depth, time
    #t1 = datetime.datetime.now()
    moves = getAllPossibleMoves(board, color)
    best = None
    opColor = gamePlay.getOpponentColor(color)
    # If only one move available no need for any computations
    if len(moves) == 1:
        bestMove = moves[0]
        return bestMove
    # Strategy1: If it is first move hardcoding the best move
    if isFirstMove(board, color):
        if color.upper() == 'R':
            bestMove = [11, 15]
        else:
            bestMove = [22, 18]
        return bestMove

    # Strategy2: Remove moves that lead to opponent captures my piece
    for move in moves:
        myBoard = deepcopy(board)
        gamePlay.doMove(myBoard,move)
        # Check if opponents capturing position gone
        if gamePlay.isCapturePossible(myBoard, opColor) == True:
            moves.remove(move)

    # Strategy3: Opponent captures one, but I capture double
    # Check if newBoard is giving Opponent a capture
    opMoves = getAllPossibleMoves(board, opColor)
    # At most of the capture, opMoves will have one element
    if gamePlay.isCapturePossible(board, opColor) == True:
        opMove = opMoves[0]
        opBoard = deepcopy(board)
        gamePlay.doMove(opBoard, opMove)
        myMoves = getAllPossibleMoves(opBoard, color)
        # Check if I can capture two pieces
        if len(myMoves) > 0:
            myMove = myMoves[0]
            # At double-capture, lenghth of the move will be >2
            if len(myMove) > 2:
                # Do nothing, let opponent capture
                pass
            else: 
                # Try to block the capture
                for move in moves:
                    myBoard = deepcopy(board)
                    gamePlay.doMove(myBoard,move)
                    opMoves = getAllPossibleMoves(myBoard, opColor)
                    #Check if opponents capturing position gone
                    if  not gamePlay.isCapturePossible(board, opColor):
                        bestMove = move
                        return bestMove

    # Now start the main MiniMax and alpha-beta here
    for move in moves:
        # Strategy4: If I can double-capture, thats best move, kind of greedy
        if len(move) > 2:
            bestMove = move
            return bestMove
        newBoard = deepcopy(board)
        alpha = -float('inf')
        beta = float('inf')
        # Calling with different depth depending on time remaining in the game
        alphaVal = alphaBeta(newBoard, move, depth, alpha, beta, True, color, opColor)
        if best == None or alphaVal > best:
            bestMove = move
            best = alphaVal

    #t2 = datetime.datetime.now()
    return bestMove