我的代码只打印:
welcome to battleship
但船舶坐标或船舶已创建 甚至结束“按任意键退出” 没有打印。
我怀疑是构造函数,看起来没有被调用,但为什么我的程序在计算而不是崩溃?
我正在使用Xamarin Studio在Mac上运行代码。
代码:
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;
}
}
答案 0 :(得分:2)
更改
public Coordinate invalid = new Coordinate(-1, -1);
要
public static Coordinate invalid = new Coordinate(-1, -1);
public Coordinate invalid = new Coordinate(-1, -1);
导致堆栈溢出。这是因为新的Coordinate在Coordinate类中初始化,所以每次必须创建新的坐标。使该字段静态,以便创建一次,并可用于所有实例。
如果引用不应该更改,您还可以添加readyonly
关键字。
public readonly static Coordinate invalid = new Coordinate(-1, -1);