Download the OO template for the basic battleship game BattleshipRefactor.zip

The template 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:

1.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. Do not ask the user (your teacher) for input to place these ships...

2.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. The algorithm should look through the array for any remaining ships each time the property is read.

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

Notes:

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

•You must use the template code provided!

•Like week #1 please make sure you show the ships in the grid, at this point we are merely testing our game logic and hidden ships make the game very hard to test.

Separately zip the entire contents of each folder containing your project/exercises using the method described in seminar 1.

seminar 1 program:

using System;

namespace BattleshipSimple

{

class Program

{

private static readonly char[,] Grid = new char[,]

{

{'.', '.', '.', '.', 'S', 'S', 'S', '.', '.', '.'},

{'P', 'P', '.', '.', '.', '.', '.', '.', '.', '.'},

{'.', '.', '.', '.', '.', '.', '.', '.', '.', 'P'},

{'.', '.', '.', '.', '.', '.', '.', '.', '.', 'P'},

{'.', '.', 'A', 'A', 'A', 'A', 'A', '.', '.', '.'},

{'.', '.', '.', '.', '.', '.', '.', 'B', '.', '.'},

{'.', 'S', '.', '.', '.', '.', '.', 'B', '.', '.'},

{'.', 'S', '.', '.', '.', '.', '.', 'B', 'P', 'P'},

{'.', 'S', '.', '.', '.', '.', '.', 'B', '.', '.'},

{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},

};

static void Main(string[] args)

{

const string TOPBOT = " --#---#---#---#---#---#---#---#---#---#---#";

Console.ForegroundColor = ConsoleColor.Gray;

while (true)

{

// Display the grid

Console.WriteLine(" | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|");

for (inti = 0; i < 10; i++)

{

Console.WriteLine(TOPBOT);

Console.Write(" " + ((char)('A' + i)) + " ");

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

{

Console.Write("|");

// Check the grid to set the colors of the cell

switch (Grid[i, j])

{

case 'S':

Console.BackgroundColor = ConsoleColor.Red;

Console.ForegroundColor = ConsoleColor.Black;

break;

case 'A':

Console.BackgroundColor = ConsoleColor.Yellow;

Console.ForegroundColor = ConsoleColor.Black;

break;

case 'P':

Console.BackgroundColor = ConsoleColor.Blue;

Console.ForegroundColor = ConsoleColor.Black;

break;

case 'B':

Console.BackgroundColor = ConsoleColor.Cyan;

Console.ForegroundColor = ConsoleColor.Black;

break;

case 'X':

// If the cell has been guessed, check if it was

// a ship and set the color accordingly

if (Grid[i, j] != '.')

Console.ForegroundColor = ConsoleColor.Red;

break;

}

Console.Write(" " + Grid[i, j] + " ");

// Reset the colors

Console.BackgroundColor = ConsoleColor.Black;

Console.ForegroundColor = ConsoleColor.Gray;

}

Console.Write("|");

Console.WriteLine();

}

Console.WriteLine(TOPBOT);

// Deal with player input

Console.Write("\nEnter your guess (e.g., B3) or Q to quit: ");

var guess = Console.ReadLine().Trim();

guess = (guess.Length > 0) ? guess.ToUpper() : ".";

char firstChar = guess[0];

// Process the guess

if (firstChar == 'Q')

{

Console.WriteLine("\nThanks for playing Battleship\n");

break;

}

// Deal with guesses of the form <letter<integer>

// Check for letter and integer both being in-range

bool validGuess = false;

if ('A' <= firstCharfirstChar <= 'J')

{

string numberString = guess.Substring(1);

int x = firstChar - 'A';

int y;

if (int.TryParse(numberString, out y))

{

if (1 <= y & y <= 10)

{

Grid[x, y - 1] = 'X';

validGuess = true;

}

}

}

// Display a message if the input was an invalid guess

if (!validGuess)

{

Console.Write("Please enter a valid guess. Press [Enter] and try again. ");

Console.ReadLine();

}

// Clear the display so that the grid always displays at the top

Console.Clear();

}

} // end Main()

} // end class Program

} // end namespace BattleshipSimple