Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# Help Already submitted my assignment but want to see a properly coded version of this as I am sure I made many errors and

C# Help

Already submitted my assignment but want to see a properly coded version of this as I am sure I made many errors and find that reviewing others code done properly helps me a lot. also I think this will help others who use Chegg to study.

Question-

Create a program to print a battleship grid to the console and mark squares as destroyed

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) { while (true) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.Write(Grid[i, j]); } Console.WriteLine(); }

Console.WriteLine("Enter your guess:"); var guess = Console.ReadLine(); } } } }

In the game of battleships, the game play area is a square grid containing ships. Players take it in turns to guess the location of the ships until the battleship is sunk.

A simple solution has been provided that has the basics of rendering a grid and asking for input. It does not look very pretty.

Your assignment is to improve on the rendering and complete the input part of the gameplay.

Improve the rendering so that each grid square is represented by a box created out of characters. e.g. #---# | | #---#

Improve the rendering so that each ship type is in a different color. Ships should be visible, not hidden from view. Yes, thats a bad game but we are just using this to test our algorithms and design. e.g. #---# | S | #---#

NOTE: the S is colored red as is the blank squares on both sides.

Improve the rendering so that the grid has horizontal labels (A through J) and vertical labels (1 through 10)

Parse the input to get the guess in the form LetterNumber e.g. B7, G10, A1. You will find String.SubString and Int32.TryParse to be useful API calls. Invalid input should cause the input to be asked again it should not be possible to crash your program with bad input

When you have a valid input mark the grid location as used and then render an X in the square to indicate it has been used. The X should be Red if it was a hit on a ship. e.g. Miss Hit #---# #---# | X | | X | #---# #---#

NOTE: The second X is colored red in this example

Notes:

You may not modify the char array to store the extra characters to print. The array should only store information about the gameboard and the extra output should be generated by your code.

Your grid rendering must display the ships. At this stage we are just testing parts of the game so we dont need them hidden from the user. (and it makes it much harder for one of your users to grade!)

You do not need to determine end of game conditions, validate repeated hits or any gameplay however feel free to add any of those things if you feel the urge.

There are many useful APIs in the Console class. You may render the grid line by line using Write and WriteLine or you may render it using the SetCursorPosition API. You may redraw the entire grid each time or simply replace the sections that have changed.

You may find it useful to set the size of the console window to something a little larger. There are APIs to do this in the Console class. The example above is just one style of drawing feel free to experiment with different characters and grid sizes.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions