Question

In: Computer Science

C# Question: OO Design Our battleships game needs several different kinds of ship – we need...

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.

Solutions

Expert Solution

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:


Related Solutions

We are going to create our own implementation of the game “FloodIt”. Several versions of this...
We are going to create our own implementation of the game “FloodIt”. Several versions of this game can be found, for example a version for android, on which this description is based, here. Several Web versions are available In this game, a square board is filled with dots, each having one of six possible colours. The players initially “captures” the top left dot. The player keeps choosing one of the six colours. Each time a colour is chosen, the dots...
use c++ This question is about providing game logic for the game of craps we developed...
use c++ This question is about providing game logic for the game of craps we developed its shell in class. At the end of the game, you should ask the user if wants to play another game, if so, make it happen. Otherwise, quit **Provide the working output of this game and the picture of the output. ** This is an implementation of the famous 'Game of Chance' called 'craps'. It is a dice game. A player rolls 2 dice....
As a class, we added several different reagents to our Winogradsky columns. We also made one...
As a class, we added several different reagents to our Winogradsky columns. We also made one control-column that had nothing added to it (aside from sediment and water). Everyone added paper towels (but at varying depths), and sulfate to the bottom of their columns. The rest of the treatments are listed below. Please indicate what types of organisms you might enrich with that type of treatment and why, or how and why that treatment might result in a different type...
At We Ship Anything, we need to create a program that will calculate the charges associated...
At We Ship Anything, we need to create a program that will calculate the charges associated with the weight of a specific package. We charge a base rate of $54.03 for any package and then add a premium to it based on the package weight. The additional costs are as follows: • If the package weighs less than 2.5 kg then we charge an additional $2.00. • If the package weighs 2.5kg to 5kg then we charge an additional $3.00....
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the following members: • A field for the name of the ship (a string). • A field for the year that the ship was built (a string). • A constructor and appropriate accessors and mutators. • A toString method that overrides the toString method in the Object class. The Ship class toString method should display the ship’s name and the year it was built. Design...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the following members: • A field for the name of the ship (a string). • A field for the year that the ship was built (a string). • A constructor and appropriate accessors and mutators. • A toString method that overrides the toString method in the Object class. The Ship class toString method should display the ship’s name and the year it was built. Design...
Advergaming; Are we exploiting our children? Write a response to the question "Are we exploiting our...
Advergaming; Are we exploiting our children? Write a response to the question "Are we exploiting our children" ( minimum 400 words).
Question 2 A text-based adventure game has several types of player. The code for the game...
Question 2 A text-based adventure game has several types of player. The code for the game uses the following class, Character to define all the shared elements for different types of player characters (Warrior, Wizard etc). 1. public class Character { 2. private String name; 3. private int life; 4. protected int hitPoints; 5. public Character(String name, int life, int hitPoints) { 6. this.name = name; 7. this.life = life; 8. this.hitPoints = hitPoints; 9. } 10. public void setHitPoints(int...
Why do we need life insurance? Who needs life insurance? Describe the different policy types and...
Why do we need life insurance? Who needs life insurance? Describe the different policy types and the advantages and disadvantages of each?
You have been assigned to design an experiment to test the effect of 5 different kinds...
You have been assigned to design an experiment to test the effect of 5 different kinds of fertilizer on the yield of strawberries grown hydroponically (using water rather than soil). How many plants would you use in this study and why? (2 points) What would you do for a control and why ? (2 points) How would you assign plants between control and treatment groups and why would you do it that way ? (2 points) What would be your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT