Исключение переполнения стека (C #) - PullRequest
1 голос
/ 20 октября 2011

В настоящее время я работаю над игрой Snake, которая может решить саму себя, но когда я активирую ее, обычно после 30 успешных попаданий, мое приложение вылетает с вышеупомянутым исключением либо в System.drawing.dll, либо в System.Windows. Forms.dll.

Проблема обычно возникает в команде «Application.DoEvents ()», но она также встречалась и в некоторых других местах.

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Threading;
using System.IO;

namespace Snake
{
    enum Directions
    {
        Right = 1,
        Left = -1,
        Up = 2,
        Down = -2,
        NoDirection = 0
    }

    public partial class GameForm : Form
    {
        #region Data Members

        SnakeGame gmGame;
        Point pCurrFood;
        Directions dirFirstDirection;
        Directions dirSecondDirection;
        Directions dirHelpDirection;
        Color DEFAULT_COLOUR = Color.White;
        const int SEGMENT_HEIGHT = 10;
        const int SEGMENT_WIDTH = 10;

        #endregion

        #region Ctor

        public GameForm()
        {
            InitializeComponent();
            this.gmGame = new SnakeGame();
            this.dirFirstDirection = Directions.NoDirection;
            this.dirSecondDirection = Directions.NoDirection;
        }

        #endregion

        #region Other Methods

        private void PaintSegment(Graphics gGraphics, Point pnPoint, Color cColour)
        {
            Pen Pen = new Pen(cColour);
            Rectangle Rectangle = new Rectangle(pnPoint.X,
                                                pnPoint.Y,
                                                SEGMENT_HEIGHT,
                                                SEGMENT_WIDTH);
            gGraphics.DrawRectangle(Pen, Rectangle);
            Brush Brush = new SolidBrush(cColour);
            gGraphics.FillRectangle(Brush, Rectangle);
        }

        private void PlaceNewFood()
        {
            Random rRand = new Random();
            int nHeight = rRand.Next(this.panel1.Size.Height);
            int nWidth = rRand.Next(this.panel1.Size.Width);

            while ((nHeight % 10 != 0) || (nWidth % 10 != 0))
            {
                nHeight = rRand.Next(this.panel1.Size.Height - 10);
                nWidth = rRand.Next(this.panel1.Size.Width - 10);

                while (this.gmGame.SnakeQueue.Contains(new Point(nWidth, nHeight)))
                {
                    nHeight = rRand.Next(this.panel1.Size.Height);
                    nWidth = rRand.Next(this.panel1.Size.Width);
                }
            }

            this.pCurrFood = new Point(nWidth, nHeight);
            this.PaintSegment(this.panel1.CreateGraphics(), this.pCurrFood, Color.Red);
        }

        private void SelfSolve()
        {

            this.dirFirstDirection = (Directions)(Math.Sign(this.gmGame.SnakeHead.Y -
                                                            this.pCurrFood.Y) * 2);
            this.dirSecondDirection = (Directions)Math.Sign(this.pCurrFood.X -
                                                            this.gmGame.SnakeHead.X);

            this.ManageSnake(this.dirFirstDirection, this.dirSecondDirection);
        }

        private bool WillCollide(Point pnPointToCheck)
        {
            return ((pnPointToCheck.X > this.panel1.Size.Width) ||
                    (pnPointToCheck.Y > this.panel1.Size.Height) ||
                    (pnPointToCheck.X * pnPointToCheck.Y < 0) ||
                    (this.gmGame.SnakeQueue.Contains(pnPointToCheck)));
        }

        private void ManageSnake(Directions dirFirstSnakeDirection,
                                 Directions dirSecondSnakeDirection)
        {
            Point pnNewHead = this.gmGame.SnakeHead;

            switch (dirFirstSnakeDirection)
            {
                case (Directions.Down):
                {
                    if (this.WillCollide(new Point(pnNewHead.X, pnNewHead.Y + SEGMENT_HEIGHT)))
                    {
                        this.ManageSnake(Directions.NoDirection, dirSecondSnakeDirection);
                    }
                    else
                    {
                        pnNewHead.Y += SEGMENT_HEIGHT;
                        dirHelpDirection = Directions.Down;
                    }

                    break;
                }
                case (Directions.Up):
                {
                    if (this.WillCollide(new Point(pnNewHead.X, pnNewHead.Y - SEGMENT_HEIGHT)))
                    {
                        this.ManageSnake(Directions.NoDirection, dirSecondSnakeDirection);
                    }
                    else
                    {
                        pnNewHead.Y -= SEGMENT_HEIGHT;
                        dirHelpDirection = Directions.Up;
                    }

                    break;
                }
                case (Directions.NoDirection):
                {
                    switch (dirSecondSnakeDirection)
                    {
                        case (Directions.Right):
                        {
                            if (this.WillCollide(new Point(pnNewHead.X + SEGMENT_WIDTH, pnNewHead.Y)))
                            {
                                this.ManageSnake(this.dirHelpDirection, dirSecondSnakeDirection);
                            }
                            else
                            {
                                pnNewHead.X += SEGMENT_WIDTH;
                            }

                            break;
                        }
                        case (Directions.Left):
                        {
                            if (this.WillCollide(new Point(pnNewHead.X - SEGMENT_WIDTH, pnNewHead.Y)))
                            {
                                this.ManageSnake(this.dirHelpDirection, dirSecondSnakeDirection);
                            }
                            else
                            {
                                pnNewHead.X -= SEGMENT_WIDTH;
                            }

                            break;
                        }
                    }

                    break;
                }
            }

            this.gmGame.AddSegment(pnNewHead);

            if (this.gmGame.SnakeHead.Equals(this.pCurrFood))
            {
                this.lblScoreNum.Text = (int.Parse(this.lblScoreNum.Text) + 1).ToString();
                this.PlaceNewFood();
            }
            else
            {
                this.PaintSegment(this.panel1.CreateGraphics(),
                                  (Point)this.gmGame.SnakeQueue.Peek(),
                                  DEFAULT_COLOUR);
                this.gmGame.RemoveSegment();
            }

            this.PaintSegment(this.panel1.CreateGraphics(),
                              this.gmGame.SnakeHead,
                              Color.Green);
            Thread.Sleep(5);
            Application.DoEvents();
            this.SelfSolve();
        }

        #endregion

        #region Events

        private void GameForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.PlaceNewFood();
                this.SelfSolve();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
            else if (e.KeyCode == Keys.Space)
            {
                MessageBox.Show("Frozen, press OK to continue");
            }
        }

        private void GameForm_ClientSizeChanged(object sender, EventArgs e)
        {
            this.PaintSegment(this.panel1.CreateGraphics(), new Point(210, 250), Color.Green);
            this.PaintSegment(this.panel1.CreateGraphics(), new Point(220, 250), Color.Green);
            this.PaintSegment(this.panel1.CreateGraphics(), new Point(230, 250), Color.Green);
            this.PaintSegment(this.panel1.CreateGraphics(), new Point(240, 250), Color.Green);
            this.PaintSegment(this.panel1.CreateGraphics(), new Point(250, 250), Color.Green);
        }

        #endregion
    }
}

Я знаю, что кода много, но, поскольку я не уверен, в чем причина проблемы, мне пришлось скопировать все это.

Я был бы очень признателен, если бы вы указали мне правильное направление.

Заранее спасибо:)

P.S. Я знаю, что алгоритм имеет недостатки, я вернусь к этому позже. Сейчас моя главная задача - решить проблему с аварией.

Ответы [ 2 ]

6 голосов
/ 20 октября 2011

Это потому, что вы продолжаете идти рекурсивно в вашем методе. e.g.:

      Thread.Sleep(5); 
      Application.DoEvents(); 
      this.SelfSolve(); 

Ваш метод в основном никогда не закончится.

Вы должны использовать таймер, и в этом таймере вызвать SelfSolve (). Это должно решить вашу проблему. При использовании таймера вам не нужно вызывать DoEvents самостоятельно, поскольку таймер winforms в любом случае отправляет вызов вашему методу в виде сообщения, а цикл обработки сообщений обрабатывает вызов и другие сообщения.

0 голосов
/ 20 октября 2011

SelfSolve не должен вызывать ManageSnake напрямую, а скорее запланировать его запуск на более поздний момент.В противном случае вы получите бесконечную рекурсию SelfSolve -> ManageSnake -> SelfSolve и т. Д.

...