1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WordSearch
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
 
        private void generatePuzzle()
        {
            const int NWORDS = 6;
            const int ROW = 10;
            const int COL = 25;
 
            char [,] grid = new char[COL,ROW];
 
            //Initialize the grid to empty characters
            for (int r = 0; r < COL; r++)
            {
                for (int c = 0; c < ROW; c++)
                {
                    grid[r, c] = ' ';
                }
            }
 
            FileStream file = null;
            try //Make sure that the file exists.
            {
                file = new FileStream("wordbank.txt", FileMode.Open, FileAccess.Read);
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Please make sure a wordbank.txt file exists in the same directory as this program!""Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
 
            StreamReader sr = new StreamReader(file);
            List<string> words = new List<string>();
            while (!sr.EndOfStream)
            {
                words.Add(sr.ReadLine());
            }
 
            string wordsToFind="";
 
            Random generator = new Random();
            for (int i = 0; i < NWORDS; i++)
            {
                bool validPosition = false;
                do
                {
                    int index = generator.Next(words.Count);
                    string selWord = words[index];
 
                    int xSel = generator.Next(COL);
                    int ySel = generator.Next(ROW);
                    int dir = generator.Next(2);
 
                    //Try inserting the word into the grid.
                    if (dir == 0) //Vertical
                    {
                        if ((ySel + selWord.Length) < ROW) //Make sure the word fits!
                        {
                            //See if we can fit the word in
                            int cntWordIndex = 0;
                            for (int yIndex = ySel; yIndex < (ySel + selWord.Length); yIndex++)
                            {
                                //Can we place this character in this slot?
                                if (grid[xSel, yIndex] != ' ' && grid[xSel, yIndex] != selWord[cntWordIndex])
                                {
                                    validPosition = false;
                                    break;
                                }
                                else
                                {
                                    validPosition = true;
                                }
                                cntWordIndex++;
                            }
                            if (validPosition)
                            {
                                //Put the word into the grid.
                                cntWordIndex = 0;
                                for (int yIndex = ySel; yIndex < (ySel + selWord.Length); yIndex++)
                                {
                                    grid[xSel, yIndex] = selWord[cntWordIndex];
                                    cntWordIndex++;
                                }
                                wordsToFind += selWord;
                                wordsToFind += "\r\n";
                            }
                        }
                    }
                    else //Horizontal
                    {
                        if ((xSel + selWord.Length) < COL) //Make sure the word fits!
                        {
                            //See if we can fit the word in
                            int cntWordIndex = 0;
                            for (int xIndex = xSel; xIndex < (xSel + selWord.Length); xIndex++)
                            {
                                //Can we place this character in this slot?
                                if (grid[xIndex, ySel] != ' ' && grid[xIndex, ySel] != selWord[cntWordIndex])
                                {
                                    validPosition = false;
                                    break;
                                }
                                else
                                {
                                    validPosition = true;
                                }
                                cntWordIndex++;
                            }
                            if (validPosition)
                            {
                                //Put the word into the grid.
                                cntWordIndex = 0;
                                for (int xIndex = xSel; xIndex < (xSel + selWord.Length); xIndex++)
                                {
                                    grid[xIndex, ySel] = selWord[cntWordIndex];
                                    cntWordIndex++;
                                }
                                wordsToFind += selWord;
                                wordsToFind += "\r\n";
                            }
                        }
                    }
 
                } while (!validPosition);
 
                
            }
 
            //Now fill the rest of the grid with random letters!
            for (int r = 0; r < COL; r++)
            {
                for (int c = 0; c < ROW; c++)
                {
                    if (grid[r, c] == ' ')
                    {
                        grid[r, c] = (char)generator.Next(97123);
                    }
                }
            }
 
            string str="";
            for (int r = 0; r < COL; r++)
            {
                for (int c = 0; c < ROW; c++)
                {
                    str += grid[r, c] + "\t";
                }
                str += "\r\n";
            }
 
            txtToFind.Text = wordsToFind;
            txtSearch.Text = str; //All that just to get this line to work :)
        }
 
        private void frmMain_Load(object sender, EventArgs e)
        {
            generatePuzzle();
        }
 
        private void btnGen_Click(object sender, EventArgs e)
        {
            generatePuzzle();
        }
    }
}