[英]Checkers c# each player plays once not possible
I created a WPF app to play checkers.我创建了一个 WPF 应用程序来玩跳棋。 While lauching, a bunch of white and black buttons are created(15 each).启动时,会创建一堆白色和黑色按钮(每个按钮 15 个)。 The white buttons are called btnWhite and the black btnBlack.白色按钮称为 btnWhite 和黑色 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 : 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 : @UPDATE 我在我的 Pawn_Click 方法中试过这个:
if(b.Name=="Black")
{
selectedPawn.IsEnabled = true;
}
else if(b.Name=="White")
{
selectedPawn.IsEnabled = false; ;
}
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 :因此,我创建了一个新方法来获取网格中的所有按钮,正如@Rup 所说并检索按钮的标签,因此我可以禁用一种颜色的所有按钮并启用另一种颜色的按钮:
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 !谢谢你的帮助,真的很感激!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.