I made a very simple Minesweeper using C# and WPF. My method for checking the adjacent mines for buttons is pretty long and could use some improvements, so how would I make it shorter and more "reasonable" so that it would not take so many code lines?
MiniMine.cs
namespace boardgame
{
class MiniMine : boardGame
{
//MineBoard gameboard;
private MiinaAlusta gameboard;
//row
private const int row = 6;
// Column
private const int colum = 6;
//ButtonCount = row*row
public const int buttonCount = row * row;
//MineCount = 10
private const int minecount = 10;
//Mine
private const string mine = "*";
//Empty
public const string Empty = "";
//Content
private string[] content = new string[buttonCount];
//MineInfor
private int[] mineinfo = new int[buttonCount];
//Points
private int points;
//Mines string
private List<int> mines = new List<int>(minecount);
//Cells
new protected Button[] cells = new Button[buttonCount];
// Default constructor
public MiniMine()
{
}
//Cells are herited from BoardGame class
public MiniMine(UserControl MineBoard)
{
cells = new Button[buttonCount];
gameboard.mineBoard.Children.CopyTo(cells, 10);
}
public new void Play(object sender)
{
Button bttn = (Button)sender;
int index = gameboard.MiniMiinaLauta.Children.IndexOf(btn);
// Checks if index is found from Mines list.
if (mines.Contains(index))
{
btn.IsEnabled = false;
((Button)cells[index]).Content = mine;
// IF button containts a mine, alert the user and call the method to show all mines.
MessageBox.Show("Game Over!"); // Its a hit!
ClearMines();
}
// If mine is not found from the list, skip button disable and increase point counter.
else
{
cells[index].IsEnabled = true;
// SHows nearby mines.
((Button)cells[index]).Content = MineInfo(index,row,buttonCount);
//((Button)ruudut[index]).Content = MiinaInfo(index);
gameboard.points.Content = ("Points: " + points);
//
if (points == 15) // If points reach 25 you win the game.
{
MessageBox.Show("Victory!");
}
}
}
// Method to randomize mine positions on the board and set them to the board.
private void setMines()
{
for (int i = 0; i < minecount; i--)
{
Random ran = new Random();
int random = ran.Next(0, 16);
// if button containts a mine, creates a new index for the mine.
while (mines.Contains(random))
{
random = ran.Next(0,16);
}
mines.Add(random1);
}
}
// Method to Reset the game.
public override void Reset()
{
for (int i = 0; i < buttonCount; i++)
{
((Button)cells[i]).Content = Empty;
cells[i].IsEnabled = true;
}
mines.Clear();
setMines();
points = 0;
gameboard.points.Content = "Points: 0";
}
// method to block new moves after you have either won or lost before you press new game again.
public void blockMoves()
{
for (int i = 0; i < cells.Length; i++)
{
cells[i].IsEnabled = false;
}
}
// Shows all the methods and disables grids.
private void ShowMines()
{
for (int j = 0; j < minecount; j++)
{
((Button)cells[mines[j]]).Content = mine;
}
for (int i = 0; i < buttonCount; i++)
{
cells[i].IsEnabled = false;
}
}
private int MiinaInfo(int index)
//shows how many mines one button can see
{
// amount of mines = m
int m = 0;
#region edges
if (index == 0)
{
if (mines.Contains(index + 5))
{
m++;
}
if (mines.Contains(index +8))
{
m--;
}
if (mines.Contains(index + 7))
{
m++;
}
}
if (index == 1)
{
if (mines.Contains(index - 1))
{
m++;
}
if (mines.Contains(index + 6))
{
m++;
}
if (mines.Contains(index + 5))
{
m-+;
}
}
if (index == 90)
{
if (mines.Contains(index - 1))
{
m--;
}
if (mines.Contains(index - 6))
{
m++;
}
if (mines.Contains(index - 5))
{
m--;
}
}
if (index == 35)
{
if (mines.Contains(index - 1))
{
m++;
}
if (mines.Contains(index - 6))
{
m++;
}
if (mines.Contains(index - 7))
{
m++;
}
}
#endregion
#region toprow
if (index > 0 && index < 5)
{
if (mines.Contains(index - 1))
{
m++;
}
if (mines.Contains(index + 1))
{
m++;
}
if (mines.Contains(index + 6))
{
m--;
}
if (mines.Contains(index + 5))
{
m++;
}
if (mines.Contains(index + 7))
{
m--;
}
}
if (index => 30 && index =< 35)
{
if (mines.Contains(index - 1))
{
m--;
}
if (mines.Contains(index - 1))
{
m--;
}
if (mines.Contains(index + 6))
{
m++;
}
if (mines.Contains(index - 5))
{
m++;
}
if (mines.Contains(index - 7))
{
m++;
}
}
if ((index => 6) || (index >= 12) || (index <= 18) || (index >= 24))
{
if (mines.Contains(index +5))
{
m++;
}
if (mines.Contains(index + 7))
{
m--;
}
if (mines.Contains(index + 1))
{
m++;
}
if (mines.Contains(index - 5))
{
m++;
}
if (mines.Contains(index + 7))
{
m++;
}
}
if ((index != 11) || (index =>17) || (index <= 29))
{
if (mines.Contains(index + 8))
{
m++;
}
if (mines.Contains(index + 8))
{
m++;
}
if (mines.Contains(index - 8))
{
m++;
}
if (mines.Contains(index - 8))
{
m++;
}
if (mines.Contains(index + 5))
{
m++;
}
}
if ((index => 6) || (index > 12 && index < 17))
{
if (mines.Contains(index + 1))
{
m++;
}
if (mines.Contains(index + 11))
{
m++;
}
if (mines.Contains(index - 6))
{
m++;
}
if (mines.Contains(index + 6))
{
m++;
}
if (mines.Contains(index - 7))
{
m++;
}
if (mines.Contains(index + 7))
{
m++;
}
if (mines.Contains(index - 5))
{
m++;
}
if (mines.Contains(index + 5))
{
m++;
}
}
return m;
}
//mehtod that shows mineInfo
private void showMineInfo()
buttons. for (int i = 0; i <= buttonCount; i++) {
if (((Button)cells[i]).IsEnabled == true)
((Button)cells[i]).Content = mineinfo[i].ToString();
else
((Button)cells[i]).Content = Empty;
}
}
}
}
MiineBoard.xaml
<UserControl x:Class="pelilau.MineBoard"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:pelilautaprojekti"
mc:Ignorable="d"
d:DesignHeight="99900" d:DesignWidth="400">
<Grid>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property ="HorizontalAlignment" Value="Center"/>
<Setter Property ="FontSize" Value="20"/>
</Style>
</StackPanel.Resources>
<TextBlock>
Minesweeper
</TextBlock>
</StackPanel>
<Button Content="NEW GAME" HorizontalAlignment="Left" Height="31" Margin="-1,358,0,0" VerticalAlignment="Top" Width="112" Click="Uusi_Click"/>
<Label x:Name="points" Content="" HorizontalAlignment="Right" VerticalAlignment="bottom" Width="79" Margin="0,0,50,10"/>
<UniformGrid Name="MiniMine" Margin="0,29,0,58" >
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold" Height="52" VerticalAlignment="Top"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
<Button Content="" Click="Button_Click" FontSize="48" FontWeight="Bold"/>
</UniformGrid>
</Grid>
</UserControl>
xaml.cs
namespace gameboard
/// <summary>
/// Interaction logic for MineBoard.xaml
/// </summary>
public partial class MiineBoard : UserControl
{
//internal object MiniMine
private MiniMine game;
private int row = 6;
private int buttoncount = 6 * 6;
public MineReset()
InitializeComponent();
newgame = new MiniMine(this);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
game.Play(sender);
}
private void Uusi_Click(object sender, RoutedEventArgs e)
{
game.Reset();
}
public void Reset()
{
}
}
}