C# 战舰类/结构

发布于 2024-08-20 13:29:26 字数 1511 浏览 6 评论 0原文

您好,我是编程新手,目前正在开发游戏战舰的克隆。我需要建立一支由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小红帽 2024-08-27 13:29:27
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");
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");
南渊 2024-08-27 13:29:27

我想我会定义一个拥有 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...

软糯酥胸 2024-08-27 13:29:27

你为什么不创建这样的东西,

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

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

}

话虽如此,我不会将所有成员公开。我使用 Getter/Setter 来更改我按类型假设的值,

您也指的是船舶的名称(驱逐舰等)

Why don't you create something like this

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

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

}

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.)

萌辣 2024-08-27 13:29:27

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

如果它是固定的并且不是太多,您可能应该为每个使用单独的数组。如果它们是可变的并且每种类型只有一个数组,则可以使用通用字典(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

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.

 For Each kvp As KeyValuePair(Of enumShipUnitType, ShipUnit[]) In m_dictShipUnits
      For each oShipUnit as Shipunit in kvp.Value
         'Do whatever
      Next
 Next
终陌 2024-08-27 13:29:27
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 的射击方法,然后将镜头添加到镜头中。每次移动后,如果船的每个点都被击中(在射击中),如果所有点都被击中,则将船设置为沉没。

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; }
  [...]
}

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).

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文