C# 战舰类/结构
您好,我是编程新手,目前正在开发游戏战舰的克隆。我需要建立一支由 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; }
}
最后,类 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}
};
我意识到最后一段代码不起作用。它只是为了表达这个想法。
但问题是我需要一个字段来说明每行锯齿状数组代表什么类型的船舶,并且我认为在每个单元格信息中说明此信息并不实际。
所以我想请您提供一些实施此问题的想法。
谢谢。
greetings, im am new to programming and at the moment developing a clone of the game battleships. i need to implement a fleet of 5 ships. this is what i have done so far:
class Cell holds the status of a table 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; }
}
class GridUnit holds table cell info:
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 class Shipunit contains both the above classes and acts as a wrapper for state info of an individual cell:
public class ShipUnit
{
public GridUnit gridUnit = new GridUnit();
public Cell cell = new Cell(Cell.cellState.SHIPUNIT);
}
at the moment i am thinking about implementing the fleet info in a Jagged Array like this:
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}
};
i realize the last bit of code does not work. it is only for presenting the idea.
but the problem being i need a field that states what type of ship each line of jagged array represent and i dont think it is practical to state this info in every cell information.
so i would like some ideas of implementation of this issue from you.
thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想我会定义一个拥有 Grid 类,它包含所有 GridUnit。那么这个 Grid 也会保存一个 List。一艘船只有大小、方向、BowCell 等属性。当向网格添加船舶时,网格可以相应地设置单位的状态。
这样,您就可以在船舶级别上使用有用的方法,例如 IsSunk()、OccupiesUnit() 等...
I think I would define an owning Grid class, the holds all of the GridUnits. Then this Grid would also hold a List. A Ship would just have properties like size, orientation, BowCell. When adding a ship to the grid, the Grid could set the status of the units accordingly.
This way, you can have usefull methods on the ship level like IsSunk(), OccupiesUnit(), etc...
你为什么不创建这样的东西,
话虽如此,我不会将所有成员公开。我使用 Getter/Setter 来更改我按类型假设的值,
您也指的是船舶的名称(驱逐舰等)
Why don't you create something like this
that being said, I would not put all members public. I'de use Getter/Setter to change the values
I assumed by type you also mean the name of the ship (destroyer,etc.)
船舶有多少种类型。它在运行时是固定的还是可变的?
如果它是固定的并且不是太多,您可能应该为每个使用单独的数组。如果它们是可变的并且每种类型只有一个数组,则可以使用通用字典(enumShipUnitType、ShipUnit[])。
然后,您可以通过从中获取 KeyValuePair 来迭代字典,如下所示。
How many types of ships are there. Is that fixed or variable at runtime?
If it is fixed and not too many, you should probably just use separate arrays for each. If they are variable AND you have only one array for each type, you could use a generic dictionary (of enumShipUnitType, ShipUnit[]).
You can then iterate over the dictionary by getting the KeyValuePair from it like so.
继承 Ship 并为不同的舰船创建子类,如战舰、巡洋舰等。添加一个“IsHit(Point shot)”方法来比较大小、方向、位置和射击位置(矩形类有许多不错的函数)。
创建两个网格(每个玩家一个),添加一个为每艘船调用 IsHit 的射击方法,然后将镜头添加到镜头中。每次移动后,如果船的每个点都被击中(在射击中),如果所有点都被击中,则将船设置为沉没。
Inherit from Ship and create sub classes like Battleship, Cruiser, etc for the different ships. Add a "IsHit(Point shot)" method that compares Size, Orientation, Position and the shot position (the Rectangle class has many nice functions for that).
Create two grids (one for each player), add a shoot method that calls IsHit for every Ship and then adds the shot to shots. After each move if every point of a ship is hit (is in shots) and set the ship to Sunk if all are hit.