Ejemplo n.º 1
0
        IEnumerator Respawn(float time)
        {
            yield return(new WaitForSeconds(time));

            this.state = DotState.ReadyForClick;
            this._renderer.gameObject.SetActive(true);
        }
Ejemplo n.º 2
0
        public int SearchBestMove(byte depth, DotState player, float alpha, float beta)
        {
            int bestMove = 0;

            CalculatedPositionCount = 0;

            MoveGenerator.MaxDepth = depth;
            MoveGenerator.GenerateMoves(player, depth);
            DotState nextPlayer = player.NextPlayer();

            foreach (var move in MoveGenerator.Moves)
            {
                if (alpha < beta)
                {
                    if (Field.MakeMove(move))
                    {
                        CalculatedPositionCount++;
                        MoveGenerator.UpdateMoves();
                        float tmp = -EvaluatePosition((byte)(depth - 1), nextPlayer, -beta, -alpha);
                        Field.UnmakeMove();
                        MoveGenerator.UpdateMoves();
                        if (tmp > alpha)
                        {
                            alpha = tmp;
                            bestMove = move;
                        }
                    }
                }
            }

            return bestMove;
        }
Ejemplo n.º 3
0
        public LinkedGroup(DotState player, int number, List<int> positions)
        {
            Player = player;
            Positions = positions;
            Number = number;

            BuildEnvelope();
        }
Ejemplo n.º 4
0
        public CapsuleView(Context context, int radius, int x, int y) : base(context)
        {
            this.Radius       = radius;
            this.capsuleState = DotState.Empty;

            this.cord   = new BoardCordinate();
            cord.BoardX = x;
            cord.BoardY = y;
        }
Ejemplo n.º 5
0
 public void SetFlowerState(DotState s)
 {
     if (this.state == DotState.Player && s == DotState.Computer)
     {
         throw new Exception("overrride the player dot");
         //Console.WriteLine("overrride the player dot");
     }
     this.state = s;
 }
Ejemplo n.º 6
0
 internal ReportData(
     IGrammarSource       source,
     LanguageData         data,
     ParserConflictInfo[] parserConflicts,
     DotState[]           parserStates)
 {
     this.source = source;
     this.data = data;
     this.parserConflicts = parserConflicts;
     this.parserStates = parserStates;
 }
Ejemplo n.º 7
0
 public bool Match(DotState[][] dots, int x, int y)
 {
     bool result = true;
     // Match all colors.
     result = Match(dots, x, y, true);
     if (!result)
     {
         result = Match(dots, x, y, false);
     }
     return result;
 }
Ejemplo n.º 8
0
 public void SetCapsuleState(DotState ds)
 {
     if (this.capsuleState == DotState.Player && ds == DotState.Computer)
     {
         throw new Exception("overrride the player dot");
         //Console.WriteLine("overrride the player dot");
     }
     this.capsuleState = ds;
     //calls the onDraw() again
     this.Invalidate();
 }
Ejemplo n.º 9
0
        public Core(Field gameField)
        {
            GameField = gameField;
            _dots = new DotState[gameField.Width][];

            for (int x = 1; x <= gameField.Width; x++)
            {
                _dots[x - 1] = new DotState[gameField.Height];
                for (int y = 1; y <= gameField.Height; y++)
                {
                    _dots[x - 1][y - 1] = gameField[Field.GetPosition(x, y)];
                }
            }
        }
Ejemplo n.º 10
0
        private bool Match(DotState[][] dots, int x, int y, bool player0)
        {
            bool result = true;
            foreach (var patternDot in PatternDots)
            {
                int xOffset = x + patternDot.X;
                int yOffset = y + patternDot.Y;
                if (xOffset < 0 || xOffset >= dots.Length || yOffset < 0 || yOffset >= dots[0].Length ||
                    !patternDot.Match(dots[x + patternDot.X][y + patternDot.Y], player0))
                {
                    result = false;
                    break;
                }
            }

            return result;
        }
Ejemplo n.º 11
0
        bool ShouldRender(DotState state, Player toPlayer)
        {
            // Hide the indicator if the owner trait is disabled
            if (trait.IsTraitDisabled)
            {
                return(false);
            }

            // Hide the indicator if no watchers are available
            if (!state.Watcher.Granted && !state.Watcher.GrantedAllies)
            {
                return(false);
            }

            // Hide the indicator if a frozen actor portrait is visible
            if (state.FrozenActor != null && state.FrozenActor.HasRenderables)
            {
                return(false);
            }

            // Hide the indicator if the unit appears to be owned by an allied player
            if (actor.EffectiveOwner != null && actor.EffectiveOwner.Owner != null &&
                toPlayer.IsAlliedWith(actor.EffectiveOwner.Owner))
            {
                return(false);
            }

            // Hide indicator if the actor wouldn't otherwise be visible if there wasn't fog
            foreach (var visibilityModifier in visibilityModifiers)
            {
                if (!visibilityModifier.IsVisible(actor, toPlayer))
                {
                    return(false);
                }
            }

            // Hide the indicator behind shroud
            if (!trait.Info.VisibleInShroud && !toPlayer.Shroud.IsExplored(actor.CenterPosition))
            {
                return(false);
            }

            return(!visibility.IsVisible(actor, toPlayer));
        }
Ejemplo n.º 12
0
        private float EvaluatePosition(byte depth, DotState player, float alpha, float beta)
        {
            if (depth == 0)
                return Estimator.Estimate(player);

            MoveGenerator.GenerateMoves(player, depth);
            DotState nextPlayer = player.NextPlayer();

            foreach (var move in MoveGenerator.Moves)
            {
                if (alpha < beta)
                {
                    if (Field.MakeMove(move))
                    {
                        CalculatedPositionCount++;
                        MoveGenerator.UpdateMoves();
                        float tmp = -EvaluatePosition((byte)(depth - 1), nextPlayer, -beta, -alpha);
                        Field.UnmakeMove();
                        MoveGenerator.UpdateMoves();
                        if (tmp > alpha)
                            alpha = tmp;
                    }
                }
            }

            return alpha;
        }
Ejemplo n.º 13
0
 public ParserState(DotState dotState, LanguageData data)
 {
     this.dotState = dotState;
     this.data     = data;
 }
Ejemplo n.º 14
0
 private void CheckCapturedAndFreed(int position, DotState player)
 {
     _lastSquareCaptureCount++;
     if ((_dots[position] & DotState.RealPutted) == DotState.RealPutted)
     {
         if ((_dots[position] & DotState.RealPlayer) != (DotState)((int)player << DotConstants.RealPlayerShift))
         {
             _lastBaseCaptureCount++;
             LastMoveCaptureCount++;
         }
         else if ((int)(_dots[position] & DotState.SurroundCountMask) >= (int)DotState.FirstSurroundLevel)
         {
             _lastBaseFreedCount++;
             LastMoveFreedCount++;
             _lastSquareFreedCount++;
         }
     }
 }
Ejemplo n.º 15
0
 void BeEaten(int value)
 {
     this._renderer.gameObject.SetActive(false);
     this.state = DotState.Removed;
     StartCoroutine(Respawn(5));
 }
Ejemplo n.º 16
0
 protected void AddCapturedFreedCount(DotState dotColor)
 {
     if (dotColor == DotState.Player0)
     {
         Player0CaptureCount += _lastBaseCaptureCount;
         Player1CaptureCount -= _lastBaseFreedCount;
         Player0Square += _lastSquareCaptureCount;
         Player1Square -= _lastSquareFreedCount;
     }
     else
     {
         Player1CaptureCount += _lastBaseCaptureCount;
         Player0CaptureCount -= _lastBaseFreedCount;
         Player1Square += _lastSquareCaptureCount;
         Player0Square -= _lastSquareFreedCount;
     }
 }
Ejemplo n.º 17
0
 protected void SetCaptureFreeState(int pos, DotState player)
 {
     _dots[pos] |= (DotState)((int)_dots[pos] << DotConstants.RealPlayerShift) & (DotState.RealPlayer | DotState.RealPutted);
     _dots[pos] &= ~DotState.Player;
     _dots[pos] |= DotState.Putted | player | ((_dots[pos] & DotState.SurroundCountMask) + (int)DotState.FirstSurroundLevel);
 }
Ejemplo n.º 18
0
 public bool MakeMove(int position, DotState color)
 {
     DotState oldCurrentPlayer = CurrentPlayer;
     CurrentPlayer = color;
     if (MakeMove(position))
         return true;
     else
     {
         CurrentPlayer = oldCurrentPlayer;
         return false;
     }
 }
Ejemplo n.º 19
0
 public static bool IsBound(this DotState dot, DotState boundCond)
 {
     return (dot & DotState.BoundMask) == boundCond;
 }
Ejemplo n.º 20
0
 // Use this for initialization
 void Start()
 {
     this.state    = DotState.ReadyForClick;
     this.DotValue = Constant.DefaultDotValue;
 }
        private bool IsValueOnlyEpsilonReduceItem(DotItem item, DotState state, int lookahead)
        {
            if (item.Position != 0
                || grammar.IsStartProduction(item.ProductionId)
                || !grammar.IsTailNullable(item)
                || !item.LA.Contains(lookahead))
            {
                return false;
            }

            int epsilonToken = item.Outcome;

            foreach (var parentItem in state.Items)
            {
                if (parentItem == item)
                {
                    continue;
                }

                if (parentItem.NextToken == epsilonToken)
                {
                    if (!grammar.IsTailNullable(parentItem))
                    {
                        // there is at least one rule which needs shift on epsilonToken
                        return false;
                    }

                    if (grammar.HasFirst(parentItem.CreateNextItem(), lookahead))
                    {
                        // One of the subseqent non-terms in parentItem can start parsing with current lookahead.
                        // It means that we need tested epsilonToken production for continue parsing on parentItem.
                        return false;
                    }
                }
            }

            return true;
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Fills kernel items with "spontaneous" lookaheads and returns table for
        /// lookahead propogation.
        /// </summary>
        private Dictionary<Tuple<int, int, int>, List<Tuple<int, int, int>>> DetermineLookaheads(DotState[] lr0states)
        {
            var result = new Dictionary<Tuple<int, int, int>, List<Tuple<int, int, int>>>();
            for (int from = 0; from != lr0states.Length; ++from)
            {
                var fromState = lr0states[from];
                var fromKernel = fromState.KernelItems;

                foreach (var fromItem in fromKernel)
                {
                    // TODO: It is possible to perform closure on the fromItem itself
                    //       and return exsiting item instances in fromState. This way
                    //       we can make DotItem a class (currently struct) and reuse
                    //       instances all over.
                    var J = ClosureLr0(
                        new MutableDotItemSet
                        {
                            new DotItem(fromItem)
                            {
                                LA = TokenSet.Of(PredefinedTokens.Propagated).EditCopy()
                            }
                        });

                    CollectClosureLookaheads(J, grammar);

                    foreach (var closedItem in J)
                    {
                        if (closedItem.IsReduce)
                        {
                            continue;
                        }

                        var X = closedItem.NextToken;
                        var gotoXstate = fromState.GetNext(X);
                        var gotoX = gotoXstate == null ? -1 : gotoXstate.Index;
                        Debug.Assert(gotoX >= 0, "Internal error. Non-existing state.");

                        var nextItemIds = Tuple.Create(gotoX, closedItem.ProductionId, closedItem.Position + 1);

                        foreach (var lookahead in closedItem.LA)
                        {
                            if (lookahead == PredefinedTokens.Propagated)
                            {
                                List<Tuple<int, int, int>> propogatedItems;
                                var key = Tuple.Create(from, fromItem.ProductionId, fromItem.Position);
                                if (!result.TryGetValue(key, out propogatedItems))
                                {
                                    propogatedItems = new List<Tuple<int, int, int>>();
                                    result[key] = propogatedItems;
                                }

                                propogatedItems.Add(nextItemIds);
                            }
                            else
                            {
                                var nextItem = gotoXstate.GetItem(nextItemIds.Item2, nextItemIds.Item3);
                                nextItem.LA.Add(lookahead);
                            }
                        }
                    }
                }
            }

            return result;
        }
Ejemplo n.º 23
0
 public static bool IsEnable(this DotState dot, DotState enableCondition)
 {
     return (dot & DotState.EnableMask) == enableCondition;
 }
Ejemplo n.º 24
0
 public ParserState(DotState dotState, LanguageData data)
 {
     this.dotState = dotState;
     this.data     = data;
 }
Ejemplo n.º 25
0
 public DotTransition(MutableIntSet tokens, DotState to)
 {
     this.Tokens = tokens;
     this.To = to;
 }
Ejemplo n.º 26
0
        protected void AddCapturedDots(int startPosition, DotState player)
        {
            DotState boundCondition = player | DotState.Putted | DotState.Bound;

            _dots[startPosition] |= DotState.Tagged;

            _lastBaseCaptureCount = 0;
            _lastBaseFreedCount = 0;
            _lastSquareCaptureCount = 0;
            _lastSquareFreedCount = 0;
            var pos = startPosition;
            _tempList.Clear();
            _tempList.Add(startPosition);

            while (_tempList.Count != 0)
            {
                pos = _tempList.Last();
                _tempList.RemoveAt(_tempList.Count - 1);
                CheckCapturedAndFreed(pos, player);

                _surroundPositions.Add((short)pos);

                if (!_dots[pos - 1].IsBound(boundCondition) && !_dots[pos - 1].IsTagged())
                {
                    _tempList.Add(pos - 1);
                    _dots[pos - 1] |= DotState.Tagged;
                }

                if (!_dots[pos - RealWidth].IsBound(boundCondition) && !_dots[pos - RealWidth].IsTagged())
                {
                    _tempList.Add(pos - RealWidth);
                    _dots[pos - RealWidth] |= DotState.Tagged;
                }

                if (!_dots[pos + 1].IsBound(boundCondition) && !_dots[pos + 1].IsTagged())
                {
                    _tempList.Add(pos + 1);
                    _dots[pos + 1] |= DotState.Tagged;
                }

                if (!_dots[pos + RealWidth].IsBound(boundCondition) && !_dots[pos + RealWidth].IsTagged())
                {
                    _tempList.Add(pos + RealWidth);
                    _dots[pos + RealWidth] |= DotState.Tagged;
                }
            }
        }
Ejemplo n.º 27
0
        private void FillDfaTable(DotState[] states)
        {
            for (int i = 0; i != states.Length; ++i)
            {
                var state = states[i];
                Debug.Assert(i == state.Index);

                foreach (var item in state.Items)
                {
                    if (!item.IsReduce)
                    {
                        int nextToken = item.NextToken;

                        if (canOptimizeReduceStates
                            && item.IsShiftReduce
                            && !state.Transitions.Exists(t => t.Tokens.Contains(nextToken)))
                        {
                            var action = new ParserAction
                            {
                                Kind = ParserActionKind.ShiftReduce,
                                ProductionId = item.ProductionId
                            };

                            AssignAction(i, nextToken, action);
                        }
                        else
                        {
                            var action = new ParserAction
                            {
                                Kind = ParserActionKind.Shift,
                                State = state.GetNextIndex(nextToken)
                            };

                            AssignAction(i, nextToken, action);
                        }
                    }
                    else if (item.IsAugmented)
                    {
                        var action = new ParserAction { Kind = ParserActionKind.Accept };
                        AssignAction(i, PredefinedTokens.Eoi, action);
                    }
                    else
                    {
                        var action = new ParserAction { Kind = ParserActionKind.Reduce, ProductionId = item.ProductionId };
                        foreach (var lookahead in item.LA)
                        {
                            AssignAction(i, lookahead, action);
                        }
                    }
                }
            }
        }
Ejemplo n.º 28
0
 public static bool IsPlayerPutted(this DotState dot, DotState player)
 {
     return (dot & DotState.EnableMask) == (DotState.Putted | player);
 }
Ejemplo n.º 29
0
        private DotState[] BuildLr0ItemSets()
        {
            var result = new List<DotState>();

            var initialItemSet = ClosureLr0(new MutableDotItemSet
                {
                    new DotItem(grammar.AugmentedProduction, 0)
                    {
                        LA = TokenSet.Mutable()
                    }
                });
            result.Add(new DotState(0, initialItemSet));

            bool addedStatesInRound;

            do
            {
                addedStatesInRound = false;

                for (int i = 0; i != result.Count; ++i)
                {
                    var itemSet = result[i].Items;

                    foreach (var token in GetOutTokens(itemSet))
                    {
                        var nextStateItems = GoTo(itemSet, token);

                        CollectClosureLookaheads(nextStateItems, grammar);
                        if (nextStateItems.Count == 0)
                        {
                            throw new InvalidOperationException("Internal error: next state cannot be empty");
                        }

                        var nextState = result.Find(state => state.Items.Equals(nextStateItems));
                        if (nextState == null)
                        {
                            addedStatesInRound = true;
                            nextState = new DotState(result.Count, nextStateItems);
                            result.Add(nextState);
                        }

                        if (result[i].AddTransition(token, nextState, TokenSet))
                        {
                            addedStatesInRound = true;
                        }
                    }
                }
            }
            while (addedStatesInRound);

            StateSet = new BitSetType(result.Count);

            return result.ToArray();
        }
Ejemplo n.º 30
0
        private bool GetInputDotForEmptyBase(int centerPos, DotState Player,
            out int inputChainDot, out int inputSurroundedDot)
        {
            if (_dots[centerPos + 1].IsPlayerPutted(Player))
            {
                inputChainDot = 0;
                inputSurroundedDot = 0;
                return false;
            }

            inputSurroundedDot = centerPos + 1;
            var pos = inputSurroundedDot;
            GetNextPos(centerPos, ref pos);
            var k = 0;
            while (!_dots[pos].IsPlayerPutted(Player) && (k < 8))
            {
                GetNextPos(centerPos, ref pos);
                k++;
            }
            inputChainDot = pos;

            return k != 8;
        }
Ejemplo n.º 31
0
 private void SubCapturedFreedCount(DotState dotColor)
 {
     if (dotColor == DotState.Player0)
     {
         if (LastMoveCaptureCount > 0)
         {
             Player0CaptureCount -= LastMoveCaptureCount;
             Player1CaptureCount += LastMoveFreedCount;
         }
         else
             Player1CaptureCount += LastMoveCaptureCount;
     }
     else
     {
         if (LastMoveCaptureCount > 0)
         {
             Player1CaptureCount -= LastMoveCaptureCount;
             Player0CaptureCount += LastMoveFreedCount;
         }
         else
             Player0CaptureCount += LastMoveCaptureCount;
     }
 }
Ejemplo n.º 32
0
        public List<int> GetInputDots(int centerPos, DotState player)
        {
            var result = new List<int>(4);

            DotState enableCond = player | DotState.Putted;
            if (!_dots[centerPos - 1].IsEnable(enableCond))
            {
                if (_dots[centerPos - RealWidth - 1].IsEnable(enableCond))
                    result.Add(centerPos - RealWidth - 1);
                else if (_dots[centerPos - RealWidth].IsEnable(enableCond))
                    result.Add(centerPos - RealWidth);
            }

            if (!_dots[centerPos + RealWidth].IsEnable(enableCond))
            {
                if (_dots[centerPos + RealWidth - 1].IsEnable(enableCond))
                    result.Add(centerPos + RealWidth - 1);
                else if (_dots[centerPos - 1].IsEnable(enableCond))
                    result.Add(centerPos - 1);
            }

            if (!_dots[centerPos + 1].IsEnable(enableCond))
            {
                if (_dots[centerPos + RealWidth + 1].IsEnable(enableCond))
                    result.Add(centerPos + RealWidth + 1);
                else if (_dots[centerPos + RealWidth].IsEnable(enableCond))
                    result.Add(centerPos + RealWidth);
            }

            if (!_dots[centerPos - RealWidth].IsEnable(enableCond))
            {
                if (_dots[centerPos - RealWidth + 1].IsEnable(enableCond))
                    result.Add(centerPos - RealWidth + 1);
                else if (_dots[centerPos + 1].IsEnable(enableCond))
                    result.Add(centerPos + 1);
            }

            return result;
        }
Ejemplo n.º 33
0
        private bool FillAmbiguousTokenActions(DotState[] states, bool isGlr)
        {
            for (int i = 0; i != states.Length; ++i)
            {
                var state = states[i];

                foreach (var ambToken in grammar.AmbiguousSymbols)
                {
                    var validTokenActions = new Dictionary<int,int>();
                    foreach (int token in ambToken.Tokens)
                    {
                        int cell = data.Get(i, token);
                        if (cell == 0)
                        {
                            continue;
                        }

                        validTokenActions.Add(token, cell);
                    }

                    switch (validTokenActions.Count)
                    {
                        case 0:
                            // AmbToken is entirely non-acceptable for this state
                            data.Set(i, ambToken.Index, 0);
                            break;
                        case 1:
                            {
                                var pair = validTokenActions.First();
                                if (pair.Key == ambToken.MainToken)
                                {
                                    // ambToken action is the same as for the main token
                                    data.Set(i, ambToken.Index, pair.Value);
                                }
                                else
                                {
                                    // Resolve ambToken to a one of the underlying tokens.
                                    // In runtime transition will be acceptable when this token
                                    // is in Msg and non-acceptable when this particular token
                                    // is not in Msg.
                                    var action = new ParserAction { Kind = ParserActionKind.Resolve, Value1 = pair.Key };
                                    data.Set(i, ambToken.Index, ParserAction.Encode(action));
                                }
                            }

                            break;
                        default:
                            if (validTokenActions.Values.Distinct().Count() == 1)
                            {
                                // Multiple tokens but with the same action
                                goto case 1;
                            }

                            if (!isGlr)
                            {
                                return false;
                            }

                            // This kind of ambiguity requires GLR to follow all alternate tokens
                            {
                                var pair = validTokenActions.First();
                                var forkAction = new ParserAction { Kind = ParserActionKind.Fork, Value1 = pair.Key };
                                data.Set(i, ambToken.Index, ParserAction.Encode(forkAction));
                            }

                            break;
                    }
                }
            }

            return true;
        }
        private void FillDfaTable(DotState[] states)
        {
            for (int i = 0; i != states.Length; ++i)
            {
                var state = states[i];

                foreach (var item in state.Items)
                {
                    if (!item.IsReduce)
                    {
                        int nextToken = item.NextToken;

                        if (canOptimizeReduceStates
                            && item.IsShiftReduce
                            && !state.Transitions.Exists(t => t.Tokens.Contains(nextToken)))
                        {
                            var action = new ParserAction
                            {
                                Kind         = ParserActionKind.ShiftReduce,
                                ProductionId = item.ProductionId,
                                Size         = (short)item.Size
                            };

                            AddAction(i, nextToken, action);
                        }
                        else
                        {
                            var action = new ParserAction
                            {
                                Kind  = ParserActionKind.Shift,
                                State = state.GetNextIndex(nextToken)
                            };

                            AddAction(i, nextToken, action);
                        }
                    }

                    bool isStartRule = item.IsAugmented;

                    if (item.IsReduce || grammar.IsTailNullable(item))
                    {
                        ParserAction action;

                        if (isStartRule)
                        {
                            if (item.Position == 0)
                            {
                                continue;
                            }
                            else
                            {
                                action = new ParserAction { Kind = ParserActionKind.Accept };
                            }
                        }
                        else
                        {
                            action = new ParserAction
                            {
                                Kind         = ParserActionKind.Reduce,
                                ProductionId = item.ProductionId,
                                Size         = (short)item.Position
                            };
                        }

                        foreach (var lookahead in item.LA)
                        {
                            if (!IsValueOnlyEpsilonReduceItem(item, state, lookahead))
                            {
                                AddAction(i, lookahead, action);
                            }
                        }
                    }
                }
            }
        }