Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

Below is my code for a Battleship game. I keep getting the following error:

Process terminated due to 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.");
    }
}

}

share|improve this question
3  
+1 for StackOverflowException – Mehrdad Jun 6 '11 at 1:48
    
first question: why does the Player class have a copy of the board? (and fixed size as well). Create a separate Board class... – Mitch Wheat Jun 6 '11 at 1:52
    
WOW thanks Mitch lol fixed it – Jay Berd Jun 6 '11 at 1:56
1  
GetXYZ() and SetXYZ() methods should be removed and properties should be used instead. Please use proper local variables and private fields naming casing (ie., "Row" should be "row"). Have a look at JetBrain's Resharper while you're still new at coding. It should help you a lot with naming conventions (jetbrains.com/resharper). – matthew.perron Jun 6 '11 at 2:01

4 Answers 4

Your BattleshipBoard object creatse a Player object during construction, and your Player object creates a BattleshipBoard during construction. This iterates back and forth until you overflow the stack.

The call to:

BattleshipBoard b = new BattleshipBoard();

will never return and cause the overflow.

share|improve this answer

Stack overflow is normally caused by infinite recusion; i.e. function A calls B and then B and A and A has no termination condition.

In your case, its the initialises of your two classes BattleshipBoard and Player.

Your Player creates a new BattleshipBoard and the BattleshipBoard creates a new Player!

Remove the line Player p = new Player(); from the BattleshipBoard and all should be good.

share|improve this answer

When you try to create your initial BattleshipBoard, it results in a infinite loop alternately creating your two types.

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.

share|improve this answer

Your BattleShipBoard has a Player which has a BattleShipBoard which has a Player which has a BattleShipBoard which has a [...go on forever...]

Hence, you have a stack overflow.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.