【发布时间】:2022-10-24 20:18:51
【问题描述】:
您好 StackOverflow 我正在尝试编写一个简单的 WPF C# 拼字游戏。我自己能做的:我创建了一个 10x10 的表格,并在其中随机放入了字母
这是 XAML 和 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>
和随机化器
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));
}
}
}
}
下一步,我不能做的是创建一个单词集合并将它们放在一个表中。我知道我们需要创建一个单词表; List<string[]>words = new List<string[]>();然后将每个单词拆分为字母,但是如何在表格中垂直或水平排列字母?
我是初学者,如果有解决方案应该不会很复杂
编辑:我想在一行中至少放一个词,但由于某种原因,它给出了一个错误,我不明白为什么
Random rchar = new Random();
var randomRowIndex = rchar.Next(0, 9);// Random Row Index
for (int i = 0; i < searchWord.Length; i++)
{
randomRowIndex = searchWord[i];
}
我有时会得到一个大于 10 的随机索引并且这个词仍然没有插入到表中
【问题讨论】: