Home > other >  c# Battleships minigame
c# Battleships minigame

Time:07-14

I am a begginer. I am stuck upon a problem about namimng the colums and the lines, differently than 0 to 10. something like this picture, the lines starting from the bottom 1 to 10 and colums from A to J.

Battleships kind of board

I've created a program that tells me if the ship is destroyed or not, after using some moves( in this image using move 5 c, in my program 5 2, counts as a hit, if all the blue squares are hit the program returns destroyed).

    char[,] board = new char[10, 10];
    for (int i = 0; i < 10; i  )
    {   
        string line = Console.ReadLine();
        string[] boardCharacters = line.Split(' ');
        for (int j = 0 ; j < 10; j  )
            board[i, j] = Convert.ToChar(boardCharacters[j]);
    }

    int countHits = 0;
    for (int z = 0; z < 14; z  )
    {
        var attackCoordinates = Console.ReadLine().Split(' ');
        int[] attack = new int[2];
        for (int i = 0; i < 2; i  )
        {
            attack[i] = int.Parse(attackCoordinates[i]);
        }

        if (board[attack[0], attack[1]] == 'x')
        {
            countHits  ;
        }

    }

    if (countHits == 8)
    {
        Console.WriteLine("destroyed");
    }

    else
    {
        Console.WriteLine("not destroyed");
    }

What should I change and how should I change it?

CodePudding user response:

Naming your rows and columns is completely up to you and does not have anything to do with the indexes in the array. So when you print your board (something you don't show here) you can actually print anything you want to name your rows and columns.

The real problem (at least with the code you show here) is when reading your coordinates for attack. When you do

var attackCoordinates = Console.ReadLine().Split(' ');
int[] attack = new int[2];
for (int i = 0; i < 2; i  )
{
    attack[i] = int.Parse(attackCoordinates[i]);
}

you actually expect two integers, so if the user inputs anything else, it throws an exception. Furthermore, as you are directly using those inputs to access your array

if (board[attack[0], attack[1]] == 'x')
{
    countHits  ;
}

you actually expect the user to input 0-based coordinates, which most users with no programming background will find very counter-intuitive.

For supporting something like 5 c you can do something like the following

var ac = Console.ReadLine().Split(' ');
var c1 = int.Parse(ac[0]) - 1;
var c2 = ac[1].ToLower()[0] - 'a';

For c1 you have to substract 1 from the input value, because your grid is named 1 .. 10, but the indexes in the array are 0 .. 9. For c2 you can use the char type and calculate the offset between the entered character and the character a. To support uppercase and lowercase letters, convert the input to lowercase first.

Of course, this is just the basics without any error handling. You have still to care for that yourself. For instance if the user inputs some invalid coordinates which cannot be parsed to an integer or are not letters of the alphabet, not enough coordinates, coordinates go out of bounds with your array, uses more than one space to separate the coordinates, ... All those inputs will still raise exceptions now and you need to sanitize your input first, before going on.

Of course there is still much more missing, but this answer is only addressing the issue you asked about. For instance you are always playing excactly 14 turns, even if hitcount is already reached earlier. You are not preventing entering the same coordinates twice, entering the board doesn't check that there are exactly 8 targets, ...

  • Related