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

Mast
13.8k12 gold badges56 silver badges127 bronze badges
asked Mar 24, 2021 at 13:20
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Ok, i will edit it \$\endgroup\$ Commented Mar 24, 2021 at 14:48
  • 4
    \$\begingroup\$ Please add the full code, and not via a URL which can deprecate. \$\endgroup\$ Commented Mar 24, 2021 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\$ Commented Mar 24, 2021 at 18:19
  • \$\begingroup\$ Sorry, I suck at english. \$\endgroup\$ Commented Mar 24, 2021 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\$ Commented Mar 25, 2021 at 5:51

1 Answer 1

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.

answered Mar 24, 2021 at 20:59
\$\endgroup\$
2
  • \$\begingroup\$ Why not Math.Clamp? \$\endgroup\$ Commented Mar 25, 2021 at 0:04
  • 1
    \$\begingroup\$ @aepot Indeed. It appeared in .NET Standard already. I missed that. \$\endgroup\$ Commented Mar 25, 2021 at 12:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.