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]++;
}
}
}
}
1 Answer 1
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).
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 valuex
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; }
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);
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.
BoardPeices
is that supposed to beBoardPieces
? At least you've been consistent, but I'm not familiar with that spelling. \$\endgroup\$wanted to know how to improve [a simple minesweeper game/board]
define good*/*better, and let in those you ask for ideas. \$\endgroup\$