❔ Wpf chess board problem

Hello! Im trying to color a chess board. I have a bunch of border tags for the squares. I think it can be inferred from the screenshots what im trying to do. Yes i know this ain't the best way of writing this code but my main focus is to have something working, then i can refactor later as needed. I'm stuck trying to color the odd numbered squares. I dont know how to write the syntax for coloring the odd numbered squares. I've been googling for the past hours trying to figure it out, but alas i couldn't figure it out 😕 I would really appreciate some help should anyone be so kind as to extend this newb a hand. Thank you very much!
2 Replies
Alexicon
Alexicon2y ago
Take a look at this example and let me know if you need any clarifications:
for (int index = 0; index < BorderGrid.Children.Count; index++)
{
if (BorderGrid.Children[index] is Border border)
{
Color color;

if (index % 2 == 0)
{
//odd color
color = Colors.Black;
}
else
{
//even color
color = Colors.Red;
}

border.Background = new SolidColorBrush(color);
}
}
for (int index = 0; index < BorderGrid.Children.Count; index++)
{
if (BorderGrid.Children[index] is Border border)
{
Color color;

if (index % 2 == 0)
{
//odd color
color = Colors.Black;
}
else
{
//even color
color = Colors.Red;
}

border.Background = new SolidColorBrush(color);
}
}
You can find a working example here: https://github.com/AlexLexicon/Discord.Example.Checkerboard
GitHub
GitHub - AlexLexicon/Discord.Example.Checkerboard
Contribute to AlexLexicon/Discord.Example.Checkerboard development by creating an account on GitHub.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.