I created a WPF app to play checkers. While lauching, a bunch of white and black buttons are created(15 each). The white buttons are called btnWhite and the black btnBlack.
I'd like that, if I play with the whites, then the next button to be clicked can't be a white one. So we have white play, next black, next white...
I really don't know how to manage this, because as I have coded, white can play as many times as they want, so as the black..
Would be really appreciated if you guys could help me !
Here's how I create the buttons :
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 4; j++)
{
if ((i + j) % 2 != 0)
{
Image img = new Image();
img.BeginInit();
img.Source = new BitmapImage(new Uri(@"./ressources/pionNoir.png", UriKind.Relative));
img.EndInit();
StackPanel stackPnl = new StackPanel();
stackPnl.Orientation = Orientation.Horizontal;
stackPnl.Margin = new Thickness(1);
stackPnl.Children.Add(img);
Button btnBlack = new Button();
btnBlack.Content = stackPnl;
btnBlack.Name = "Black";
btnBlack.Tag = "Black";
btnBlack.Click += Pawn_Click;
_grid.Children.Add(btnBlack);
Grid.SetRow(btnBlack, j);
Grid.SetColumn(btnBlack, i);
tab[i, j].initSide(false);
}
}
}
Pawn_Click :
public void Pawn_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
if (t)
{
selectedPawn = b;
t = false;
}
if (b.BorderBrush == Brushes.SteelBlue)
{
b.BorderThickness = new Thickness(1);
b.BorderBrush = Brushes.Black;
}
else
{
b.BorderBrush = Brushes.SteelBlue;
b.BorderThickness = new Thickness(3);
}
if (b != selectedPawn)
{
selectedPawn.BorderThickness = new Thickness(1);
selectedPawn.BorderBrush = Brushes.Black;
}
selectedPawn = b;
pawnPosition[0] = Grid.GetColumn(selectedPawn);
pawnPosition[1] = Grid.GetRow(selectedPawn);
isDraught = false;
if (selectedPawn.Tag.ToString() == "White")
{
whoPlays = true;
}
else
{
whoPlays = false;
}
if (selectedPawn.Name.Contains("Draught"))
{
b.BorderBrush = Brushes.Red;
b.BorderThickness = new Thickness(3);
isDraught = true;
}
@UPDATE I've tried this inside my Pawn_Click method :
if(b.Name=="Black")
{
selectedPawn.IsEnabled = true;
}
else if(b.Name=="White")
{
selectedPawn.IsEnabled = false; ;
}