I'm trying to program a simple game in C# and WPF. The buttons are working fine, the game is almost ready. However, I have to ensure, that the button are sorted randomly at the program start. And I do not really know how to achieve it.
The command Shuffle is obviously false :((
Thank you for any kind of help. Regards, Paul.
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
int gedrückterKnopf = 0;
Button[] knöpfe = {button01, button02, button03, button04, button05, button06, button07, button08, button09, button10, button11, button12, button13, button14, button15, button16 };
Shuffle(knöpfe);
for (int i = 0; i < 16; i++)
{
if (knöpfe[i] == sender)
{
gedrückterKnopf = i;
break;
}
}
int leerstelle = 0;
for (int i = 0; i<16; i++)
{
if (!knöpfe[i].IsEnabled)
{
leerstelle = i;
}
}
//MessageBox.Show(gedrückterKnopf.ToString() + " " + leerstelle.ToString());
int zeile1 = Grid.GetRow(knöpfe[gedrückterKnopf]);
int spalte1 = Grid.GetColumn(knöpfe[gedrückterKnopf]);
int zeile2 = Grid.GetRow(knöpfe[leerstelle]);
int spalte2 = Grid.GetColumn(knöpfe[leerstelle]);
if ( (zeile1 == zeile2 - 1 || zeile1 == zeile2 + 1) && spalte1 == spalte2
||
(spalte1 == spalte2 - 1 || spalte1 == spalte2 + 1) && zeile1 == zeile2
)
{
Grid.SetRow(knöpfe[gedrückterKnopf], zeile2);
Grid.SetColumn(knöpfe[gedrückterKnopf], spalte2);
Grid.SetRow(knöpfe[leerstelle], zeile1);
Grid.SetColumn(knöpfe[leerstelle], spalte1);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire