[英]Debugging C# - stack overflow exception?
Below is my code for a Battleship game.下面是我的战舰游戏代码。 I keep getting the following error:我不断收到以下错误:
Process terminated due to StackOverflowException.进程因 StackOverflowException 而终止。 . .
It keeps pointing to它一直指向
char[,] Grid = new char[10, 10];
How can this be fixed?如何解决这个问题?
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.");
}
}
} }
Your BattleshipBoard object creatse a Player object during construction, and your Player object creates a BattleshipBoard during construction.您的 BattleshipBoard object 在构建过程中创建一个玩家 object,您的玩家 object 在构建过程中创建一个 BattleshipBoard。 This iterates back and forth until you overflow the stack.这会来回迭代,直到您溢出堆栈。
The call to:呼吁:
BattleshipBoard b = new BattleshipBoard();
will never return and cause the overflow.永远不会返回并导致溢出。
Stack overflow is normally caused by infinite recusion;栈溢出一般是无限回溯造成的; ie function A calls B and then B and A and A has no termination condition.即 function A 调用 B 然后 B 和 A 和 A 没有终止条件。
In your case, its the initialises of your two classes BattleshipBoard
and Player
.在您的情况下,它是您的两个类BattleshipBoard
和Player
的初始化。
Your Player
creates a new BattleshipBoard
and the BattleshipBoard
creates a new Player
!你的Player
创建了一个新的BattleshipBoard
,而BattleshipBoard
又创建了一个新的Player
!
Remove the line Player p = new Player();
删除行Player p = new Player();
from the BattleshipBoard
and all should be good.从BattleshipBoard
,一切都应该很好。
When you try to create your initial BattleshipBoard, it results in a infinite loop alternately creating your two types.当您尝试创建初始 BattleshipBoard 时,会导致无限循环交替创建您的两种类型。
If you want the two classes to have a reference to the other corresponding class, then you can have your Player class take the BattleshipBoard in its constructor.如果您希望这两个类引用另一个对应的 class,那么您可以让您的玩家 class 在其构造函数中使用 BattleshipBoard。
Your BattleShipBoard
has a Player
which has a BattleShipBoard
which has a Player
which has a BattleShipBoard
which has a [ ...go on forever... ]你的BattleShipBoard
有一个Player
有一个BattleShipBoard
有一个Player
有一个BattleShipBoard
有 [ ...go on forever... ]
Hence, you have a stack overflow.因此,您有堆栈溢出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.