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

This needs to use the code given. Only slightly altered. C# Programming Summary

ID: 3757365 • Letter: T

Question

This needs to use the code given. Only slightly altered.

C# Programming

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

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.

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.

Questions to address:

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

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

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)

            {

                printGrid(Grid);

                Console.WriteLine("Type 'quit' to exit.");

                Console.WriteLine("Enter your guess:");

                string cons = Console.ReadLine();

                cons = cons.ToUpper();

                char column = cons[0];

                int num = cons[1];

                bool result = Int32.TryParse(cons, out num);

                string validChars = "ABCDEFGHIJ";

                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)

        {

            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)

        {

            //P = patrol baot

            //S = Submarine

            //B = Battleship

            //A = Aircraft carrier

            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;

                    Console.Write(" X ");

                    break;

                case 'M':

                    Console.Write("|");

                    Console.ForegroundColor = ConsoleColor.White;

                    Console.Write(" X ");

                    break;

            }

            Console.ResetColor();

        }

    }

}

Explanation / Answer

using System;

{

class Battleships

{

const int seasize = 10;

const int EMPTY_SEA = 0, ATTACKED = 1, BATTLESHIP = 2, CRUISER = 3, SUBMARINE = 4, ROWINGBOAT = 5;

static int[,] sea = new int[seasize, seasize];

static void clearsea(){

//Clears the sea to empty

for (int x = 0; x < 10; x++)

{

for (int y = 0; y < 10; y++)

{

sea[x, y] = 0;

}

}

  

}

static void drawsea() {

//Draws the sea on the users screen

for (int x = 0; x < 10; x++)

{

for (int y = 0; y < 10; y++)

{

Console.Write(sea[x, y]);

} Console.WriteLine("");

}

  

}

static void placeships() {

//Places ships in the sea

sea[0, 5] = BATTLESHIP;

  

sea[2, 7] = BATTLESHIP;

  

sea[0, 0] = CRUISER;

  

sea[2, 0] = CRUISER;

  

sea[4, 3] = CRUISER;

  

sea[5, 6] = SUBMARINE;

  

sea[8, 8] = ROWINGBOAT;

}

  

static void Main() {

clearsea();

placeships();

drawsea();

Console.ReadLine();

}

}  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote