예제 #1
0
파일: GameBoard.cs 프로젝트: tswaugh/Mzinga
        public static GameBoard ParseGameString(string gameString, bool trusted = false)
        {
            if (string.IsNullOrWhiteSpace(gameString))
            {
                throw new ArgumentNullException(nameof(gameString));
            }

            string[] split = gameString.Split(BoardStringSeparator);

            if (!EnumUtils.TryParseExpansionPieces(split[0], out ExpansionPieces expansionPieces))
            {
                throw new ArgumentException("Couldn't parse expansion pieces.", nameof(gameString));
            }

            GameBoard gb = new GameBoard(expansionPieces);

            for (int i = 3; i < split.Length; i++)
            {
                string moveString = NotationUtils.NormalizeBoardSpaceMoveString(split[i]);
                Move   move       = NotationUtils.ParseMoveString(gb, moveString);

                if (trusted)
                {
                    gb.TrustedPlay(move, moveString);
                }
                else
                {
                    gb.Play(move, moveString);
                }
            }

            return(gb);
        }
예제 #2
0
        public Board(string boardString)
        {
            if (string.IsNullOrWhiteSpace(boardString))
            {
                throw new ArgumentNullException(nameof(boardString));
            }

            string[] split = boardString.Split(BoardStringSeparator);

            if (!EnumUtils.TryParseExpansionPieces(split[0], out ExpansionPieces expansionPieces))
            {
                throw new ArgumentException("Couldn't parse expansion pieces.", nameof(boardString));
            }

            InitPieces(expansionPieces);

            string boardStateString = split[1];

            if (!Enum.TryParse(boardStateString, out BoardState boardState))
            {
                throw new ArgumentException("Couldn't parse board state.", nameof(boardString));
            }
            BoardState = boardState;

            string[] currentTurnSplit = split[2].Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);

            string currentTurnColorString = currentTurnSplit[0];

            if (!Enum.TryParse(currentTurnColorString, out PlayerColor currentTurnColor))
            {
                throw new ArgumentException("Couldn't parse current turn color.", nameof(boardString));
            }

            string currentPlayerTurnString = currentTurnSplit[1];

            if (!int.TryParse(currentPlayerTurnString, out int currentPlayerTurn))
            {
                throw new ArgumentException("Couldn't parse current player turn.", nameof(boardString));
            }

            CurrentTurn = 2 * (currentPlayerTurn - 1) + (int)currentTurnColor;

            Queue <Piece> parsedPieces = new Queue <Piece>(EnumUtils.NumPieceNames);

            for (int i = 3; i < split.Length; i++)
            {
                parsedPieces.Enqueue(new Piece(split[i]));
            }

            while (parsedPieces.Count > 0)
            {
                Piece parsedPiece = parsedPieces.Dequeue();
                if (parsedPiece.InPlay)
                {
                    if (parsedPiece.Position.Stack > 0 && !HasPieceAt(parsedPiece.Position.GetBelow()))
                    {
                        parsedPieces.Enqueue(parsedPiece);
                    }
                    else
                    {
                        Piece piece = GetPiece(parsedPiece.PieceName);
                        MovePiece(piece, parsedPiece.Position, true);
                    }
                }
            }

            if (!IsOneHive())
            {
                throw new ArgumentException("The boardString violates the one-hive rule.", nameof(boardString));
            }
        }