コード例 #1
0
ファイル: AIastar.cs プロジェクト: IntelOrca/Snake
        private Node[] GetNeighbourNodes(Node n)
        {
            Point    mid   = n.Location;
            NodeList nodes = new NodeList();

            for (int i = 1; i < (int)Direction.Count; i++)
            {
                Point delta  = DirectionFunctions.GetXY((Direction)i);
                Point newPnt = new Point(mid.X + delta.X, mid.Y + delta.Y);
                if (!mSnakeGame.MapBounds.Contains(newPnt))
                {
                    continue;
                }

                TileType type = mSnakeGame.GetTileAt(newPnt);
                if (type != TileType.Null && type != TileType.Food)
                {
                    continue;
                }

                if (mSnakeGame.IsThereSnakeAt(newPnt))
                {
                    continue;
                }

                nodes.Add(new Node(newPnt));
            }

            return(nodes.ToArray());
        }
コード例 #2
0
 private void FinaliseDirection()
 {
     if (firstDirectionMove != Direction.Null)
     {
         mDirection = firstDirectionMove;
     }
     firstDirectionMove  = secondDirectionMove;
     secondDirectionMove = Direction.Null;
     mHeading            = DirectionFunctions.GetXY(mDirection);
     mHasSnakeMovedSinceDirectionChange = false;
 }
コード例 #3
0
        private void Move()
        {
            FinaliseDirection();
            for (int i = mBody.Count - 1; i >= 1; i--)
            {
                mBody[i] = mBody[i - 1];
            }

            Point delta = DirectionFunctions.GetXY(mDirection);

            Head = new Point(delta.X + Head.X, delta.Y + Head.Y);

            mHasSnakeMovedSinceDirectionChange = true;
        }
コード例 #4
0
        private void AIUpdate()
        {
            //If there is no AI then do nothing
            if (mAI == null)
            {
                return;
            }

            Point     destPoint = mAI.GetNextDestination(Head);
            Direction d         = DirectionFunctions.GetDirection(Head, destPoint);

            firstDirectionMove  = Direction.Null;
            secondDirectionMove = Direction.Null;
            mDirection          = d;

            if (d == Direction.Null && !mExitingLevel)
            {
                Reverse();
                AddScore(-1);
            }
        }
コード例 #5
0
        public void SetDirection(Direction specifiedDirection)
        {
            if (mExitingLevel)
            {
                return;
            }

            if (mDirection == Direction.East || mDirection == Direction.West)
            {
                if ((specifiedDirection == Direction.East || specifiedDirection == Direction.West) && firstDirectionMove != Direction.Null)
                {
                    secondDirectionMove = specifiedDirection;
                }
                else
                {
                    firstDirectionMove = specifiedDirection;
                }
            }
            else if (mDirection == Direction.North || mDirection == Direction.South)
            {
                if ((specifiedDirection == Direction.North || specifiedDirection == Direction.South) && firstDirectionMove != Direction.Null)
                {
                    secondDirectionMove = specifiedDirection;
                }
                else
                {
                    firstDirectionMove = specifiedDirection;
                }
            }
            else
            {
                firstDirectionMove = specifiedDirection;
            }

            if (firstDirectionMove == DirectionFunctions.Opposite(mDirection))
            {
                firstDirectionMove = Direction.Null;
            }
        }
コード例 #6
0
        public SnakeUpdateFlag Update()
        {
            mUpdateCnt = (mUpdateCnt + 1) % (int)((float)SnakeGame.UpdatesPerSecond / mSpeed);
            if (mUpdateCnt != 0)
            {
                return(SnakeUpdateFlag.Normal);
            }

            AIUpdate();

            Direction d = firstDirectionMove;

            if (d == Direction.Null)
            {
                d = mDirection;
            }

            Point delta = DirectionFunctions.GetXY(d);

            Point dest = new Point(delta.X + Head.X, delta.Y + Head.Y);

            if (dest == Head)
            {
                return(SnakeUpdateFlag.Normal);
            }

            //Check if snake has reached level exit
            if (dest.X == mSnakeGame.MapSize.Width / 2 && dest.Y == -1)
            {
                mSpeed        = ExitLevelSpeed;
                mExitingLevel = true;
            }

            //Check if snake is exiting the level
            if (mExitingLevel)
            {
                //Continue the move out of the level
                Move();

                //Check if whole snake has exited
                if (HasCompletelyExited())
                {
                    AddScore(10);
                    return(SnakeUpdateFlag.ReachedExit);
                }

                //Return normal as snake has not completely exited yet
                return(SnakeUpdateFlag.Normal);
            }

            //Check if snake has hit the edge of the level
            if (!mSnakeGame.IsPointInLevel(dest))
            {
                return(SnakeUpdateFlag.HitEdge);
            }

            //Check if snake has hit a snake
            if (mSnakeGame.IsThereSnakeAt(dest))
            {
                return(SnakeUpdateFlag.HitSnake);
            }

            //Get the tile at dest
            TileType type = mSnakeGame.GetTileAt(dest);

            switch (type)
            {
            case TileType.Wall:
                return(SnakeUpdateFlag.HitWall);

            case TileType.Food:
                mSnakeGame.GetFoodAt(dest);
                mFoodCount++;
                AddScore(1);

                ExtendSnake(mExtendSnakeOnFood);
                break;
            }

            //Move the snake
            Move();

            return(SnakeUpdateFlag.Normal);
        }