C#-Codeusing System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfChess960
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
static readonly Random rnd = new Random();
static readonly SolidColorBrush blackBrush = new SolidColorBrush(Color.FromRgb(0x95, 0x6A, 0x45));
static readonly SolidColorBrush whiteBrush = new SolidColorBrush(Color.FromRgb(0xE1, 0xE0, 0xDE));
private List<ChessGrid> grids;
public MainWindow()
{
InitializeComponent();
grids = new List<ChessGrid>();
InitChessBoard();
}
private void InitChessBoard()
{
gridChessBoard.Children.Clear();
grids.Clear();
const char A = 'A';
for (int i = 0; i < 8; i++)
{
char c = (char)(A + i);
int n = i + 1;
TextBlock tbkC = new TextBlock() { Text = c.ToString(), Foreground = Brushes.White, TextAlignment = TextAlignment.Center, Width = 40, Height = 20 };
TextBlock tbkN = new TextBlock() { Text = n.ToString(), Foreground = Brushes.White, TextAlignment = TextAlignment.Center, Width = 20, Margin = new Thickness(0, 11, 0, 0) };
Grid.SetColumn(tbkC, n);
Grid.SetRow(tbkC, 0);
gridChessBoard.Children.Add(tbkC);
Grid.SetColumn(tbkN, 0);
Grid.SetRow(tbkN, n);
gridChessBoard.Children.Add(tbkN);
}
bool white = true;
for (int r = 1; r <= 8; r++)
{
for (int c = 1; c <= 8; c++)
{
ChessGrid grid = new ChessGrid();
grid.Name = $"grid_{r}_{c}";
grid.White = white;
grid.Background = white ? whiteBrush : blackBrush;
white = !white;
Grid.SetRow(grid, r);
Grid.SetColumn(grid, c);
gridChessBoard.Children.Add(grid);
grids.Add(grid);
}
white = !white;
}
}
private void GenerateRandom()//K = König, D = Dame, L = Läufer, S = Springer, T = Turm, B = Bauer
{
InitChessBoard();
#region König
int rowK = rnd.Next(1, 9);
int columnK = rnd.Next(2, 8);
int rowKMirror = 9 - rowK;
ChessGrid gridK = grids.Single(a => a.Name == $"grid_{rowK}_{columnK}");
TextBlock tbkK = new TextBlock() { Text = "K", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.White };
gridK.Children.Add(tbkK);
ChessGrid gridKMirror = grids.Single(a => a.Name == $"grid_{rowKMirror}_{columnK}");
TextBlock tbkKMirror = new TextBlock() { Text = "K", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.Black };
gridKMirror.Children.Add(tbkKMirror);
#endregion
#region Türme
int rowT = rowK;
int columnTL = rnd.Next(1, columnK);
int columnTR = rnd.Next(columnK + 1, 9);
int rowTMirror = rowKMirror;
//Linker Turm
ChessGrid gridTL = grids.Single(a => a.Name == $"grid_{rowT}_{columnTL}");
TextBlock tbkTL = new TextBlock() { Text = "T", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.White };
gridTL.Children.Add(tbkTL);
ChessGrid gridTLMirror = grids.Single(a => a.Name == $"grid_{rowTMirror}_{columnTL}");
TextBlock tbkTLMirror = new TextBlock() { Text = "T", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.Black };
gridTLMirror.Children.Add(tbkTLMirror);
//Rechter Turm
ChessGrid gridTR = grids.Single(a => a.Name == $"grid_{rowT}_{columnTR}");
TextBlock tbkTR = new TextBlock() { Text = "T", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.White };
gridTR.Children.Add(tbkTR);
ChessGrid gridTRMirror = grids.Single(a => a.Name == $"grid_{rowTMirror}_{columnTR}");
TextBlock tbkTRMirror = new TextBlock() { Text = "T", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.Black };
gridTRMirror.Children.Add(tbkTRMirror);
#endregion
#region Dame
ChessGrid gridD = null;
do
{
int rowD = rnd.Next(1, 9);
int columnD = rnd.Next(1, 9);
int rowDMirror = 9 - rowD;
gridD = grids.SingleOrDefault(a => a.Name == $"grid_{rowD}_{columnD}" && a.Children.Count == 0);
if (gridD != null)
{
TextBlock tbkD = new TextBlock() { Text = "D", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.White };
gridD.Children.Add(tbkD);
ChessGrid gridDMirror = grids.Single(a => a.Name == $"grid_{rowDMirror}_{columnD}");
TextBlock tbkDMirror = new TextBlock() { Text = "D", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.Black };
gridDMirror.Children.Add(tbkDMirror);
}
} while (gridD == null);
#endregion
#region Läufer
ChessGrid gridLL = null;
ChessGrid gridLR = null;
//Linker Läufer
do
{
int rowLL = rnd.Next(1, 9);
int columnLL = rnd.Next(1, 9);
int rowLLMirror = 9 - rowLL;
gridLL = grids.SingleOrDefault(a => a.Name == $"grid_{rowLL}_{columnLL}" && a.Children.Count == 0);
if (gridLL != null)
{
TextBlock tbkLL = new TextBlock() { Text = "L", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.White };
gridLL.Children.Add(tbkLL);
ChessGrid gridLLMirror = grids.Single(a => a.Name == $"grid_{rowLLMirror}_{columnLL}");
TextBlock tbkLLMirror = new TextBlock() { Text = "L", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.Black };
gridLLMirror.Children.Add(tbkLLMirror);
}
} while (gridLL == null);
//Rechter Läufer
do
{
int rowLR = rnd.Next(1, 9);
int columnLR = rnd.Next(1, 9);
int rowLRMirror = 9 - rowLR;
gridLR = grids.SingleOrDefault(a => a.Name == $"grid_{rowLR}_{columnLR}" && a.Children.Count == 0 && a.White != gridLL.White);
if (gridLR != null)
{
TextBlock tbkLR = new TextBlock() { Text = "L", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.White };
gridLR.Children.Add(tbkLR);
ChessGrid gridLRMirror = grids.Single(a => a.Name == $"grid_{rowLRMirror}_{columnLR}");
TextBlock tbkLRMirror = new TextBlock() { Text = "L", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.Black };
gridLRMirror.Children.Add(tbkLRMirror);
}
} while (gridLR == null);
#endregion
#region Springer
ChessGrid gridSL = null;
ChessGrid gridSR = null;
//Linker Springer
do
{
int rowSL = rnd.Next(1, 9);
int columnSL = rnd.Next(1, 9);
int rowSLMirror = 9 - rowSL;
gridSL = grids.SingleOrDefault(a => a.Name == $"grid_{rowSL}_{columnSL}" && a.Children.Count == 0);
if (gridSL != null)
{
TextBlock tbkSL = new TextBlock() { Text = "S", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.White };
gridSL.Children.Add(tbkSL);
ChessGrid gridSLMirror = grids.Single(a => a.Name == $"grid_{rowSLMirror}_{columnSL}");
TextBlock tbkSLMirror = new TextBlock() { Text = "S", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.Black };
gridSLMirror.Children.Add(tbkSLMirror);
}
} while (gridSL == null);
//Rechter Springer
do
{
int rowSR = rnd.Next(1, 9);
int columnSR = rnd.Next(1, 9);
int rowSRMirror = 9 - rowSR;
gridSR = grids.SingleOrDefault(a => a.Name == $"grid_{rowSR}_{columnSR}" && a.Children.Count == 0);
if (gridSR != null)
{
TextBlock tbkSR = new TextBlock() { Text = "S", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.White };
gridSR.Children.Add(tbkSR);
ChessGrid gridSRMirror = grids.Single(a => a.Name == $"grid_{rowSRMirror}_{columnSR}");
TextBlock tbkSRMirror = new TextBlock() { Text = "S", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.Black };
gridSRMirror.Children.Add(tbkSRMirror);
}
} while (gridSR == null);
#endregion
#region Bauern
for (int i = 0; i < 8; i++)
{
ChessGrid gridB = null;
do
{
int rowB = rnd.Next(1, 9);
int columnB = rnd.Next(1, 9);
int rowBMirror = 9 - rowB;
gridB = grids.SingleOrDefault(a => a.Name == $"grid_{rowB}_{columnB}" && a.Children.Count == 0);
if (gridB != null)
{
TextBlock tbkB = new TextBlock() { Text = "B", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.White };
gridB.Children.Add(tbkB);
ChessGrid gridBMirror = grids.Single(a => a.Name == $"grid_{rowBMirror}_{columnB}");
TextBlock tbkBMirror = new TextBlock() { Text = "B", TextAlignment = TextAlignment.Center, Width = 40, Margin = new Thickness(0, 4, 0, 0), FontWeight = FontWeights.Bold, FontSize = 24, Foreground = Brushes.Black };
gridBMirror.Children.Add(tbkBMirror);
}
} while (gridB == null);
}
#endregion
}
private void buttonReset_Click(object sender, RoutedEventArgs e)
{
InitChessBoard();
}
private void buttonRandom_Click(object sender, RoutedEventArgs e)
{
GenerateRandom();
}
}
class ChessGrid : Grid
{
public bool White { get; set; }
}
}