Requirements for Battleship using C# Windows Form 1. Two grids to represent the
ID: 3764233 • Letter: R
Question
Requirements for Battleship using C# Windows Form
1. Two grids to represent the game board. One grid will be used for the placement of Player 1 ships and will mark the hits and misses of Player 2. The other grid will be used to represent the placement of Player 2s ships and will mark the hits and misses of Player 1. The Player 2 grid will not display the positions of the ships; only the hits and misses. Grid sizes should be 12x12 using an ALPHA nomenclature (running vertical) and a NUMERIC nomenclature (running horizontal).
2. The form should allow Player 1 to place the following ships
a. Carrier – representing 5 cells
b. Battleship – representing 4 cells
c. Submarine – representing 3 cells
d. Cruiser – representing 3 cells
e. PT Boat – representing 2 cells
3. No two ships can possess the same locations. Ships can only be placed horizontally or vertically.
4. Once the ships are placed, Player 1 should click a button to start the game. The Player 2 grid should also have ship placement done around this time, and the game will commence.
5. For Player 1, when a ship is placed into a series of cells, each cell should contain an accurate representation of what that ship type is. You may use a letter, image, or other text identifier of your choice. Note: Player 2’s ships should not be identifiable to Player 1. Player 1 should only know what a given ship is at the point is has been sunk. You will need to display something to Player 1 that a specific ship of Player 2’s has been sunk.
6. Each player (starting with Player 1) will take turns calling locations; as is similar with the normal rules of Battleship. Player 2 will be the computer which will randomly call out grid cell positions (aka coordinates).
>>> Advanced targeting approach: If you want to make it target a smaller area when a hit is made (to better find and sink a ship), then you may attempt to so. The logic represents accurate “homing in on the target”. <<<
7. In a section of the form, you will need to keep a log of each player’s turns and coordinate locations. Also record the event of a ship getting sunk for the respective player.
8. When a coordinate is called, mark the position with a red (for hit) and white (for miss).
9. Once all the ships for a player have been sunk, you will display some sort of victory message (or losing message) to Player 1.
10. At any point during the game Player 1 can quit. When quitting, the user should be prompted for confirmation of their option. If they continue to quit, then the application ends. If they cancel quitting, the game continues where they left off.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Battleship
{
class Program
{
static void Main(string[] args)
{
RunUserCode();
Console.ReadLine();
}
static int[,] sea;
static int BATTLESHIP = 4;
static int CRUISER = 3;
static int SUBMARINE = 3;
static int PTBOAT = 2;
static int CARRIER = 5;
static int EMPTY_SEA = 0;
static int ROW;
static int COLUMN;
static string blankRow;
static void RunUserCode()
{
Console.WriteLine("Welcome to Battleships");
sea = new int[CARRIER, CARRIER];
blankRow = new string(Char.Parse(" "), (CARRIER * 2) + 2);
DrawGameBoard();
}
private static void DrawGameBoard()
{
DrawColumnHeaders();
for ( ROW = EMPTY_SEA; ROW < CARRIER; ROW++ )
{
Console.Write(Environment.NewLine);
string rowHeader = ROW.ToString() + " ";
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(rowHeader);
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.DarkCyan;
for ( COLUMN = EMPTY_SEA; COLUMN < CARRIER; COLUMN++ )
{
int cellContent = sea[COLUMN, ROW];
if ( cellContent > 0 )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(cellContent + " ");
Console.ForegroundColor = ConsoleColor.Gray;
}
else
{
Console.Write(cellContent + " ");
}
}
Console.Write(Environment.NewLine);
Console.Write(blankRow);
Console.ResetColor();
}
Console.Write(Environment.NewLine);
}
private static void DrawColumnHeaders()
{
Console.WriteLine();
string Header = " ";
Console.Write(Header);
for ( ROW = EMPTY_SEA; ROW < CARRIER; ROW++ )
{
string rowHeader = ROW.ToString() + " ";
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(rowHeader);
Console.ResetColor();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.