2
\$\begingroup\$

I created a simple minesweeper game in c#, and wanted to know how to improve it. I didn't really understand how to make it so everything besides the zero opens up.

How would I make my class Board better?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minesweeper_trial
{
    class Board
    {
        public int[,] BoardPeices = new int[5, 5];
        //--------------------------------------------------------------------------------------
        public void AddMine(Mine mine1)
        {
            BoardPeices[mine1.PositionX, mine1.PositionY] = 9;
        }
        //--------------------------------------------------------------------------------------
        public void PrintBoard()
        {
            for (int y = 0; y < 5; y++)
            {
                for (int x = 0; x < 5; x++)
                {
                    Console.Write(BoardPeices[x,y]);
                }
                Console.Write(Environment.NewLine);
            }
        }
        //--------------------------------------------------------------------------------------
        public void SetBoard()
        {
            for (int y = 0; y < 5; y++)
            {
                for (int x = 0; x < 5; x++)
                {
                    BoardPeices[x, y] = 0;
                }
            }
        }
        //--------------------------------------------------------------------------------------
        public void NumberBesideMine(Mine mine1)
        {
            if (mine1.PositionX == 4 && mine1.PositionY == 4)
            {
                BoardPeices[mine1.PositionX - 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX, mine1.PositionY - 1]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY - 1]++;
            }
            else if (mine1.PositionX == 0 && mine1.PositionY == 0)
            {
                BoardPeices[mine1.PositionX + 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX, mine1.PositionY + 1]++;
                BoardPeices[mine1.PositionX + 1, mine1.PositionY + 1]++;
            }
            else if (mine1.PositionX == 0 && mine1.PositionY == 4)
            {
                BoardPeices[mine1.PositionX + 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX, mine1.PositionY - 1]++;
                BoardPeices[mine1.PositionX + 1, mine1.PositionY - 1]++;
            }
            else if (mine1.PositionX == 4 && mine1.PositionY == 0)
            {
                BoardPeices[mine1.PositionX - 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX, mine1.PositionY + 1]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY + 1]++;
            }
            else  if (mine1.PositionY == 4)
            {
                BoardPeices[mine1.PositionX + 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX, mine1.PositionY - 1]++;
                BoardPeices[mine1.PositionX + 1, mine1.PositionY - 1]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY - 1]++;
            }
            else if (mine1.PositionX == 4)
            {
                BoardPeices[mine1.PositionX - 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX, mine1.PositionY + 1]++;
                BoardPeices[mine1.PositionX, mine1.PositionY - 1]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY + 1]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY - 1]++;
            }
            else if (mine1.PositionY == 0)
            {
                BoardPeices[mine1.PositionX + 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX, mine1.PositionY + 1]++;
                BoardPeices[mine1.PositionX + 1, mine1.PositionY + 1]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY + 1]++;
            }
            else if (mine1.PositionX == 0)
            {
                BoardPeices[mine1.PositionX + 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX, mine1.PositionY + 1]++;
                BoardPeices[mine1.PositionX, mine1.PositionY - 1]++;
                BoardPeices[mine1.PositionX + 1, mine1.PositionY - 1]++;
                BoardPeices[mine1.PositionX + 1, mine1.PositionY + 1]++;
            }
            else
            {
                BoardPeices[mine1.PositionX + 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY]++;
                BoardPeices[mine1.PositionX, mine1.PositionY + 1]++;
                BoardPeices[mine1.PositionX, mine1.PositionY - 1]++;
                BoardPeices[mine1.PositionX + 1, mine1.PositionY - 1]++;
                BoardPeices[mine1.PositionX + 1, mine1.PositionY + 1]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY + 1]++;
                BoardPeices[mine1.PositionX - 1, mine1.PositionY - 1]++;
            }
        }
    }
}

Link to full code

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Ok, i will edit it \$\endgroup\$ – PARTH Mar 24 at 14:48
  • 4
    \$\begingroup\$ Please add the full code, and not via a URL which can deprecate. \$\endgroup\$ – Flater Mar 24 at 16:22
  • 1
    \$\begingroup\$ BoardPeices is that supposed to be BoardPieces? At least you've been consistent, but I'm not familiar with that spelling. \$\endgroup\$ – Mast Mar 24 at 18:19
  • \$\begingroup\$ Sorry, I suck at english. \$\endgroup\$ – PARTH Mar 24 at 21:08
  • \$\begingroup\$ wanted to know how to improve [a simple minesweeper game/board] define good*/*better, and let in those you ask for ideas. \$\endgroup\$ – greybeard Mar 25 at 5:51
2
\$\begingroup\$

As far I understand you need to intersect your 5x5 board with a 3x3 square with a center in mine1.
It could be achieved via 2D loop (2 nested loops).

  1. First (optional), if you use an old version of .NET that don't have the Math.Clamp method, let's create a simple method to clamp a value x into the range [min, max].

    private static T Clamp<T>(T x, T min, T max)
        where T : IComparable<T>
    {
        return x.CompareTo(min) < 0 ? min : x.CompareTo(max) > 0 ? max : x;
    }
    
  2. Next, let's find a boundaries.

    int maxX = BoardPeices.GetLength(0) - 1; // Should be 4
    int maxY = BoardPeices.GetLength(1) - 1; // Should be 4
    
    int left = Clamp(mine1.PositionX - 1, 0, maxX);
    int right = Clamp(mine1.PositionX + 1, 0, maxX);
    int top = Clamp(mine1.PositionY - 1, 0, maxY);
    int bottom = Clamp(mine1.PositionY - 1, 0, maxY);
    
  3. Finally, let's iterate.

    for (j = top; <= bottom; ++j)
    {
        for (i = left; <= right; ++i)
        {
            ++BoardPeices[i, j];
        }
    }
    --BoardPeices[mine1.PositionX, mine1.PositionY]; // undo the mine1 cell increment
    

Thus, items 2 and 3 together replace your NumberBesideMine method.

\$\endgroup\$
2
  • \$\begingroup\$ Why not Math.Clamp? \$\endgroup\$ – aepot Mar 25 at 0:04
  • 1
    \$\begingroup\$ @aepot Indeed. It appeared in .NET Standard already. I missed that. \$\endgroup\$ – Dmitry Mar 25 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.