// moves card(s) at, and on top of, position x,y to the "best" position on the board.
// moves to the left most columb in the case of a tie.
// moves to the columb that will create the largest stack of correctly stacked cards.
void Game::autoMoveCards( unsigned int x, unsigned int y )
{

  std::vector< sf::Vector2i > validMoves; // .x is the position, .y is the size of the stack

  if ( ! movableStack( x, y ) )
    return;

  for ( unsigned int i = 0; i < board.size(); i++ )
    if ( i != x )
      if ( validMove( x, y, i ) )
        validMoves.push_back( sf::Vector2i( i, getMovableStackSize(i) ) );

  sf::Vector2i bestMove( -1, -1 );
  for ( unsigned int i = 0; i < validMoves.size(); i++ )
  {
    //std::cout << bestMove.x << ":" << bestMove.y << " & " << validMoves[i].x << ":" << validMoves[i].y << std::endl;

    if ( bestMove.y < validMoves[i].y )
    {
      //std::cout << "new best move" << std::endl;
      bestMove = validMoves[i];
    }
  }

  if ( bestMove.x != -1 )
    moveCards( x, y, bestMove.x );
}
void DBConverter::calcPassoutProbabilities(const vector<CardsSet>& hands, const vector<Card>& widow, 
	const vector<Card>& moves, int _firstPlayer)
{
	int firstPlayer = _firstPlayer;
	int totalCards[NumOfSuits];
	int currentHand = 0;
	for( int i = 0; i < NumOfSuits; i++ ) {
		totalCards[i] = NumOfRanks;
	}
	vector<CardsSet> currentHands = hands;

	CardsSet utilizedCards = EmptyCardsSet;
	
	GetLog() << "ADDING... " << hands.size() << " " << widow.size() << " " << moves.size() << " " << firstPlayer << endl;
	vector<DBSet> dbCandidates;

	for( int firstCard = 0; firstCard + NumOfPlayers <= moves.size(); firstCard += NumOfPlayers ) {
		// Number of current turn
		int moveNumber = firstCard / NumOfPlayers;
		// Moves in current turn
		vector<Card> moveCards(NumOfPlayers);
		for( int i = 0; i < NumOfPlayers; i++ ) {
			moveCards[i] = moves[firstCard + i];
			GetLog() << "MOVE: " << moveCards[i] << endl;
		}
		// Move suit
		Suit moveSuit = GetCardSuit( moveCards[firstPlayer] );
	
		// Checking for errors. 
		GetLog() << "FirstPlayer: " << firstPlayer << endl;
		// Checking possible errors in data
		CheckError( IsSetContainsCard( currentHands[firstPlayer], moveCards[firstPlayer] ), "Bad game: card is not from set" );
		CheckError(	(isPossibleMoveSuit( currentHands, moveCards, moveSuit ) 
			|| (moveNumber < 2 && isPossibleMoveSuit( currentHands, moveCards, GetCardSuit(widow[moveNumber]) ) ) ), 
			"Impossible move suit. Probably it's Rostov passout" 
		);
		CheckError( moveNumber >= 2 || moveSuit == GetCardSuit(widow[moveNumber]), "Impossible move suit. Probably it's Rostov passout" );
		// Adding widow card
		if( moveNumber < 2 ) {
			utilizedCards = AddCardToSet(utilizedCards, widow[moveNumber]);
		}
		
		// Adding pending information about players moves in current turn.
		// This method updates almost all input parameters.
		addPassoutTurn(moveSuit, currentHands, firstPlayer, moveCards, moveNumber, dbCandidates, utilizedCards);
	}
	// Adding candidate layouts to data
	for( int i = 0; i < dbCandidates.size(); i++ ) {
		commitDBSet( dbCandidates[i] );
	}
	GetLog() << "Passout successfully processed" << endl;
}