Exemple #1
0
        public DecompressedBoardState(int boardHistory)
        {
            BoardHistoryUtils.ValidateBoardHistory(
                boardHistory,
                out int currentTurn,
                out int playerOneBoardSnapshot,
                out int playerTwoBoardSnapshot,
                out var boardResult);

            BoardHistory           = boardHistory;
            CurrentTurn            = currentTurn;
            PlayerOneBoardSnapshot = playerOneBoardSnapshot;
            PlayerTwoBoardSnapshot = playerTwoBoardSnapshot;
            BoardResult            = boardResult;
        }
Exemple #2
0
        public TakeTurnResult TryTakeTurn(int x, int y, BoardPlayer player, out DecompressedBoardState newDecompressedState)
        {
            if (CurrentTurn == 9 || BoardResult != BoardResult.InProgress)
            {
                newDecompressedState = this;
                return(TakeTurnResult.NotInProgress);
            }

            if (player != CurrentPlayer)
            {
                newDecompressedState = this;
                return(TakeTurnResult.WrongPlayer);
            }

            int unsetIndex            = -1;
            int targetIndex           = y * 3 + x;
            int combinedBoardSnapshot = PlayerOneBoardSnapshot | PlayerTwoBoardSnapshot;

            for (int i = 0; i < 9; i++)
            {
                int snapshotIndex = 1 << i;

                if ((combinedBoardSnapshot & snapshotIndex) == 0)
                {
                    unsetIndex++;
                }

                if (targetIndex == i)
                {
                    if ((combinedBoardSnapshot & snapshotIndex) == snapshotIndex)
                    {
                        newDecompressedState = this;
                        return(TakeTurnResult.TileAlreadySet);
                    }
                    else
                    {
                        int newState = BoardHistoryUtils.SetTurn(BoardHistory, CurrentTurn, unsetIndex);
                        newDecompressedState = new DecompressedBoardState(newState);
                        return(TakeTurnResult.Success);
                    }
                }
            }

            newDecompressedState = this;
            return(TakeTurnResult.UnknownFailure);
        }