调试C# - 栈溢出异常?

4
以下是我的战舰游戏代码。我一直收到以下错误信息:
“由于 StackOverflowException 被终止进程。”
它一直指向:
char[,] Grid = new char[10, 10];

这个问题怎么解决?
using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BattleShip_Jamshid_Berdimuratov
{
class BattleshipBoard
{
    Player p = new Player();
    public void Randomize()
    {
        p.SetGrid(1, 2);
        p.SetGrid(2, 2);
        p.SetGrid(3, 2);
    }

    public void DisplayBoard(char[,] Board)
    {
        int Row;
        int Column;

        Console.WriteLine(" ¦ 0 1 2 3 4 5 6 7 8 9");
        Console.WriteLine("--+--------------------");
        for (Row = 0; Row <= 9; Row++)
        {
            Console.Write((Row).ToString() + " ¦ ");
            for (Column = 0; Column <= 9; Column++)
            {
                Console.Write(Board[Column, Row] + " ");
            }
            Console.WriteLine();
        }

        Console.WriteLine("\n");
    }
}

class Player
{
    char[,] Grid = new char[10, 10];
    public int HitCount = 0;
    public int MissCount = 0;
    int x = 0;
    int y = 0;
    BattleshipBoard b = new BattleshipBoard();

    public int getHitCount()
    {
        return HitCount;
    }
    public int getMissCount()
    {
        return MissCount;
    }
    public void AskCoordinates()
    {
        Console.WriteLine("Enter X");
        string line = Console.ReadLine();
        int value;
        if (int.TryParse(line, out value))
        {
            x = value;
        }
        else
        {
            Console.WriteLine("Not an integer!");
        }

        Console.WriteLine("Enter Y");
        line = Console.ReadLine();
        if (int.TryParse(line, out value))
        {
            y = value;
        }
        else
        {
            Console.WriteLine("Not an integer!");
        }

        try
        {
            if (Grid[x, y].Equals('S'))
            {
                Grid[x, y] = 'H';
                Console.Clear();
                Console.WriteLine("Hit!");
                HitCount += 1;
            }
            else
            {
                Grid[x, y] = 'M';
                Console.Clear();
                Console.WriteLine("Miss!");
            }
        }
        catch
        {
            Console.Clear();
            Console.WriteLine("Error: Please enter numbers between 0 and 9. (Inclusive)");
        }
    }
    public char[,] GetGrid()
    {
        return Grid;
    }
    public void SetGrid(int x, int y)
    {
        Grid[x, y] = 'S';
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.Title = "BerdShip!";
        Console.WriteLine("Welcome to Berdship!\r\n\r\n");
        Console.WriteLine("What is your name?");
        string name = System.Console.ReadLine();
        Console.WriteLine();
        BattleshipBoard b = new BattleshipBoard();
        Player p = new Player();
        b.Randomize();
        while (p.getHitCount() < 12)
        {
            b.DisplayBoard(p.GetGrid());
            p.AskCoordinates();
        }
        Console.WriteLine("Congratulations, " + name + "! You Win!");
        Console.WriteLine("Thanks for playing BerdShip. Press enter to quit.");
    }
}

}


3
+1 表示对于 StackOverflowException 的赞同。 - user541686
为什么Player类有一个棋盘的副本?(而且还是固定大小的)。创建一个单独的Board类... - Mitch Wheat
哇,谢谢 Mitch,哈哈,问题解决了。 - Jay Berd
1
应当移除 GetXYZ() 和 SetXYZ() 方法,而应使用属性。请使用正确的本地变量和私有字段命名规范(例如“Row”应该是“row”)。在您刚开始编码时可以查看JetBrains的Resharper。它应该对命名约定有很大帮助(http://www.jetbrains.com/resharper/)。 - Matthew Perron
4个回答

10

你的BattleshipBoard对象在构造期间创建了一个Player对象,而你的Player对象在构造期间创建了一个BattleshipBoard。这样反复迭代,直到堆栈溢出。

调用:

BattleshipBoard b = new BattleshipBoard();

该函数永远不会返回并导致溢出。


1

堆栈溢出通常是由无限递归引起的;即函数A调用B,然后B和A,而A没有终止条件。

在您的情况下,是您的两个类BattleshipBoardPlayer的初始化。

您的Player创建了一个新的BattleshipBoard,而BattleshipBoard又创建了一个新的Player

BattleshipBoard中删除Player p = new Player();这一行,一切都应该没问题了。


1

当您尝试创建初始的BattleshipBoard时,它会导致无限循环交替创建两种类型。

如果您希望这两个类相互引用,则可以让您的Player类在其构造函数中使用BattleshipBoard。


0

你的 BattleShipBoard 有一个 Player,它又有一个 BattleShipBoard,再有一个 Player,它又有一个 BattleShipBoard,如此循环 [...永远继续下去...]

因此,你遇到了堆栈溢出错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接