Mike Varelas Mike Varelas - 1 month ago 6
C# Question

c# Constructor not called

My code just prints:

welcome to battleship


but the ship coordinates or ship created
and even the end "Press any key to exit"
is not printed.

My suspicion goes to the Constructor, looks like its not called but why my program is counting and not crashing?

Im running the code on a Mac with Xamarin Studio.

Code:

using System;
using System.Collections.Generic;

namespace battleship
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("welcome to battleship");

EnumShipClassification.ShipClassification cruiser = EnumShipClassification.ShipClassification.theCruiser;
Coordinate startCoordinate = new Coordinate(5, 5);
EnumDirection.Direction direction = EnumDirection.Direction.west;

Ship ship = new Ship(cruiser, startCoordinate, direction);
Console.WriteLine("ship created"); // @debug
Console.WriteLine("all coords: " + ship.coordinates);

//@end
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}

public class Ship
{
public int size; // values from wikipedia
public int amount; // values from wikipedia
public List<Coordinate> coordinates = new List<Coordinate>();

public EnumShipClassification.ShipClassification classification;
public Ship(EnumShipClassification.ShipClassification classification, Coordinate startCoordinate, EnumDirection.Direction toDirection)
{
Console.WriteLine("Ship Constructor called "); // @debug
this.classification = classification;
Console.WriteLine("classification added"); // @debug

switch (classification)
{
case EnumShipClassification.ShipClassification.theBattleship: size = 5; amount = 1; break;
case EnumShipClassification.ShipClassification.theCruiser: size = 4; amount = 2; break;
case EnumShipClassification.ShipClassification.theDestroyer: size = 3; amount = 3; break;
case EnumShipClassification.ShipClassification.theSubmarine: size = 2; amount = 4; break;
default: break;
}

Console.WriteLine("switch ended with size {0} and amout {1}", size, amount);

for (int i = 0; i < size; i++)
{
coordinates.Add(startCoordinate.advancedBy(i, toDirection));
Console.WriteLine("i is: " + i); // @debug
}
}
}

public class EnumDirection
{
public enum Direction
{
north, south, east, west // + all (Direction[] array with content [.n, .s, .e, .w]
}

public Direction[] all = { Direction.north, Direction.south, Direction.east, Direction.west };
}

public class EnumShipClassification
{
public enum ShipClassification // numbers: BoatSize
{
theBattleship = 5, theCruiser = 4, theDestroyer = 3, theSubmarine = 2
}
}


public class Coordinate
{
public int row;
public int column;

private int fieldSize = 10;

public void init()
{
row = 0;
column = 0;
}

public Coordinate invalid = new Coordinate(-1, -1);

public Coordinate(int row, int column)
{
this.row = row;
this.column = column;
}

public Coordinate getRandomCoordinate(int betweenX, int Y)
{
Random r = new Random(fieldSize);
return new Coordinate(r.Next(), r.Next());
}

public Coordinate neighbor(EnumDirection.Direction inDirection)
{
return advancedBy(1, inDirection: inDirection);
}

public Coordinate advancedBy(int displacement, EnumDirection.Direction inDirection)
{
switch (inDirection)
{
case EnumDirection.Direction.north: return new Coordinate(column: column, row: row + displacement);
case EnumDirection.Direction.south: return new Coordinate(column: column, row: row - displacement);
case EnumDirection.Direction.east: return new Coordinate(column: column + displacement, row: row);
case EnumDirection.Direction.west: return new Coordinate(column: column - displacement, row: row);

default: break; // never happens
}
return null; // never happens
}

public string description
{
get
{
return "column: " + column + ", row: " + row;
}
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;

try
{
Coordinate _obj = (Coordinate)obj;
return _obj.row == this.row && _obj.column == this.column;
}
catch
{
return false;
}
}

public override int GetHashCode()
{
return 1; // TODO: return a real value
}

public static bool operator ==(Coordinate lhs, Coordinate rhs)
{
return lhs.row == rhs.row && lhs.column == rhs.column;
}

public static bool operator !=(Coordinate lhs, Coordinate rhs)
{
return lhs.row != rhs.row && lhs.column != rhs.column;
}
}

Answer

Change

public Coordinate invalid = new Coordinate(-1, -1);

To

public static Coordinate invalid = new Coordinate(-1, -1);

public Coordinate invalid = new Coordinate(-1, -1); causes stack overflow. that's because new Coordinate initialized inside Coordinate class so every time new coordinate must be created. Make the field static so that is created once and can be used for all instances.

You can also add readyonly keyword if the reference is not supposed to change.

public readonly static Coordinate invalid = new Coordinate(-1, -1);