1

问候,我是编程新手,目前正在开发游戏战舰的克隆。我需要建立一个由 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; }
}

GridUnit 类保存表格单元格信息:

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; }
}

finally 类 Shipunit 包含上述两个类,并充当单个单元格状态信息的包装器:

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}
};

我意识到最后一点代码不起作用。它只是为了提出这个想法。

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

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

谢谢你。

4

5 回答 5

3
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");
于 2010-02-10T14:38:52.607 回答
0

我想我会定义一个拥有的 Grid 类,它包含所有的 GridUnits。然后这个 Grid 也将包含一个列表。一艘船只有大小、方向、BowCell 等属性。当将一艘船添加到网格中时,网格可以相应地设置单元的状态。

这样,您可以在船级上使用有用的方法,例如 IsSunk()、OccupiesUnit() 等...

于 2010-02-10T14:39:04.017 回答
0

你为什么不创造这样的东西

船级
{
    公共 ShipUnits[] _SUParts;
    公共字符串_strType;

    公共船舶(字符串 styType,int NbPart)
    {
         _SUParts = new ShipUnit[长度];
         _strType = strType;
    }

}

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

我假设您的类型也意味着船的名称(驱逐舰等)

于 2010-02-10T14:39:16.543 回答
0

有多少种船。在运行时是固定的还是可变的?

如果它是固定的并且不是太多,您可能应该只为每个使用单独的数组。如果它们是可变的并且每种类型只有一个数组,则可以使用通用字典(enumShipUnitType、ShipUnit[])。

然后,您可以像这样从字典中获取 KeyValuePair 来迭代字典。

 For Each kvp As KeyValuePair(Of enumShipUnitType, ShipUnit[]) In m_dictShipUnits
      For each oShipUnit as Shipunit in kvp.Value
         'Do whatever
      Next
 Next
于 2010-02-10T14:58:02.417 回答
0
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 继承并为不同的船创建子类,如战舰、巡洋舰等。添加一个“IsHit(Point shot)”方法来比较大小、方向、位置和射击位置(矩形类有很多很好的函数)。

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

创建两个网格(每个玩家一个),添加一个为每艘船调用 IsHit 的拍摄方法,然后将镜头添加到镜头中。如果船舶的每个点都被击中(在射击中),则在每次移动后,如果所有点都被击中,则将船舶设置为沉没。

于 2010-02-10T15:01:59.180 回答