大家好,我是编程新手,目前正在开发一个战舰游戏的克隆。我需要实现一个由5艘船组成的舰队。这就是我到目前为止所做的:
类单元格保存表单元格的状态:
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; }
}最后,类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}
};我意识到最后一点代码不起作用。这只是为了展示想法。
但问题是,我需要一个字段来说明锯齿数组的每一行代表什么类型的船,我认为在每个单元格信息中说明这一信息是不现实的。
所以我想从你那里得到一些实现这个问题的想法。
谢谢。
发布于 2010-02-10 22:38:53
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-10 22:39:04
我想我会定义一个拥有网格的类,它包含了所有的GridUnits。那么这个网格也会保存一个列表。一艘船只有一些属性,比如大小,方向,BowCell。当将一艘船添加到网格时,网格可以相应地设置单元的状态。
这样,您就可以在ship级别上使用IsSunk()、OccupiesUnit()等方法。
发布于 2010-02-10 22:58:02
有多少种类型的船?它在运行时是固定的还是可变的?
如果它是固定的,并且不是太多,那么您可能应该对每个数组使用单独的数组。如果它们是可变的,并且每种类型只有一个数组,那么可以使用通用字典( 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
Nexthttps://stackoverflow.com/questions/2237482
复制相似问题