| 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(97, 123); |
| } |
| } |
| } |
| |
| 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(); |
| } |
| } |
| } |