【问题标题】:How to add a word of letters to the table for a scrabble game如何在拼字游戏的桌子上添加一个字母单词
【发布时间】: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&lt;string[]&gt;words = new List&lt;string[]&gt;();然后将每个单词拆分为字母,但是如何在表格中垂直或水平排列字母?

我是初学者,如果有解决方案应该不会很复杂

编辑:我想在一行中至少放一个词,但由于某种原因,它给出了一个错误,我不明白为什么

     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 的随机索引并且这个词仍然没有插入到表中

【问题讨论】:

    标签: c# wpf scrabble


    【解决方案1】:

    脚步:

    1. 决定方向 - 水平与垂直
    2. 决定单词应该从哪里开始 - 作为坐标 0..9(不要忘记检查单词是否适合)
    3. 用单词字符替换生成的字符

      示例功能代码:

      public enum WordDirection
      {
          Vertical,
          Horizontal
      }
      
      public void WriteWord(ObservableCollection<ObservableCollection<char>> charTable, 
          IEnumerable<char> word, int startX, int startY, WordDirection direction)
      {
          int x = startX;
          int y = startY;
          foreach(char ch in word)
          {
              charTable[x][y] = ch;
              if(direction == WordDirection.Horizontal)
              {
                  x++;
              }
              else //these two maybe need to be swapped as I'm not sure which is which (as I'm writing this without testing)
              {
                  y++;
              }
          }
      }
      

      示例调用可能如下所示:

      WriteWord(Chars, "test".AsEnumerable(), 0, 0, WordDirection.Vertical);
      

      笔记:

      • 如果单词不合适,这段代码很可能会抛出异常
      • 以后写的字会覆盖以前的,除非你实施额外的检查

    【讨论】:

      【解决方案2】:

      就像 Marty 的回答一样,但这也会检查您的文字是否有空间。抱歉,如果太复杂,这是我的第一个 StackOverflow 答案。

      它将: 1. 创建一个用 char '33' 填充的 2d 字符数组。 2.遍历单词,检查是否有空格,如果有空格则提交单词。 3. 用随机字符填充其余字符。

      public ObservableCollection<ObservableCollection<char>> Chars { get; set; }
      
      public List<string> Words { get; set; }
      
      public enum WordDirection
      {
          Vertical,
          Horizontal
      }
      
      Random rnd = new Random();
      
      public void Main(){
          FillWithEmptyChars();
          WriteWords(Words);
          FillRestOfSpace();
      }
      
      public void FillWithEmptyChars(){
           Chars = new();
           for (int x = 0; x < 10; x++)
           {
               Chars.Add(new());
               for (int y = 0;  y < 10; y ++)
               {
                   Chars[x].Add((char)33); //Couldnt find a better char, chosen for ease
               }
           }
      }
      
      public void WriteWords(List<string> words){
          
          foreach(string word in words){
              var startx = rnd.Next(0, 9);
              var starty =  rnd.Next(0, 9);
              var worddir = (WordDirection) rnd.Next(0, 1);
              if WriteWord(word, startx, starty, worddir, false)
                  WriteWord(word, startx, starty, worddir, true);
          }
      }
      
      public void WriteWord(IEnumerable<char> word, 
                              int startX, 
                              int startY,
                              WordDirection direction,
                              bool commit)
      {
          //Start position
          int x = startX;
          int y = startY;
          
          //Clamp position
              if(direction == WordDirection.Horizontal)
                  x = Math.Clamp(startX, 0, 10 - word.Count);
              if(direction == WordDirection.Vertical)
                  y = Math.Clamp(startY, 0, 10 - word.Count);
          
          //Add a letter
          foreach(char ch in word)
          {
              //Only commit after checking if there's space left, to not overwrite other letters
              if (commit)
                  charTable[x][y] = ch;
              
              //Check if there is space or that the letters match up
              if !(charTable[x][y] == ch || charTable[x][y] == (char)33)
                  return false;
                  
              if(direction == WordDirection.Horizontal)
                  x++;
              if(direction == WordDirection.Vertical)
                  y++;
          }
          
          return true;
      }
      
      public void FillRestOfSpace(){
           for (int x = 0; x < 10; x++)
           {
               for (int y = 0;  y < 10; y ++)
               {
                   if (Chars[x][y] == (char)33)
                      Chars[x][y] = (char)rnd.Next(65, 91) //Couldnt find a better char, chosen for ease
               }
           }
      }
      

      【讨论】:

      • 谢谢,但是我应该在哪里写将插入该字段的单词?
      • 老实说,我不明白代码中写的很多内容。你能给我一个如何在表格中插入一个单词的例子吗?
      • 我认为没有充分的理由先用空字符填充,然后用随机字符重写空字符。一开始你可以只用随机填充,写单词时,它会被重写。
      • @Dima我用一个例子扩展了我的答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      相关资源
      最近更新 更多