-3

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; ;
        }
David
  • 221
  • 3
  • 13
  • 2
    Can you provide any more detail about what the problem is? – Ian Sep 05 '18 at 13:54
  • 1
    Broadly I'd say either you want to change the state of each button to reflect that they're not active, or to check the game state in the click handler before acting on the click for that piece. – Rup Sep 05 '18 at 13:54
  • 1
    Check the active player in `Pawn_Click()` and either proceed with the move or do nothing. – Dan Wilson Sep 05 '18 at 13:55
  • I've updated the question with the pawn_click method, but I can't find a way to detect if its either white or black who's playing.. – David Sep 05 '18 at 13:56
  • @Ian, the problem is that i can play with the white as many times as I want, and the rules in checkers is that each player plays once at a time, not as many times as you want... I'd like to disable the click on the black after I played with a black, same for the white.. – David Sep 05 '18 at 13:57
  • 1
    Create a global variable to store the active player and change it when a move is made. – Dan Wilson Sep 05 '18 at 13:58
  • I've tried something but its still not working, can you guys check it ? I've updated my code with what I've tried ! – David Sep 05 '18 at 14:39
  • If the idea there is to disable all of the current colour's pawns and enable the opposite colours then you'll need to do a bit more work than that: you'd have to get the list of all buttons from the grid and compare each one's colour against the activated one's colour. I think Dan's idea is better: have some global state that stores whose turn it is (and why not display that on the UI too? "White to play") and at the start of the click handler check whether this is a pawn for the current player and just return if not. – Rup Sep 05 '18 at 15:08
  • Yes, I've tried as Dan told me and I did it, i'll post the answer ! – David Sep 05 '18 at 16:25

1 Answers1

1

So, I created a new method to get all the buttons grom the grid, as @Rup said and retrieved the tag of the buttons, so I could disable all buttons from one color and let the other color button's enabled :

 public void desactivate_Btn()
    {
        UIElement b;
        for (int i = 0; i < theGrid.Children.Count; i++)
        {
            b = theGrid.Children[i];
            if (whoPlays)
            {
                if (((Button)b).Tag.ToString() == "White")
                {

                    b.IsEnabled = false; // to disable all the buttons
                   selectedPawn = null; // to disable the selected button
                   t = true; // i had to put 't' true as this variable needs to be true at the beginning of my code


                }
                else if (((Button)b).Tag.ToString() == "Black")
                {
                    b.IsEnabled = true;
                }
            }
            else
            {
                if (((Button)b).Tag.ToString() == "Black")
                {
                    b.IsEnabled = false;
                   selectedPawn = null;
                    t = true;
                }
                else if (((Button)b).Tag.ToString() == "White")
                {
                    b.IsEnabled = true;

                }
            }
        }
    }

Thank you for your help, really appreciated !

David
  • 221
  • 3
  • 13