In: Computer Science
C# Question:
OO Design
Our battleships game needs several different kinds of ship – we need a type to store the ships and information about it.
Create a base class called Ship
Create child classes for each of the ship types in the game (https://en.wikipedia.org/wiki/Battleship_(game)#Description) that inherit from Ship
Ships should have the following properties
A private array of positions
A Position is composed of an X and Y coordinate – you should create a struct to encapsulate this
A read only length
The constructor for each inherited type should set this to the correct length for the ship type
A read only color to be drawn in
The constructor for each inherited type should set this to a different ConsoleColor for drawing
A flag called Sunk
This should default to false for all ships
A property called IsBattleship
This property should be overridden by each child type. Only the BattleShip should return true.
A method called Reset
This method should reset the members to their empty defaults
A method called Place(Position start, Direction direction)
Direction should be an enumeration of either Horizontal or Vertical
This will complete the Position array with the set of
coordinates that the ship is covering
e.g Place(new Position(1, 1), Direction.Horizontal) on a patrol
boat will fill the array with the points (1, 1) and (2, 1)
Notes:
This is a separate program from previous weeks. You do not need to consider or implement anything to do with the grid or guessing
You should choose the correct types and access modifiers for each type.
Create a test program that can run code such as the following:
AircraftCarrier ac = new AircraftCarrier();
Console.WriteLine(ac.IsBattleShip);
ac.Place(new Position(1, 1), Direction.Horizontal);
ac.Reset();
You should write additional code to test all of the methods and
ship types.
Given that the positions array is private how can you test that the values are correct?
Create classes and structs to define the ship types and
positions in a game of battleships. Write a test program to
demonstrate that the you have followed everything above.
Position.cs
namespace BattleShip
{
public struct Position
{
public int X;
public int Y;
}
}
Direction.cs
namespace BattleShip
{
//Direction should be an enumeration of either Horizontal or Vertical
public enum Direction
{
Horizontal, Vertical
}
}
Submarine.cs
using System;
namespace BattleShip
{
public class Submarine : Ship
{
public bool flag = true;
public Submarine() : base(3, ConsoleColor.Green)
{
}
}
}
PatrolBoat.cs
using System;
namespace BattleShip
{
public class PatrolBoat : Ship
{
public PatrolBoat() : base(3, ConsoleColor.White)
{
}
}
}
Destroyer.cs
using System;
namespace BattleShip
{
public class Destroyer : Ship
{
public Destroyer() : base(2, ConsoleColor.Magenta)
{
}
}
}
BattleShip.cs
using System;
namespace BattleShip
{
public class BattleShip : Ship
{
public override bool IsBattleShip { get { return true; } }
public BattleShip() : base(4, ConsoleColor.Yellow)
{
}
}
}
AircraftCarrier.cs
using System;
namespace BattleShip
{
public class AircraftCarrier : Ship
{
public AircraftCarrier() : base(5, ConsoleColor.Red)
{
}
}
}
Ship.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BattleShip
{
public class Ship
{
private Position[] positions;
private int length;
private ConsoleColor color;
public bool flag;
public virtual bool IsBattleShip { get; private set; }
public Ship()
{
}
public Position[] getpositions
{
get
{
return this.positions;
}
}
public Ship(int length, ConsoleColor color)
{
this.length = length;
this.color = color;
flag = false;
this.positions = new Position[length];
}
public int GetLength()
{
return this.length;
}
public ConsoleColor GetConsoleColor()
{
return this.color;
}
public virtual void Reset()
{
this.positions = new Position[length];
this.flag = false;
}
public void Place(Position start, Direction direction)
{
for (int i = 0; i < this.length; i++)
{
if (direction == Direction.Horizontal)
{
Position newPos = new Position();
newPos.X = start.Y;
newPos.Y = start.X + i;
this.positions[i] = newPos;
}
else
{
Position newPos = new Position();
newPos.X = start.Y + i;
newPos.Y = start.X;
this.positions[i] = newPos;
}
}
}
}
}
Driver.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BattleShip
{
public class Driver
{
static void Main(string[] args)
{
Console.WriteLine("Exercise the ship class and 5 descendent ships");
Position newPos = new Position();
newPos.X = 0; newPos.Y = 0;
AircraftCarrier ac = new AircraftCarrier();
Console.ForegroundColor = ac.GetConsoleColor();
ac.Place(newPos, Direction.Horizontal);
Console.WriteLine("\nkind: " + ac.GetType().Name);
Console.Write("Coordinates: ");
for (int k = 0; k < ac.GetLength(); k++)
{
Console.Write(ac.getpositions[k].X + "," + ac.getpositions[k].Y + " ");
}
Console.WriteLine("\nLength: " + ac.GetLength());
Console.WriteLine("Sunk: " + ac.flag);
Console.WriteLine("isBattleship: " + ac.IsBattleShip);
ac.Reset();
newPos.X = 3; newPos.Y = 1;
BattleShip bs = new BattleShip();
Console.ForegroundColor = bs.GetConsoleColor();
bs.Place(newPos, Direction.Vertical);
Console.WriteLine("\nkind: " + bs.GetType().Name);
Console.Write("Coordinates: ");
for (int k = 0; k < bs.GetLength(); k++)
{
Console.Write(bs.getpositions[k].X + "," + bs.getpositions[k].Y + " ");
}
Console.WriteLine("\nLength: " + bs.GetLength());
Console.WriteLine("Sunk: " + bs.flag);
Console.WriteLine("isBattleship: " + bs.IsBattleShip);
ac.Reset();
newPos.X = 7; newPos.Y = 6;
Destroyer ds = new Destroyer();
Console.ForegroundColor = ds.GetConsoleColor();
ds.Place(newPos, Direction.Vertical);
Console.WriteLine("\nkind: " + ds.GetType().Name);
Console.Write("Coordinates: ");
for (int k = 0; k < ds.GetLength(); k++)
{
Console.Write(ds.getpositions[k].X + "," + ds.getpositions[k].Y + " ");
}
Console.WriteLine("\nLength: " + ds.GetLength());
Console.WriteLine("Sunk: " + ds.flag);
Console.WriteLine("isBattleship: " + ds.IsBattleShip);
ac.Reset();
newPos.X = 4; newPos.Y = 4;
PatrolBoat pb = new PatrolBoat();
Console.ForegroundColor = pb.GetConsoleColor();
pb.Place(newPos, Direction.Horizontal);
Console.WriteLine("\nkind: " + pb.GetType().Name);
Console.Write("Coordinates: ");
for (int k = 0; k < pb.GetLength(); k++)
{
Console.Write(pb.getpositions[k].X + "," + pb.getpositions[k].Y + " ");
}
Console.WriteLine("\nLength: " + pb.GetLength());
Console.WriteLine("Sunk: " + pb.flag);
Console.WriteLine("isBattleship: " + pb.IsBattleShip);
ac.Reset();
newPos.X = 0; newPos.Y = 7;
Submarine sm = new Submarine();
Console.ForegroundColor = sm.GetConsoleColor();
sm.Place(newPos, Direction.Vertical);
Console.WriteLine("\nkind: " + sm.GetType().Name);
Console.Write("Coordinates: ");
for (int k = 0; k < sm.GetLength(); k++)
{
Console.Write(sm.getpositions[k].X + "," + sm.getpositions[k].Y + " ");
}
Console.WriteLine("\nLength: " + sm.GetLength());
Console.WriteLine("Sunk: " + sm.flag);
Console.WriteLine("isBattleship: " + sm.IsBattleShip);
ac.Reset();
Console.ReadKey();
}
}
}
Sample output: