Search code examples
c#minesweeper

C# char returns emoji or nothing


I'm trying to learn c# and trying to make simple minesweeper game on console. When i am returning int to char it returns '' or an emoji when it should return me 0-3. I know that my code is not clean i am still learning just the basics.

public char howManyMinesHaveBeenPut(int xCoordinate, int yCoordinate)
        {
            short howMany = 0;

            if(canPlayerPut(xCoordinate - 1, yCoordinate - 1) && isMine(xCoordinate - 1, yCoordinate - 1))
            {
                howMany++;
                Console.WriteLine("Mina 1");
            }

            if (canPlayerPut(xCoordinate, yCoordinate - 1) && isMine(xCoordinate, yCoordinate - 1))
            {
                howMany++;
                Console.WriteLine("Mina 2");

            }

            if (canPlayerPut(xCoordinate + 1, yCoordinate - 1) && isMine(xCoordinate + 1, yCoordinate - 1))
            {
                howMany++;
                Console.WriteLine("Mina 3");

            }

            if (canPlayerPut(xCoordinate - 1, yCoordinate) && isMine(xCoordinate - 1, yCoordinate))
            {
                howMany++;
                Console.WriteLine("Mina 4");

            }

            if (canPlayerPut(xCoordinate + 1, yCoordinate) && isMine(xCoordinate + 1, yCoordinate))
            {
                howMany++;
                Console.WriteLine("Mina 5");

            }

            if (canPlayerPut(xCoordinate - 1, yCoordinate + 1) && isMine(xCoordinate - 1, yCoordinate + 1))
            {
                howMany++;
                Console.WriteLine("Mina 6");

            }

            if (canPlayerPut(xCoordinate, yCoordinate + 1) && isMine(xCoordinate, yCoordinate + 1))
            {
                howMany++;
                Console.WriteLine("Mina 7");

            }
            if (canPlayerPut(xCoordinate + 1, yCoordinate + 1) && isMine(xCoordinate + 1, yCoordinate + 1))
            {
                howMany++;
                Console.WriteLine("Mina 8");

            }

            return Convert.ToChar(howMany);
        }

I've tried (char), convert to char, changing from int to short.

here is the link for full code: https://github.com/Thuthutka/minesweeper/blob/main/Program.cs


Solution

  • The numerical values that represent the Arabic digits are 48 ('0') to 57 ('9'). You're using the values from 0 to 3 which represent control characters. You need add '0' or 48 to your values to get the correct character returned.

    return Convert.ToChar(howMany + '0');
    

    You might want to try this implementation:

    public char howManyMinesHaveBeenPut(int xCoordinate, int yCoordinate)
    {
        int howMany = 0;
        int mine = 0;
    
        for (int x = -1; x <= 1; x++)
            for (int y = -1; y <= 1; y++)
            {
                mine++;
                if (x != 0 && y != 0)
                {
                    if (canPlayerPut(xCoordinate + x, yCoordinate + y) && isMine(xCoordinate + x, yCoordinate + y))
                    {
                        howMany++;
                        Console.WriteLine($"Mina {mine}");
                    }
                }
            }
        
        return Convert.ToChar(howMany + '0'); 
    }
    

    Also, please considering using the standard C# naming convention for methods - Pascal, not camal, case.