Module 2 Programming Assignment – Battleship Refactor Refactor your Battleship drawing code from Module 1. Download the OO...

90.2K

Verified Solution

Question

Programming

Module 2 Programming Assignment – BattleshipRefactor

Refactor your Battleship drawing code from Module 1.

Download the OO template for the basic battleship game,BattleshipRefactor.zip (refer below)

The template has the outline of a battleship game using OOconstructs. You need to add the Grid class and complete the code sothat it runs correctly.

At a minimum, you need to complete the Draw and DropBomb methodswith code similar to the previous assignment. There will be changesdue to the different layout, but the core of your code will do thesame thing.

In this Module we are adding 2 new features:

  1. Variable sized grid ­ the trivial part of this is making a 2Darray based on the passed in size. The second part is adding someships to that grid ­ we cannot use the constant array from Module1. You need to come up with a very simple algorithm to add a fewships to the grid. This is purely test code so it doesn't have tobe sophisticated in any way (there is time for that later in theclass). If you can't think of any ways ask in the forums. Do notask the user (your teacher) for input to place these ships.
  2. Game over detection - This will be implemented in the readonlyproperty Grid.HasShipsLeft. In this property you will determine ifthere are any ships left not hit with a bomb. The algorithm shouldlook through the array for any remaining ships each time theproperty is read.

You may add as many other methods, fields or properties as youfeel are required for the assignment.

Notes:

The code will be tested with gridsranging in size from 5 (the smallest you can fit a size 5 ship)upwards. Make sure that all aspects of the game work correctlywithout crashing or hanging.

You must use the template codeprovided!

Like in Module 1, please make sure youshow the ships in the grid. At this point we are merely testing ourgame logic and hidden ships make the game very hard to test.

BattleShipGame.cs

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);

}
}
}
}

Program.cs

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);
  
}
}
}

Answer & Explanation Solved by verified expert
4.4 Ratings (675 Votes)
Working code implemented in C and appropriate comments provided for better understanding Here I am attaching code for all files Programcs using System namespace BattleshipSimple class Program static void Mainstring args var game new BattleShipGame10 ConsoleKeyInfo response do gamePlay ConsoleWriteLineDo you want to play again yn response ConsoleReadKey while responseKey ConsoleKeyY Gridcs using System using SystemCollectionsGeneric using SystemLinq using SystemText using SystemThreadingTasks this class is designed to handle the data pertaining to the game space namespace BattleshipSimple class Grid variables for parsing the different console colors ConsoleColor planeForground ConsoleForegroundColor Dictionary ShipColors new Dictionary variable for holding the master unaltered grid internal char masterGrid internal int size initialize the grid and put the colors in the dictionary public Gridint gridSize load the colors into the dictionary ShipColorsAddS ConsoleColorBlue ShipColorsAddP ConsoleColorCyan ShipColorsAddA ConsoleColorGreen ShipColorsAddB    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students