Question

In: Computer Science

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

Module 2 Programming Assignment – Battleship Refactor

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 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.

In this Module 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 Module 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 size 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 in Module 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.

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

Solutions

Expert Solution

Working code implemented in C# and appropriate comments provided for better understanding.

Here I am attaching code for all files:

Program.cs:

using System;


namespace BattleshipSimple
{
class Program
{
static void Main(string[] args)
{
var game = new BattleShipGame(10);
ConsoleKeyInfo response;
do
{
game.Play();

Console.WriteLine("Do you want to play again (y/n)");
response = Console.ReadKey();

} while (response.Key == ConsoleKey.Y);
  
}
}
}
Grid.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//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 = Console.ForegroundColor;
Dictionary<char, ConsoleColor> ShipColors = new Dictionary<char, ConsoleColor>();
//variable for holding the master, unaltered grid.
internal char[,] masterGrid;
internal int size;

//initialize the grid and put the colors in the dictionary.
public Grid(int gridSize) {
//load the colors into the dictionary
ShipColors.Add('S', ConsoleColor.Blue);
ShipColors.Add('P', ConsoleColor.Cyan);
ShipColors.Add('A', ConsoleColor.Green);
ShipColors.Add('B', ConsoleColor.Yellow);
ShipColors.Add('C', ConsoleColor.Magenta);
ShipColors.Add('.', planeForground);
masterGrid = new char[gridSize, gridSize];
size = gridSize;

}

//if it is now Hit, Miss, or '.' then
internal void DropBomb(int X, int Y)
{
if (masterGrid[X, Y] != 'H' | masterGrid[X, Y] != 'M' | masterGrid[X, Y] != '.')
{ //than mark it as a hit.
masterGrid[X, Y] = 'H';
}
else
{ //else mark it as a miss
masterGrid[X, Y] = 'M';
}
}

//draws the current information from "workingGrid" onto the console.
public void Draw()
{
//first clear the console
Console.Clear();

//write the top letter guide
Console.Write(" ");
for(int i = 0; i < size; i++)
{ //write the correct alphabetical char to the spot on the grid
Console.Write(Convert.ToChar(65 + i));
if(i != size - 1)
{ //if we are not at the end, add a '-'
Console.Write("-");
}

}

Console.WriteLine();
//iterate through the rows
for (int row = 0; row < size; row++)
{
//and columns
for (int column = 0; column < size; column++)
{
//if we are on the first column of the line, add the row number.
if (column == 0)
{
Console.Write("{0,2}", row + 1);
}

//if there is a hit at that location, set it to red and print an X
if (masterGrid[row, column] == 'H')
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("X");
Console.ForegroundColor = planeForground;
}
else if (masterGrid[row, column] == 'M')
{
Console.Write("X");
}
//if there is a miss at the location, set the color to
else
{

Console.ForegroundColor = ShipColors[masterGrid[row, column]];
Console.Write(masterGrid[row, column]);
Console.ForegroundColor = planeForground;

}

//add the last piece of the grid to the row
Console.Write("|");
}
//end the current line and print the next bar to the console.
Console.WriteLine();
}

//now print the lower part of the grid.
Console.Write(" ");
for (int i = 0; i < size; i++)
{
Console.Write("#");
if (i != size - 1)
{
Console.Write("-");
}

}

}

//replace every value inside the grid with '.'
public void Reset()
{
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
masterGrid[row, col] = '.';
}
}
}

//checks to see if the grid has anything other than '.', 'H', or 'M' left.
public bool HasShipsLeft()
{
for (int row = 0; row < size; row++)
{
for (int col = 0; col < size; col++)
{
if (masterGrid[row, col] != '.' | masterGrid[row,col] != 'H' | masterGrid[row,col] != 'M' )
{
return true;
}
}
}
return false;
}

//returns true if the ship will fit in the horizontal direction
bool horizontalCheck(int x, int y, int length)
{
try
{
for (int i = 0; i < length; i++)
{
if (masterGrid[x, y + i] != '.')
{
return false;
}
}
return true;
}

catch (IndexOutOfRangeException)
{
return false;
}
}

//returns true if the ship will fit in the vertical direction
bool verticalCheck(int x, int y, int length)
{
try
{
for (int i = 0; i < length; i++)
{
if (masterGrid[x + i, y] != '.')
{
return false;
}
}
return true;
}

catch (IndexOutOfRangeException)
{
return false;
}
}

//populate generates a given number of ships onto the grid in random locations.
public void populate(int shipCount)
{
Reset(); //first, reset the grid to be empty.
Random rng = new Random(); //ship placement will be random.
int row = 0; //variables for the row and column
int col = 0;   
bool vertHor = false; //vert is true, hor is false.
int shipNum = 0; //this variable holds what ship we are working on out of our total ships.
int shipSize = 1; //shipSize holds the amount of squares the ship takes. initialized to 1 because you cant have a ship of size 0
char shipChar = 'S'; //shipChar holds the char that the ship will be. initialized to 'S' by default.


//iterate for each ship that needs placing.
do
{
//get a random location and direction for the ship
row = rng.Next(0, size);
col = rng.Next(0, size);
//generate a random true or false.
vertHor = (rng.Next(100) >= 50);

switch (shipNum + 1)
{
case 1: shipSize = 1; shipChar = 'S'; break;
case 2: shipSize = 2; shipChar = 'P'; break;
case 3: shipSize = 3; shipChar = 'A'; break;
case 4: shipSize = 4; shipChar = 'B'; break;
case 5: shipSize = 5; shipChar = 'C'; break;
default: shipSize = 1; shipChar = 'S'; break;
}
  
//first check to see if the desired location is available
if(masterGrid[row,col] == '.')
{
//make sure the ship has room.
if(horizontalCheck(row,col,shipSize) == true & verticalCheck(row, col, shipSize) == true)
{
//if there is room, increment the counter. if there is not room, the counter does not get incremented.

for(int i = 0; i < shipSize; i++)
{
if (vertHor)
{
masterGrid[row, col + i] = shipChar;
}
else
{
masterGrid[row + i, col] = shipChar;
}
}
shipNum++;
}
}
} while (shipNum < shipCount);
}
}
}
BattleShipGame.cs:

using System;


namespace BattleshipSimple
{
internal class BattleShipGame
{
private Grid grid;

public BattleShipGame(int gridSize)
{
grid = new Grid(gridSize);
grid.populate(5);
  
}

internal void Reset()
{
grid.Reset();
}

//quick function to convert chars 'a' through 'z' to an int 0 - 24 (25? off by one errors always confuse me)
private int letterToInt(string letter)
{
return letter[0] - 65;
}

internal void Play()
{
while (grid.HasShipsLeft())
{
grid.Draw();

//get the input from the user
string[] raw;
Console.WriteLine("\nEnter a coordinate in the format A:1");
//parse it, and execute dropbomb
raw = Console.ReadLine().Split(':');
grid.DropBomb(Int32.Parse(raw[1]) - 1, letterToInt(raw[0]));

}
}
}
}


Related Solutions

Download this file to your hard drive and follow the instructions in the Module 9 Assignment...
Download this file to your hard drive and follow the instructions in the Module 9 Assignment (Word) document. Prepare two files for your post. The first file should be a text file containing your Python solution. The second file should contain your output file(txt). Complete the following using Python on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples:...
Module 07 Written Assignment - C-Diff Your written assignment for this module should be a 1-2...
Module 07 Written Assignment - C-Diff Your written assignment for this module should be a 1-2 page paper (not including title page and reference page) that describes the following: -You are caring for a patient with c-diff as part of your workload assignment. Discuss what c-diff is and how it is transmitted (how you can get it)? -What actions will you take as a nurse to protect yourself and the other patients on the unit when taking care of your...
This is an assignment for python. Download the code “ GradeBook.py ” You will be modifying...
This is an assignment for python. Download the code “ GradeBook.py ” You will be modifying the code to do the following: 1) create an empty dictionary named “FinalAverages” 2) In one for loop you will zip all lists together and get their individual members on each iteration. You can name these what ever you want. 3) on each iteration: Calculate the WEIGHTED average using the weights provided, and then add a new dictionary value to the dictionary “ FinalAverages...
Module 11 Written Assignment-Comprehensive Final (Nursing 1) Your written assignment for this module should be a...
Module 11 Written Assignment-Comprehensive Final (Nursing 1) Your written assignment for this module should be a 1-2 page paper (not including title page and reference page) that describes the following: 1. What did you learn from this course that you did not already know? 2. How will you apply what you learned to your patient care?
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files...
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files from the Internet. This program is very similar to wget utility in Unix/Linux environment.The synopsis of SimpleWebGet is: java SimpleWebGet URL. The URL could be either a valid link on the Internet, e.g., www.asu.edu/index.html, or gaia.cs.umass.edu/wireshark-labs/alice.txt or an invalid link, e.g., www.asu.edu/inde.html. ww.asu.edu/inde.html. The output of SimpleWebGet for valid links should be the same as wget utility in Linux, except the progress line highlighted...
Your written assignment for this module should be a 1-2 page paper (not including title page...
Your written assignment for this module should be a 1-2 page paper (not including title page and reference page) that describes the following: What are the different types of immunities and give an example of each Describe how stress impacts the immunity of a person You should include a minimum of 3 scholarly references (suggestion on using SkyScape). Include a title page, in-text citations, and a reference page in APA format. Submit your completed assignment by following the directions linked...
Your written assignment for this module should be a 1-2 page paper (not including title page...
Your written assignment for this module should be a 1-2 page paper (not including title page and reference page) that describes the following: What are the different types of immunities and give an example of each Describe how stress impacts the immunity of a person You should include a minimum of 3 scholarly references. Include a title page, in-text citations, and a reference page in APA format
Your written assignment for this module should be a 1-2 page paper (not including title page...
Your written assignment for this module should be a 1-2 page paper (not including title page and reference page) that describes the following: Describe what a fluid and electrolyte imbalance is and how this is important to the function of the body? Pick a fluid or electrolyte imbalance and describe how the patient would present, in addition to the treatment (nursing and expected medical)?
For this programming assignment, you will use your previous code that implemented a video game class...
For this programming assignment, you will use your previous code that implemented a video game class and objects with constructors. Add error checking to all your constructors, except the default constructor which does not require it. Make sure that the high score and number of times played is zero or greater (no negative values permitted). Also modify your set methods to do the same error checking. Finally add error checking to any input requested from the user. #include <iostream> #include...
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your...
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your code to enhance the user experience and to use objects and classes. All functional requirements in Project 1 remain, except where enhancing the system replaces specific functions. Functional Requirements The console entry point for the user inputs is on the same line as the prompt. (new) User enters name at the beginning of a session. System covers four math operations – addition, subtraction, multiplication,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT