Exemple #1
0
        private bool SnakeBirth(SnakeItem snake)
        {
            // once 7 nodes is contained in our snake, it birth
            count += 1;
            bool found      = false;
            int  snakeValue = snake.SnakeValue;

            if (!Snakes.ContainsKey(snakeValue))
            {
                // created a new node into the dictionary if its value does not exist
                List <SnakeItem> snakesSameValue = new List <SnakeItem>()
                {
                    snake
                };
                Snakes.Add(snakeValue, snakesSameValue);
            }
            else
            {
                // if the same value were found, a new snake is add to a list of the same value
                foreach (var item in Snakes[snakeValue])
                {
                    if (!snake.Equals(item))
                    {
                        // comparing all snakes with same value,
                        // found if each node from a snake differs from the other
                        SnakesFound = new List <SnakeItem>();
                        SnakesFound.Add(snake);
                        SnakesFound.Add(item);
                        return(true);
                    }
                }
                Snakes[snake.SnakeValue].Add(snake);
            }
            return(found);
        }
Exemple #2
0
        public void Add(Snake snake, Canvas canvas)
        {
            Canvas = canvas;

            if (Snakes == null)
            {
                Snakes = new ConcurrentDictionary<string, Snake>();
                Timer = new Timer(onMove, null, 0, 1000/30);
                AddFood();
            }

            if (Snakes.ContainsKey(snake.id))
            {
                return;
            }

            Snakes.TryAdd(snake.id, snake);
            SendSnakes();
        }