Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# Need to make a battleship program that does this: The template (code below) has the outline of a battleship game using OO constructs. You

C#

Need to make a battleship program that does this:

The template (code below) has the outline of a battleship game using OO constructs. You need to add the Grid class and complete the code so that it runs correctly.

At a minimum you need to complete the Draw and DropBomb methods with code similar to the previous assignment. There will be changes due to the different layout but the core of your code will do the same thing.

This week we are adding 2 new features:

Variable sized grid - the trivial part of this is making a 2d array based on the passed in size. The second part is adding some ships to that grid - we cannot use the constant array from week #1. You need to come up with a very simple algorithm to add a few ships to the grid. This is purely test code so it doesn't have to be sophisticated in any way (there is time for that later in the class). If you can't think of any ways ask in the forums.

Game over detection. This will be implemented in the readonly property Grid.HasShipsLeft. In this property you will determine if there are any ships left not hit with a bomb by looking through the array for any remainng ships and returning true or false.

This is the code provided:

using System; namespace BattleshipSimple { internal class BattleShipGame { private Grid grid; public BattleShipGame(int gridSize) { grid = new Grid(gridSize); } internal void Reset() { grid.Reset(); } internal void Play() { while (grid.HasShipsLeft) { grid.Draw(); //Read input from user into x, y int x, y; grid.DropBomb(x, y); } } } } 
using System; namespace BattleshipSimple { class Program { static void Main(string[] args) { var game = new BattleShipGame(10); ConsoleKeyInfo response; do { game.Reset(); game.Play(); Console.WriteLine("Do you want to play again (y/n)"); response = Console.ReadKey(); } while (response.Key == ConsoleKey.Y); } } } 

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions

Question

Extensive line management responsibility for HR issues.

Answered: 1 week ago