Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# Need some help getting the following working: Create a new class called Ships. - Ships should have the following properties: - A way to

C# Need some help getting the following working:

Create a new class called Ships.

- Ships should have the following properties:

- A way to store a collection of the Ship class from last week.

- Does the Ships class represent the IS-A (inheritance) or the HAS-A (composition) relationship? Design your class accordingly. Use suitable generic collection classes.

void Add(Ship)

- This is the method that allows you to add a ship.

- The add method should validate that no ships are overlapping.

- If there is a collision this method should throw a suitable exception.

void Clear()

- This is the method that allows you to reset the collection.

bool SunkMyBattleship {get; private set}

- This readonly property returns true if the battleship has been sunk.

- The private set part is optional depending on how you implement it. But the property should be readonly to users of the class.

bool Attack(int x, int y)

- This is the method that attacks the collection of ships and marks any positions as hit.

- This should also mark the ship as Sunk if all positions are hit.

- The method should return an indication of a hit (true) or a miss (false).

- Your attack method should validate that x and y are positive integers and throw a suitable exception if they are not.

You should choose the correct types and access modifiers for each type.

You may add as much code and data structures as you like to fulfill all the requirements.

Create a test program that creates a Ships collection and populates it with some ships. You may hard-code positions although your instructor will test your code with different ones so make sure to test your code thoroughly.

The test code should ask the user for X, Y positions and attack the ship collection until the battleship is hit.

I have the ship class to create the ships and all of the methods working, so just the stuff previously mentioned.

The ship class I that needs to be contained in the above code is:

public abstract class Ship

{

private Positions[] position; //takes in x, y coordinates through a struct public readonly int length; //length of boat, e.g carrier = 5 public readonly ConsoleColor color; //color of boat public bool Sunk = false; //need to write a way to determine if the length of the boat has been hit

public Ship(int length, ConsoleColor color) { this.length = length; this.color = color; this.Sunk = false;

this.position = new Positions[length];

}

}

Hopefully this is enough to complete the tasks.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions