开发者

C# Battleships Class/Structure

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.


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");


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


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜