Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the code: C# In the game of Battleship, the game play area is a square

ID: 3756129 • Letter: C

Question

Complete the code:

C#

In the game of Battleship, 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.

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


#---#   #---#
| X |   | X |
#---#   #---#

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 don’t 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.

Code to fix:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {//Print grid
                printGrid(Grid);
                Console.WriteLine("Type 'quit' to exit.");
                Console.WriteLine("Enter your guess:");
                string cons = Console.ReadLine();
                cons = cons.ToUpper();
                char column = cons[0];
                char num = cons[1];
                //bool result = Int32.TryParse(cons, out num);
                string validChars = "ABCDEFGHIJ";
                


                //When characters are valid and number is less than 11
                if (cons == "QUIT")
                {
                    return;
                }
                else if (!validChars.Contains(column) || (num >= 11))
                {
                    Console.WriteLine("Please enter only A-J, and 1-10 e.g 'B5'");
                }
                Console.WriteLine("num = {0}, column = {1}", num, column);

            }
        }
        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', '.', '.'},
            {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
       };
        public static void printGrid(Char[,] Grid)
        {
            //Horizontal labels
            Console.WriteLine("   | A | B | C | D | E | F | G | H | I | J |");
            Console.WriteLine("---#---#---#---#---#---#---#---#---#---#---#");
            for (int i = 0; i < 10; i++)
            {
                if (i == 9)
                {
                    Console.Write("{0} ", i + 1);
                }
                else
                    Console.Write(" {0} ", i + 1);
                for (int j = 0; j < 10; j++)
                {
                    ShipColors(Grid[i, j]);
                }
                Console.Write("| ");
                Console.WriteLine("---#---#---#---#---#---#---#---#---#---#---#");
            }
        }
        public static void ShipColors(char useThis)
        {
            //A= Airplane, B=Boat, P=Patrol, S = Submarine,
            switch (useThis)
            {
                case '.':
                    Console.Write("|   ");
                    break;
                case 'A':
                    Console.Write("|");
                    Console.BackgroundColor = ConsoleColor.Blue;
                    Console.Write(" A ");
                    break;
                case 'B':
                    Console.Write("|");
                    Console.BackgroundColor = ConsoleColor.Green;
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.Write(" B ");
                    break;
                case 'P':
                    Console.Write("|");
                    Console.BackgroundColor = ConsoleColor.Yellow;
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.Write(" P ");
                    break;
                case 'S':
                    Console.Write("|");
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.Write(" S ");
                    break;
                case 'H':
                    Console.Write("|");
                    Console.ForegroundColor = ConsoleColor.Red;    //Shows a hit on a ship, by displaying red 'X'
                    Console.Write(" X ");
                    break;
                case 'M':
                    Console.Write("|");
                    Console.ForegroundColor = ConsoleColor.White; //Show a square was missed, but has been used
                    Console.Write(" X ");
                    break;
            }
            Console.ResetColor();
        }

    }
}


Explanation / Answer

Answer: