I'm trying to write a simple WPF C# scrabble game. What I was able to do on my own: I created a 10x10 table and randomly put letters in it
Here is the code of XAML and Randomizer
<ItemsControl ItemsSource="{Binding Chars}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button
Width="30"
Height="30"
Margin="3"
Content="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And randomizer
public partial class MainWindow : Window
{
public ObservableCollection<ObservableCollection<char>> Chars { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
Random rchar = new Random();
Chars = new();
for (int x = 0; x < 10; x++)
{
Chars.Add(new());
for (int y = 0; y < 10; y ++)
{
Chars[x].Add((char)rchar.Next(65, 91));
}
}
}
}
The next step, which I could not do, is to create a collection of words and place them in a table. I understand that we need to create a Word List; List<string[]>words = new List<string[]>();
and then split each word into letters but then how do I arrange the letters vertically or horizontally in the table?
I am a beginner and if there is a solution it should not be very complicated
string word = "House";
char[,] arr = new char[10, 10];
for (int x = 0; x < arr.GetLength(0); x++)
{
for (int y = 0; y < arr.GetLength(1); y++)
{
if(y < word.Length)
{
arr[x,y] = word[y];
} else
{
arr[x, y] = (char)(rchar.Next(65, 91));
}
Console.Write(arr[x, y] + " ");
}
Console.WriteLine();
}
How to fix that? The word needs to be displayed once and anywhere