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

C# Program: Our Battleship game needs several different kinds of ship – we need

ID: 3915837 • Letter: C

Question

C# Program: Our Battleship 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 o A Position is composed of an X and Y coordinate – you should create a struct to encapsulate this. • A read-only length o 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 o The constructor for each inherited type should set this to a different ConsoleColor for drawing. • A flag called Sunk o This should default to false for all ships. • A property called IsBattleship o This property should be overridden by each child type. Only the BattleShip should return true. • A method called Reset o This method should reset the members to their empty defaults • A method called Place(Position start, Direction direction) o Direction should be an enumeration of either Horizontal or Vertical. o 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 the methods and ship types. Given that the positions array is private, how can you test that the values are correct?

Explanation / Answer

1st method

using System;

{

class Battleships

{

const int seasize = 10;

const int EMPTY_SEA = 0, ATTACKED = 1, BATTLESHIP = 2, CRUISER = 3, SUBMARINE = 4, ROWINGBOAT = 5;

static int[,] sea = new int[seasize, seasize];

static void clearsea(){

//Clears the sea to empty

for (int x = 0; x < 10; x++)

{

for (int y = 0; y < 10; y++)

{

sea[x, y] = 0;

}

}

  

}

static void drawsea() {

//Draws the sea on the users screen

for (int x = 0; x < 10; x++)

{

for (int y = 0; y < 10; y++)

{

Console.Write(sea[x, y]);

} Console.WriteLine("");

}

  

}

static void placeships() {

//Places ships in the sea

sea[0, 5] = BATTLESHIP;

  

sea[2, 7] = BATTLESHIP;

  

sea[0, 0] = CRUISER;

  

sea[2, 0] = CRUISER;

  

sea[4, 3] = CRUISER;

  

sea[5, 6] = SUBMARINE;

  

sea[8, 8] = ROWINGBOAT;

}

  

static void Main() {

clearsea();

placeships();

drawsea();

Console.ReadLine();

}

}  

}

2nd method

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace FirstApplication

{

class Battleship

{

public int shipPosition;

public string board = "------------- | 1 | 2 | 3 | ------------- | 4 | 5 | 6 | ------------- | 7 | 8 | 9 | -------------";

public bool shipIsHit = false;

public void PositionShip()

{

Random randomNumber = new Random();

shipPosition = randomNumber.Next(1, 10); // random number between 1 and 9

}

public void HitPosition(int hit)

{

if (hit == shipPosition)

{

board = board.Replace(hit.ToString(),"X");

shipIsHit = true;

}

else

{

board = board.Replace(hit.ToString(), "-");

}

}  

}

class Program

{

static void Main(string[] args)

{

Battleship game = new Battleship();

int guess;

bool isInteger;

game.PositionShip();

Console.WriteLine("You are playing battleship... Try to find the ship!");

do

{

Console.WriteLine(" Enter a number between 1 and 9");

Console.WriteLine("Board: {0}", game.board);

isInteger = Int32.TryParse(Console.ReadLine(), out guess);

if (isInteger && (guess >= 1 && guess <= 9))

{

game.HitPosition(guess);

}

else

{

Console.WriteLine("That's not a valid number!");

}

if (!game.shipIsHit)

{

Console.WriteLine("You missed!");

}

}

while (!game.shipIsHit);

Console.WriteLine(" Board: {0}", game.board);

Console.WriteLine("BOOM! You found the battleship at position {0}", game.shipPosition);

Console.ReadKey();

}

}

}