Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# Question: OO Design Our battleships game needs several different kinds of shi

ID: 3805414 • Letter: C

Question

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.

Explanation / Answer

int shipCounter = 0, trend = 0; static Random rnd = new Random(); bool gameOver = false, playerTurn = false; int[] score = { 0, 0 }; struct gameData { public bool occupied, hit, marked; } gameData[,,] data; public void computerMove() { Point target = seekTarget(); try { if (data[1, target.X, target.Y].hit) computerMove(); else { data[1, target.X, target.Y].hit = true; if (data[1, target.X, target.Y].occupied) { attacking = true; score[0]++; computerMove(); } } playerTurn = true; } catch (IndexOutOfRangeException) { computerMove(); } } public Point seekTarget() { Point origin = new Point(-1, -1); //find a point that's been hit. int x = 0, y = 0; while (x = -1 && ((!data[1, lim[0].X, lim[0].Y].hit && !data[1, lim[0].X, lim[0].Y].occupied) || (data[1, lim[0].X, lim[0].Y].hit && data[1, lim[0].X, lim[0].Y].occupied))) { lim[1].Y--; if (lim[1].Y == -1) break; } while (lim[2].X