Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Our Battleship game needs to store a set of ships. Create a new class called Ships. Ships should have the following properties: void Add(Ship) This

Our Battleship game needs to store a set of ships.

Create a new class called Ships.

Ships should have the following properties:

void Add(Ship)

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

The add method should validate that no ships are overlapping. How you do this is up to you to design. Feel free to discuss it in the forums but do not post code.

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.

This is some of the code I have , but it is not working properly.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Module_4_Battleship { public class Ships { public string ShipName { get; set; } public ShipOrientation Orientation { get; set; } public int ShipNum { get; set; } public int ExtentUnits { get; set; } public int Position_Row { get; set; } public int Position_Col { get; set; } static bool PlayerGameOver, GameOverBool; public override string ToString() { return "Ship Number: " + ShipNum + " Ship Name: " + ShipName; } public override bool Equals(object obj) { if (obj == null) return false; Ship objAsShip = obj as Ship; if (objAsShip == null) return false; else return Equals(objAsShip); } public override int GetHashCode() { return ShipNum; } public bool Equals(Ship other) { if (other == null) return false; return (this.ShipNum.Equals(other.ShipNum)); } ***public static void Add(Ship) { List ships = new List { new Ship() { ShipName = "Battleship", ShipNum = 1 }, new Ship() { ShipName = "Cruiser", ShipNum = 2 }, new Ship() { ShipName = "Submarine", ShipNum = 3 }, new Ship() { ShipName = "Attacker", ShipNum = 4 }, new Ship() { ShipName = "The Soldier", ShipNum = 5 }, new Ship() { ShipName = "Boat", ShipNum = 6 }*** }; ; Console.WriteLine(); foreach (Ship aShip in ships) { Console.WriteLine(aShip); } Console.WriteLine(" Contains(\"1\"): {0}", ships.Contains(new Ship { ShipNum = 1, ShipName = "" })); Console.WriteLine(" Insert(2, \"2\")"); ships.Insert(2, new Ship() { ShipName = "Cruiser", ShipNum = 2 }); //Console.WriteLine(); foreach (Ship aShip in ships) { Console.WriteLine(aShip); } Console.WriteLine(" Parts[3]: {0}", ships[3]); Console.WriteLine(" Remove(\"1534\")"); ships.Remove(new Ship() { ShipNum = 3, ShipName = "Submarine" }); Console.WriteLine(); foreach (Ship aShip in ships) { Console.WriteLine(aShip); } Console.WriteLine(" RemoveAt(3)"); ships.RemoveAt(3); Console.WriteLine(); foreach (Ship aShip in ships) { Console.WriteLine(aShip); } } public void clear() { Console.Clear(); } public bool Attack(int Row, int Col) { if (this.Orientation == ShipOrientation.Horizontal) return ( Row == this.Position_Row && Col >= Position_Col && Col <= (Position_Col + ExtentUnits - 1) ); else return ( Col == this.Position_Col && Row >= Position_Row && Row <= (Position_Row + ExtentUnits - 1) ); } public enum ShipOrientation { Horizontal = 0, Vertical } } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Question

LO5 Illustrate the steps in developing a base pay system.

Answered: 1 week ago

Question

LO3 Outline strategic compensation decisions.

Answered: 1 week ago