/// <summary>
        /// Parses move notation into a move
        /// </summary>
        /// <param name="moveNotation">Move notation</param>
        private void Parse(string moveNotation, Board board)
        {
            if (moveNotation.Contains("+"))
            {
                int index = moveNotation.IndexOf('+');
                From = new Square(moveNotation.Substring(0, index));

                sbyte team = (sbyte)board.Get(From);
                if (team == -1)
                {
                    return;
                }

                int addValue = int.Parse(moveNotation.Substring(index + 1));
                To = From.Add(addValue, team);
            }
            else
            {
                int split = ParseSplit(moveNotation);
                if (split == -1)
                {
                    //Move is taking out a piece
                    From = null;
                    To   = new Square(moveNotation);
                }
                else
                {
                    From = new Square(moveNotation.Substring(0, split));
                    To   = new Square(moveNotation.Substring(split));
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets all the possible moves for a team given a dice roll
        /// </summary>
        /// <param name="roll">Dice roll</param>
        /// <param name="team">Marble team</param>
        public MoveCollection GetMoves(Position position, DiceRoll roll, sbyte team)
        {
            if (team < 0 || team > TEAM_COUNT)
            {
                return(null);
            }

            sbyte winner = -1;

            if (position.IsWon(out winner))
            {
                return(null);
            }

            MoveCollection moves = new MoveCollection(this, position);

            Square[] marbleSquares = GetMarbles(position, team);

            bool hasMarbleOnSpawn = position.Get(new Square(team, 0)) == team;
            int  activePieces     = GetActivePiecesCount(marbleSquares);

            //Loop until a marble in our base is found
            for (int i = 0; i < marbleSquares.Length; i++)
            {
                //If marble is at start square
                if (marbleSquares[i] == null)
                {
                    //If no team marble is on spawn square
                    if (!hasMarbleOnSpawn)
                    {
                        int[] takeOut = roll.CanTakeOutWith();
                        if (takeOut.Length > 0)
                        {
                            for (int j = 0; j < takeOut.Length; j++)
                            {
                                Square target = new Square(team, takeOut[j]);
                                if (position.Get(target) != team)
                                {
                                    moves.Add(new Move(new PieceMove(null, target), team));
                                }
                            }
                        }
                    }

                    //Add a special case for taking out a piece and using the other die value on another marble
                    if (activePieces > 0)
                    {
                        int[]     combinations = roll.GetDoublesTakeOutCombinations();
                        PieceMove takeOutCombo = new PieceMove(null, new Square(team, 0));
                        for (int p = 0; p < activePieces; p++)
                        {
                            for (int pc = 0; pc < combinations.Length; pc++)
                            {
                                PieceMove correspondant = new PieceMove(marbleSquares[p], marbleSquares[p].Add(combinations[pc], team));

                                if (hasMarbleOnSpawn && !correspondant.From.Equals(takeOutCombo.To))
                                {
                                    continue;
                                }

                                moves.Add(new Move(new PieceMove[] { correspondant, takeOutCombo }, team));
                            }
                        }
                    }

                    break;
                }
            }

            List <int[]> pieceCombinations = GetPieceCombinations(activePieces);

            for (int c = 0; c < pieceCombinations.Count; c++)
            {
                int     pieces      = pieceCombinations[c].Length;
                int[][] pieceValues = roll.GetValues(pieces);
                for (int k = 0; k < pieceValues.Length; k++)
                {
                    PieceMove[] pieceMoves = new PieceMove[pieces];

                    for (int j = 0; j < pieces; j++)
                    {
                        Square marblePiece = marbleSquares[pieceCombinations[c][j]];
                        pieceMoves[j] = new PieceMove(marblePiece, marblePiece.Add(pieceValues[k][j], team));

                        if (j > 0)
                        {
                            if (pieceMoves[j].From.Equals(pieceMoves[j - 1].To))
                            {
                                //Swap the move order
                                PieceMove current = pieceMoves[j];
                                pieceMoves[j]     = pieceMoves[j - 1];
                                pieceMoves[j - 1] = current;
                            }
                        }
                    }

                    Move move = new Move(pieceMoves, team);
                    moves.Add(move);
                }
            }

            return(moves);
        }