예제 #1
0
    /// <summary>
    /// Tries to move Pawn that belongs to CurrentPlayer from Band (if any).
    /// </summary>
    /// <returns>Whether we should skip player's turn (because he has pawns on band) or not.
    protected bool MovePawnFromBand()
    {
        if (Band)
        {
            bool bKeepTrying      = false;
            bool bBandHasPawns    = false;
            bool bIsDiceAvailable = false;
            do
            {
                bBandHasPawns    = Band.HasPawns(CurrentPlayer);
                bIsDiceAvailable = IsDiceAvailable();
                bKeepTrying      = bBandHasPawns && bIsDiceAvailable;

                if (bKeepTrying)
                {
                    // Since we're here, it means that there are our pawns on Band
                    // and dices still can be used for moves.

                    // Trying to find new moves that start from Band
                    PossibleMoves = new PossibleMoves(this, Band, Dices, true);

                    // If we found any moves, we can do first one.
                    if (PossibleMoves.HasAnyMoves())
                    {
                        // Doing first available move, we've found
                        PossibleMoves.DoFirstMove();

                        // bKeepTrying is already set to true,
                        // so we don't need to do with it anything else
                        // as we want to repeat whole process remove all
                        // our pawns from Band
                    }
                    else
                    {
                        // If we reached this place, it means that the we still
                        // have at least one pawn placed on the Band. Since we
                        // couldn't move it (had no possible moves) we have to skip
                        // our turn according to backgammon rules.

                        bKeepTrying = false;
                    }
                }
            }while (bKeepTrying);

            bBandHasPawns    = Band.HasPawns(CurrentPlayer);
            bIsDiceAvailable = IsDiceAvailable();

            if (bBandHasPawns || !bIsDiceAvailable)
            {
                return(true);
            }
        }
        else
        {
            Logger.Error(this, "Couldn't move pawns from Band because reference to it is null.");
        }

        return(false);
    }