English 中文(简体)
C# 战舰类/结构体
原标题:C# Battleships Class/Structure

问候,我是编程的新手,目前正在开发战舰游戏的克隆版。我需要实现一个由5艘船组成的舰队。到目前为止,这是我所做的:

单元格类 (Cell) 存储表格单元格的状态。

public class Cell
{
    // class for holding cell status information
    public enum cellState
    {
        WATER,
        SCAN,
        SHIPUNIT,
        SHOT,
        HIT
    }

    public Cell()
    {
        currentCell = cellState.WATER;
    }

    public Cell(cellState CellState)
    {
        currentCell = CellState;
    }

    public cellState currentCell { get; set; }
}

网格单元格保存表格单元格信息:

public class GridUnit
{
    public GridUnit()
    {
        Column = 0;
        Row = 0;
    }

    public GridUnit(int column, int row)
    {
        Column = column;
        Row = row;
    }

    public int Column { get; set; }

    public int Row { get; set; }
}

最后,“船运”既包含上述课程,也作为国家个人囚室的包裹:

public class ShipUnit
{
    public GridUnit gridUnit = new GridUnit();

    public Cell cell = new Cell(Cell.cellState.SHIPUNIT);
}

目前我正在考虑将车队信息实现为这样的锯齿数组:

ShipUnit[][] Fleet = new ShipUnit[][]
{
new ShipUnit[] {ShipUnit,ShipUnit,ShipUnit,ShipUnit,ShipUnit},
new ShipUnit[] {ShipUnit,ShipUnit,ShipUnit,ShipUnit},
new ShipUnit[] {ShipUnit,ShipUnit,ShipUnit}
new ShipUnit[] {ShipUnit,ShipUnit,ShipUnit}
new ShipUnit[] {ShipUnit,ShipUnit}
};

我意识到最后一段代码无法正常工作。它仅用于表达想法。

但问题在于,我需要一个字段来说明每一行锯齿形数组代表的船的类型,我认为在每个单元格信息中说明这些信息并不实际。

所以我想从你那里得到一些针对这个问题的实施思路。

谢谢。

最佳回答
class Ship
{
    ShipUnit[] shipUnits;
    string type;
    public Ship(int length, string type)
    {
        shipUnits = new ShipUnit[length];
        this.type = type;
    }
}

Ship[] fleet = new Ship[5];
fleet[0] = new Ship(5, "Carrier");
fleet[1] = new Ship(4, "Battleship");
fleet[2] = new Ship(3, "Submarine");
fleet[3] = new Ship(3, "Something else");
fleet[4] = new Ship(2, "Destroyer");
问题回答

我认为我可以定义一个包含所有GridUnits的拥有Grid类。然后,这个Grid也会持有一个列表。船只只需要具备大小、方向和船首单元格等属性。当将一艘船舶添加到网格上时,网格可以相应地设置单元格的状态。

这样,你可以在船只级别上使用有用的方法,如IsSunk()、OccupiesUnit()等。

为什么您不创造出像这样的东西呢?

class Ship
{
    public ShipUnits[] _SUParts;
    public String _strType;

    public Ship(String styType, int NbPart)
    {
         _SUParts = new ShipUnit[length];
         _strType = strType;
    }

}

话虽如此,我不会公开所有成员。我会使用Getter/Setter更改值。

我認為您所謂的“型號”也包括船名(驅逐艦等)。

有多少类型的船舶。 这在运行时间是否固定或变数?

如果它是固定的且数量不太多,你应该只使用各自的数组。如果它们是可变的且每种类型只有一个数组,你可以使用通用字典(enumShipUnitType,ShipUnit数组)。

你可以通过从字典中获取键值对来迭代它。

 For Each kvp As KeyValuePair(Of enumShipUnitType, ShipUnit[]) In m_dictShipUnits
      For each oShipUnit as Shipunit in kvp.Value
          Do whatever
      Next
 Next
class Ship {
  public Size Size { get; set; }
  public Orientation Orientation { get; set; }
  public Point Position { get; set; }
  public Boolean Sunk { get; set; }
  public String Name { get; set; }
  [...]
}

继承于 Ship,并为不同的船创建 Battleship、Cruiser 等子类。添加一个“IsHit(Point shot)”方法,比较大小、方向、位置和射击位置(Rectangle 类有许多不错的功能)。

class Grid {
  private Size size = new Size(10, 10);
  private List<Ship> ships = new List<Ship>();
  private List<Point> shots;
  [...]
}

创建两个网格(每个玩家一个),添加一个shoot方法,该方法调用每个船只的IsHit方法,然后将射击添加到shots中。在每次移动后,如果船只的每个点都被击中(在shots中),则将该船设置为Sunk。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签