mardi 5 décembre 2017

find_if with lambda and capturing variables

I am trying to solve the knight problem ( in chess ) and I have the following code for all the possible moves that the knight can make:

typedef vector<pair<int,int> > Path;

Path move(const pair<int,int> & pos)
{

    Path allMoves = 
    {
        { (get<0>(pos))+1, (get<1>(pos))+2},
        { (get<0>(pos))+2, (get<1>(pos))+1},
        { (get<0>(pos))+2, (get<1>(pos))-1},
        { (get<0>(pos))+1, (get<1>(pos))-2},

        { (get<0>(pos))-1, (get<1>(pos))-2},
        { (get<0>(pos))-2, (get<1>(pos))-1},
        { (get<0>(pos))-2, (get<1>(pos))+1},
        { (get<0>(pos))-1, (get<1>(pos))+2}
    };

    return allMoves;
}

and I want to filter it using the following conditions:

  • The square is inside the board (no negative positions, does not equal or exceed the board size)
  • The square is not in the given Path

I have tried to do the following:

Path legal_moves( const int size, Path visitedSquares, const pair<int,int> pos )
{

    Path possible_moves = move(pos);

    auto legalMoves = find_if(  possible_moves.begin(),
                                possible_moves.end(),
                                []()
                                {

                                });

}

But I am not sure how to get the first element ( x on the chess board ) and the second element ( y on the chess board ) inside the lambda and check the condition.

Thanks for your time

Aucun commentaire:

Enregistrer un commentaire