2011-06-06 103 views
4

下面是我的戰艦遊戲代碼。我不斷收到以下錯誤:調試C# - 堆棧溢出異常?

Process terminated due to 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 for StackOverflowException – Mehrdad 2011-06-06 01:48:55

+0

第一個問題:爲什麼Player類有一個板的副本? (以及固定大小)。創建一個單獨的Board類... – 2011-06-06 01:52:53

+0

WOW感謝Mitch哈哈固定它 – 2011-06-06 01:56:17

回答

10

你BattleshipBoard對象施工期間creatse一個Player對象,並且您的播放器對象創建施工期間BattleshipBoard。這反覆迭代,直到你溢出堆棧。

的號召:

BattleshipBoard b = new BattleshipBoard(); 

永遠不會返回,並導致溢出。

1

堆棧溢出通常是由無限回憶造成的;即函數A調用B,然後B和A和A沒有終止條件。

在你的情況下,它的兩個類BattleshipBoardPlayer的初始值。

你的Player創建一個新的BattleshipBoardBattleshipBoard創建一個新的Player

BattleshipBoard中刪除行Player p = new Player();並且全部都應該是好的。

0

BattleShipBoardPlayer具有BattleShipBoard具有Player具有BattleShipBoard具有[ ...永遠持續下去...]

因此,你有一個堆棧溢出。

1

當您嘗試創建初始BattleshipBoard時,會導致無限循環交替創建兩種類型。

如果你想讓這兩個類有對其他相應類的引用,那麼你可以讓你的Player類在其構造函數中使用BattleshipBoard。