Battleships meets Minesweeper.
★★★A game is played on an 8x8 grid of squares. Ten enemy military buildings each occupying one square and are hidden from the player. At the start of the game a UAV may reveal the location of a building. Either the building itself, or an adjacent square horizontally, vertically or diagonally. A player has 50 missiles (turns) to correctly target the ten installations by entering an x and y coordinate to fire at. The state of the board is shown to the player after each missile is launched as follows:
”-“ an unknown square.
“H” a building that has been correctly targeted and hit.
“x” a miss.
“u” a UAV marker.
Write a program to play the game, positioning the buildings randomly in the grid.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
get_target that:place_buildings that:uav that:"u" marker on one of its surrounding squares (horizontally, vertically or diagonally), chosen randomly, that does not already contain a building.hit that:"H" (hit)."x" (miss).draw_board that:play_game that:place_buildings to place the buildings on the game board.uav to place the uav markers on the game board.get_target and hit to get the player input and handle the result."-".play_game is called.10 military installations sighted. You have 50 cruise missiles to destroy them!
01234567
0--------
1--------
2--u-----
3------u-
4--u-----
5----u---
6--------
7--------
Enter the x target:
1
Enter the y target:
2
Miss. Only 49 missiles left.
…
get_target
// Function to return a list of x, y coordinates to fire at
static int[] get_target()
{
---
Console.WriteLine("Enter the x target:");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the y target:");
int y = Convert.ToInt32(Console.ReadLine());
return new int[] { x, y };
}
place_buildings
// Place the buildings on the board
static void place_buildings()
{
---
// Place 10 buildings
for (int building = 0; building < 10; building++)
{
---
bool valid_spawn = false;
int x = 0;
int y = 0;
---
// Two buildings cannot occupy the same square
while (!valid_spawn)
{
---
x = random_generator.Next(0, 8);
y = random_generator.Next(0, 8);
// Check if the square already has a building
if (board[x, y] != "T")
{
---
valid_spawn = true;
---
}
---
}
---
board[x, y] = "T";
---
}
}
uav
static void uav()
{
---
// Scan the whole board for buildings
for (int x = 0; x < 8; x++)
{
---
for (int y = 0; y < 8; y++)
{
---
if (board[x, y] == "T")
{
---
// Collect the surrounding squares that are on the board and
// do not already contain a building
List<int[]> candidates = new List<int[]>();
---
for (int dx = -1; dx < 2; dx++)
{
---
for (int dy = -1; dy < 2; dy++)
{
---
int nx = x + dx;
int ny = y + dy;
---
if ((dx != 0 || dy != 0)
&& nx >= 0 && nx < 8
&& ny >= 0 && ny < 8
&& board[nx, ny] != "T")
{
candidates.Add(new int[] { nx, ny });
}
---
}
}
---
// Place a "u" marker on a random one of them
int[] chosen = candidates[random_generator.Next(candidates.Count)];
board[chosen[0], chosen[1]] = "u";
---
}
}
}
}
hit
// Return if the target was a hit or not
static bool hit(int[] target)
{
---
int x = target[0];
int y = target[1];
// If square contains a building it is a hit
if (board[x, y] == "T")
{
---
board[x, y] = "H";
return true;
---
}
---
// otherwise it is a miss
else
{
---
board[x, y] = "x";
return false;
---
}
}
draw_board
static void draw_board()
{
---
Console.WriteLine(" 01234567");
// Draw rows
for (int y = 0; y < 8; y++)
{
---
Console.Write(y);
// Draw columns
for (int x = 0; x < 8; x++)
{
---
// Hide buildings and draw a hyphen for unexplored squares
if (board[x, y] == "T")
{
---
Console.Write("-");
---
}
---
// If the square is explored show it
else if (board[x, y] == "u" || board[x, y] == "H" || board[x, y] == "x")
{
---
Console.Write(board[x, y]);
---
}
---
else
{
---
Console.Write("-");
---
}
---
}
---
Console.WriteLine();
---
}
}
play_game, main program
// Play the game
static void play_game()
{
---
place_buildings();
uav();
Console.WriteLine("10 military installations sighted. You have 50 cruise missiles to destroy them!");
int score = 0;
int missiles = 50;
---
// Play until the game is won or there are no missiles left
while (score < 10 && missiles > 0)
{
---
draw_board();
int[] target = get_target();
missiles--;
// If the shot was a hit increase the score
if (hit(target))
{
---
score++;
Console.WriteLine("That's a hit! You have now scored " + score);
---
}
---
else
{
---
Console.WriteLine("Miss. Only " + missiles + " missiles left.");
---
}
---
}
---
// If the player has scored 10 they have won
if (score == 10)
{
---
Console.WriteLine("All buildings have been destroyed. You WIN.");
---
}
---
else
{
---
Console.WriteLine("You are out of missiles. You lose. Your score is " + score);
---
}
}
---
public static void Main(string[] args)
{
---
// Initialize the board with dashes to represent unexplored squares
for (int x = 0; x < 8; x++)
{
---
for (int y = 0; y < 8; y++)
{
---
board[x, y] = "-";
---
}
---
}
---
play_game();
}