Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello 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
XML
<pre> <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
C#
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));
             }
         }

     }
 }


What I have tried:

The next step 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
Posted
Updated 19-Oct-22 10:00am
Comments
Rick York 18-Oct-22 16:21pm    
That is not how the Scrabble game works. Players put down letters onto the board as they play various words. The board has various squares designated for double or triple letter and double or triple word scores. You do need a table of words that are allowed to be played (your dictionary) but that is only used to validate the words that are played.

What you described sounds more like a crossword puzzle.
d0berr 18-Oct-22 17:19pm    
Yes, you're right. I need to place, for example, an array of 5 words on the board and build the logic that if a person found the word, the button is no longer clickable
FreedMalloc 18-Oct-22 20:13pm    
Are you writing a Scrabble game or a Word Search game? Scrabble does not start with words already on a grid for a player to find. In Scrabble the players choose words and place them on a 15x15 grid to maximize their score. A word Search game would populate a grid with some number of words and fill the rest of the grid in randomly so a player would search for words in the list.

It sounds like you are writing a Word Search game. Is that correct?
d0berr 19-Oct-22 2:27am    
Yes it’s correct I try to wire I simple word search game. I'm sorry, but I'm just a beginner and have not been able to solve this problem for two days. I would be glad if you could give me a working code
merano99 19-Oct-22 17:18pm    
Why was the question tagged C? C# and C are two completely different languages and cannot be mixed.
If no solution with C (or C++) comes into question it would be good to remove the flag.
If you want to program a word search game instead of a Scrabble game, also change the title and add the text of the question with your answers of the questions to improve the chance of getting suitable answers.

Are you looking to write a program that auto generates the whole puzzle or will there be a puzzle creator? I'm not asking for an answer. It's a question you must answer because it greatly affects the code to be written.

You'll need to decide on a list of words to place on the grid. Are they entered manually or will the program read them randomly from some dictionary? How many words per puzzle? It may be impossible to place all the words if there are too many. Too few is probably a not a very challenging puzzle. Can words be placed in reverse order (backwards - ANANAB)?

When it's time to place the words on the grid you'll need to determine: 1) whether the word will be placed vertically, horizontally or diagonally; 2) in reverse or normal order; 3) where each word will start in the grid.
Note: You need to make sure that the start location is not too close to an edge. Also, if a word crosses a word already placed it can only do so at a shared letter. For instance "MANGO" can only cross PEACH where the letter A occurs in both words. This gets more complicated if a word crosses more than one already placed word and when words share more than one letter.

Once a starting location and direction have been determined it is merely a looping operation to place the word on the grid. For vertical placement the column won't change but the row will, for horizontal placement the opposite is true and with diagonal both vary.

In the case of vertical placement the loop is something like:
C#
int column = columnStart;
for (int row = rowStart; row < rowStart + word.length; row++)
{
    grid[row][column] = word[row-rowStart];
}

This, of course, assumes that both the start positions and direction have already been validated to not run off the edge or cross already placed words on non-shared letters. It also assumes that the variable word is already in the desired order.

I would suggest placing the words first on an empty grid. That way you can easily inspect the grid to make sure it's placing words correctly before you go on to randomly fill in all the other empty grid spaces. One other thing to consider when randomly placing the remaining letters is that you want to make sure you don't place letters that form words already in the find list.
 
Share this answer
 
Comments
d0berr 19-Oct-22 16:07pm    
1. For words I just wanted to create an array of 5 words for example and start by placing them vertically or horizontally and it will only be read forward with no reverse reading

var words = new[]{ "we", "wet", "enter", "enterance", "ran", "an", "rat"};
FreedMalloc 19-Oct-22 16:19pm    
That's good. It'll make the initial coding much easier. Words crossing each other, diagonals, reversing, greater word counts, etc. can all be added in later iterations one by one if desired once the initial program is working.
d0berr 19-Oct-22 16:34pm    
Yes it’s correct I try to wire I simple word search game. I'm sorry, but I'm just a beginner and have not been able to solve this problem for two days. I would be glad if you could give me a working code
d0berr 19-Oct-22 16:21pm    
But how do I now arrange these words across the table and fill the empty indexes with other letters?
FreedMalloc 19-Oct-22 16:51pm    
Randomly pick a starting row and column. Ensure that starting there doesn't run off the edge of the grid, or cross another word. If it does adjust it up or down, left or right until it fits. Or, pick new starting positions.

Start with a grid where each index is empty. Once all words are placed randomly fill in all the indexes that are still empty.

This task was assigned to you. It's part of your grade or you're paid to do it. I doubt anyone on this site will just hand you code for you to turn in.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900