private void InitializePublicInfo(CrozzleGenerateFile boot) { PublicInfo.SetRows(boot.GetRows()); PublicInfo.SetColumns(boot.GetColumns()); PublicInfo.SetFullColumns(boot.GetColumns() * 2); PublicInfo.SetFullRows(boot.GetRows() + 3); PublicInfo.SetConfig(boot.GetConfigurationFile()); PublicInfo.SetWordlist(boot.GetWordListFile()); }
private void saveCrozzleToolStripMenuItem_Click(object sender, EventArgs e) { if (SIT323Crozzle.CreateCrozzle.GetBestCrozzle().GetScore() == 0) { MessageBox.Show("No Crozzle Generated, please Generate a Crozzle first", "ERROR"); return; } SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = "czl file(*.czl) | *.czl"; saveDialog.DefaultExt = "czl"; saveDialog.AddExtension = true; saveDialog.RestoreDirectory = true; if (saveDialog.ShowDialog() == DialogResult.OK) { string savePath = saveDialog.FileName; FileStream stream = new FileStream(savePath, FileMode.Create); StreamWriter writer = new StreamWriter(stream); CrozzlePartial bs = SIT323Crozzle.CreateCrozzle.GetBestCrozzle(); writer.WriteLine("ROWS=" + PublicInfo.GetRows()); writer.WriteLine("COLUMNS=" + PublicInfo.GetColumns()); writer.WriteLine("CONFIGURATION_FILE=\"" + PublicInfo.GetConfig() + "\""); writer.WriteLine("WORDLIST_FILE=\"" + PublicInfo.GetWordlist() + "\""); for (int i = 0; i < bs.GetUsedWord().Count; i++) { string s = ""; if (bs.GetUsedWord()[i].GetType().Equals("COLUMN")) { s += "COLUMN="; s += bs.GetUsedWord()[i].GetColumns() - bs.GetMinWidth(); s += ","; s += bs.GetUsedWord()[i].GetWordContent(); s += ","; s += bs.GetUsedWord()[i].GetRows() - bs.GetMinHeight(); } else if (bs.GetUsedWord()[i].GetType().Equals("ROW")) { s += "ROW="; s += bs.GetUsedWord()[i].GetRows() - bs.GetMinHeight(); s += ","; s += bs.GetUsedWord()[i].GetWordContent(); s += ","; s += bs.GetUsedWord()[i].GetColumns() - bs.GetMinWidth(); } writer.WriteLine(s); } writer.Close(); stream.Close(); } }
public static void GenerateCrozzle(CrozzlePartial cp) { if (!Crozzle.aTimer.Enabled) { return; } List <Word> wordsToInsert = CanInsertWords(cp); int length = wordsToInsert.Count(); if (length == 0) { // one crozzle is found if (cp.GetScore() > highScore) { highScore = cp.GetScore(); } else { return;// return if the new crozzle's score is not high enough } if (PublicInfo.GetMinGroups() > 1) { cp = SeperateGroups(cp); } if (cp.GetScore() > HighScoreCrozzle.GetScore()) { HighScoreCrozzle = cp; // print score of crozzle Console.WriteLine("==============================================================="); Console.WriteLine("New Highest Score: " + cp.GetScore()); Console.WriteLine("==============================================================="); } return; } if (cp.GetUsedWord().Count >= PublicInfo.GetRows() && cp.GetUsedWord().Count <= PublicInfo.GetRows() + PublicInfo.GetColumns() && cp.GetScore() <= cp.GetUsedWord().Count *averageScorePerWord * 5 / 6) { return; } for (int i = 0; i < length; i++) { if (wordsToInsert[i].GetAddScore() < 0) { continue; } CrozzlePartial c = InsertWord(cp, wordsToInsert[i], wordsToInsert[i].GetAddScore()); GenerateCrozzle(c); } }
private static int CalculateScoreAfterRemovingAWord(CrozzlePartial cp, Word removeWord) { int score = cp.GetScore(); // reverse the process of adding a word score -= PublicInfo.GetPointsPerWord(); for (int i = 0; i < cp.GetUsedWord().Count; i++) { if (!cp.GetUsedWord()[i].GetType().Equals(removeWord.GetType()) && CrozzleValidation.Crossing(cp.GetUsedWord()[i], removeWord)) { char crossingLetter = CrozzleValidation.GetCrossingLetter(cp.GetUsedWord()[i], removeWord); score -= WordInfo.intersectingPointsPerLetter[crossingLetter]; score += WordInfo.nonIntersectingPointsPerLetter[crossingLetter]; } } return(score); }
/// <summary> /// Seperate a one-group crozzle into multi-group crozzle /// </summary> /// <param name="cp">One-group crozzle</param> /// <returns>One multi-group crozzle</returns> public static CrozzlePartial SeperateGroups(CrozzlePartial cp) { int bestScore = 0; List <Word> bestUsedWords = new List <Word>(); for (int i = 0; i < cp.GetUsedWord().Count; i++) { List <Word> usedWords = new List <Word>(); for (int j = 0; j < cp.GetUsedWord().Count; j++) { usedWords.Add(cp.GetUsedWord()[j]); } Word deleteWord = cp.GetUsedWord()[i]; usedWords.Remove(cp.GetUsedWord()[i]); int groups; try { WordGroupServiceClient ws; string endpoint = "BasicHttpBinding_IWordGroupService"; ws = new WordGroupServiceClient(endpoint); Grid grid = new Grid(PublicInfo.GetFullRows(), PublicInfo.GetFullColumns(), usedWords); string[] stringGrid = ConvertToString(grid.GetGrid(), PublicInfo.GetFullRows(), PublicInfo.GetFullColumns()); groups = ws.Count(stringGrid); } catch { // use local group calculation method if online method fail groups = CalculateGroup(usedWords); } int score = CalculateScoreAfterRemovingAWord(cp, cp.GetUsedWord()[i]); if (groups >= PublicInfo.GetMinGroups() && groups <= PublicInfo.GetMaxGroups() && score > bestScore && CheckAllIntersections(usedWords)) { bestScore = score; bestUsedWords = usedWords; } } // generate a multi-groups crozzle with used words cp.SetUsedWord(bestUsedWords); cp.SetScore(bestScore); cp.SetGrid(new Grid(PublicInfo.GetFullRows(), PublicInfo.GetFullColumns(), bestUsedWords)); return(cp); }
private void RemoveWords(List <string> wlist) { for (int i = 0; i < wlist.Count; i++) { if (wlist[i].Length >= 4 && PublicInfo.ContainHighValueLetter(wlist[i]) < 1) { wlist.Remove(wlist[i--]); continue; } if (wlist[i].Length >= 6 && PublicInfo.ContainHighValueLetter(wlist[i]) < 2) { wlist.Remove(wlist[i--]); continue; } if (wlist[i].Length >= PublicInfo.GetColumns() / 2 - 1) { wlist.Remove(wlist[i--]); } } }
private CrozzlePartial GenerateInitialCrozzle(string initialWordContent, List <string> wlist) { CrozzlePartial cp = new CrozzlePartial(); // set initial word position Word initialWord = SetInitialWordPositionAndCrozzlePosition(initialWordContent, cp); // set initial crozzle List <Word> usedWord = new List <Word>(); usedWord.Add(initialWord); Grid g = new Grid(PublicInfo.GetFullRows(), PublicInfo.GetFullColumns(), usedWord); cp.SetGrid(g); cp.SetUsedWord(usedWord); cp.SetWordlist(wlist); int initialScore = GetInitialScore(initialWordContent); cp.SetScore(initialScore + PublicInfo.GetPointsPerWord()); return(cp); }
private Word SetInitialWordPositionAndCrozzlePosition(string initialWordContent, CrozzlePartial cp) { Word initialWord = new Word(); initialWord.SetWordContent(initialWordContent); initialWord.SetType("ROW"); double temp = (PublicInfo.GetFullRows() - 1) / 2.0; int y = (int)Math.Ceiling(temp); initialWord.SetRows(y); temp = (PublicInfo.GetFullColumns() - initialWordContent.Length) / 2.0; int x = (int)Math.Ceiling(temp); initialWord.SetColumns(x); cp.SetHeight(PublicInfo.GetFullRows()); cp.SetWidth(PublicInfo.GetFullColumns()); cp.SetMaxHeight(y); cp.SetMaxWidth(x + initialWordContent.Length - 1); cp.SetMinHeight(y); cp.SetMinWidth(x); return(initialWord); }
private void InitializePublicInfo(Config configuration) { PublicInfo.SetMinGroups(configuration.GetMinimumNumberOfGroups()); PublicInfo.SetMaxGroups(configuration.GetMaximumNumberOfGroups()); PublicInfo.SetPointsPerWord(configuration.GetPointsPerWord()); }
private void CreateCrozzle() { Config configuration = new Config(); configuration.SetConfig("configuration.txt"); string intersectionsPerPoints = configuration.GetIntersectingPointsPerLetter(); string nonIntersectionsPerPoints = configuration.GetNonIntersectingPointsPerLetter(); InitializePublicInfo(configuration); WordInfo wordlist = new WordInfo(); wordlist.SetWordList("wordList.txt"); wordlist.SetIntersectingPointsPerLetter(intersectionsPerPoints); wordlist.SetNonIntersectingPointsPerLetter(nonIntersectionsPerPoints); List <string> wlist = wordlist.GetWordList(); // preprocess wordlist SIT323Crozzle.CreateCrozzle.SetPotentialValue(wlist, wordlist.GetIntersectingPointsPerLetter(), wordlist.GetNonIntersectingPointsPerLetter()); string initialWordContent = PreProcess(wlist); // get initial crozzle CrozzlePartial initialCrozzle = GenerateInitialCrozzle(initialWordContent, wlist); aTimer.Start(); SIT323Crozzle.CreateCrozzle.GenerateCrozzle(initialCrozzle); CrozzlePartial goodCrozzle = SIT323Crozzle.CreateCrozzle.GetBestCrozzle(); aTimer.Stop(); // get grid and show crozzle string style = configuration.GetStyle(); bool uppercase = configuration.GetUppercase(); Grid crozzleGrid = goodCrozzle.GetGrid(); char[,] largeGrid = crozzleGrid.GetGrid(); char[,] grid = new char[PublicInfo.GetRows(), PublicInfo.GetColumns()]; for (int i = 0; i < PublicInfo.GetRows(); i++) { for (int j = 0; j < PublicInfo.GetColumns(); j++) { if (i + goodCrozzle.GetMinHeight() <= goodCrozzle.GetMaxHeight() && j + goodCrozzle.GetMinWidth() <= goodCrozzle.GetMaxWidth()) { grid[i, j] = largeGrid[i + goodCrozzle.GetMinHeight(), j + goodCrozzle.GetMinWidth()]; } } } String crozzleHTML = @"<!DOCTYPE html> <html> <head>" + style + @"</head><body><table>"; for (int row = 0; row < PublicInfo.GetRows(); row++) { String tr = "<tr>"; for (int column = 0; column < PublicInfo.GetColumns(); column++) { if (grid[row, column].CompareTo('\0') != 0) { tr += @"<td style='background:" + configuration.GetBgcolourNonEmptyTD(); if (uppercase == true) { tr += "'>" + grid[row, column].ToString().ToUpper() + @"</td>"; } if (uppercase == false) { tr += "'>" + grid[row, column].ToString().ToLower() + @"</td>"; } } else { tr += @"<td style='background:" + configuration.GetBgcolourEmptyTD(); if (uppercase == true) { tr += "'>" + grid[row, column].ToString().ToUpper() + @"</td>"; } if (uppercase == false) { tr += "'>" + grid[row, column].ToString().ToLower() + @"</td>"; } } } tr += "</tr>"; crozzleHTML += tr; } crozzleHTML += @"</table>"; crozzleHTML += "<p>score: "; crozzleHTML += goodCrozzle.GetScore(); crozzleHTML += "</p>"; crozzleHTML += "</body></html>"; CrozzleBrowser.DocumentText = crozzleHTML; ErrorBrowser.DocumentText = " "; }
public static CrozzlePartial InsertWord(CrozzlePartial cp, Word s, int addScore) { CrozzlePartial returnCP = new CrozzlePartial(); returnCP.SetWidth(cp.GetWidth()); returnCP.SetHeight(cp.GetHeight()); returnCP.SetMaxHeight(cp.GetMaxHeight()); returnCP.SetMinHeight(cp.GetMinHeight()); returnCP.SetMaxWidth(cp.GetMaxWidth()); returnCP.SetMinWidth(cp.GetMinWidth()); List <string> wordlist = new List <string>(); for (int i = 0; i < cp.GetWordlist().Count; i++) { wordlist.Add(cp.GetWordlist()[i]); } List <Word> usedWord = new List <Word>(); for (int i = 0; i < cp.GetUsedWord().Count; i++) { usedWord.Add(cp.GetUsedWord()[i]); } Grid grid = new Grid(); int score = cp.GetScore(); wordlist.Remove(s.GetWordContent()); usedWord.Add(s); char[,] charGrid = new char[PublicInfo.GetFullRows(), PublicInfo.GetFullColumns()]; for (int i = 0; i < PublicInfo.GetFullRows(); i++) { for (int j = 0; j < PublicInfo.GetFullColumns(); j++) { charGrid[i, j] = cp.GetGrid().GetGrid()[i, j]; } } int minHeight = cp.GetMinHeight(); int maxHeight = cp.GetMaxHeight(); int minWidth = cp.GetMinWidth(); int maxWidth = cp.GetMaxWidth(); if (s.GetType().Equals("ROW")) { int rows = s.GetRows() - 1; int columnsBegin = s.GetColumns() - 1; int columnsEnd = s.GetColumns() + s.GetWordContent().Length - 2; if (rows > maxHeight) { returnCP.SetMaxHeight(rows); } if (rows < minHeight) { returnCP.SetMinHeight(rows); } if (columnsBegin < minWidth && columnsEnd > maxWidth) { returnCP.SetMaxWidth(columnsEnd); returnCP.SetMinWidth(columnsBegin); } else if (columnsBegin < minWidth && columnsEnd <= maxWidth) { returnCP.SetMinWidth(columnsBegin); } else if (columnsBegin >= minWidth && columnsEnd > maxWidth) { returnCP.SetMaxWidth(columnsEnd); } for (int i = 0; i < s.GetWordContent().Length; i++) { charGrid[s.GetRows() - 1, s.GetColumns() + i - 1] = s.GetWordContent()[i]; } } else if (s.GetType().Equals("COLUMN")) { int columns = s.GetColumns() - 1; int rowsBegin = s.GetRows() - 1; int rowsEnd = s.GetRows() + s.GetWordContent().Length - 2; if (columns > maxWidth) { returnCP.SetMaxWidth(columns); } if (columns < minWidth) { returnCP.SetMinWidth(columns); } if (rowsBegin < minHeight && rowsEnd > maxHeight) { returnCP.SetMaxHeight(rowsEnd); returnCP.SetMinHeight(rowsBegin); } else if (rowsBegin < minHeight && rowsEnd <= maxHeight) { returnCP.SetMinHeight(rowsBegin); } else if (rowsBegin >= minHeight && rowsEnd > maxHeight) { returnCP.SetMaxHeight(rowsEnd); } for (int i = 0; i < s.GetWordContent().Length; i++) { charGrid[s.GetRows() + i - 1, s.GetColumns() - 1] = s.GetWordContent()[i]; } } score += addScore; grid.SetGrid(charGrid); returnCP.SetGrid(grid); // Set & Return returnCP.SetUsedWord(usedWord); returnCP.SetWordlist(wordlist); returnCP.SetGrid(grid); returnCP.SetScore(score); return(returnCP); }
public static int CalInsertScore(CrozzlePartial cp, Word word) { char[,] grid = cp.GetGrid().GetGrid(); int minHeight = cp.GetMinHeight(); int maxHeight = cp.GetMaxHeight(); int minWidth = cp.GetMinWidth(); int maxWidth = cp.GetMaxWidth(); int currentHeight = maxHeight - minHeight + 1; int currentWidth = maxWidth - minWidth + 1; int result = 10; if (word.GetType().Equals("ROW")) { int rows = word.GetRows() - 1; int columnsBegin = word.GetColumns() - 1; int columnsEnd = word.GetColumns() + word.GetWordContent().Length - 2; if (columnsBegin < 0) { return(-1); } if (columnsEnd > PublicInfo.GetFullColumns() - 1) { return(-1); } if (rows > maxHeight) { int newHeight = rows - minHeight + 1; if (newHeight > PublicInfo.GetRows()) { return(-1); } } if (rows < minHeight) { int newHeight = maxHeight - rows + 1; if (newHeight > PublicInfo.GetRows()) { return(-1); } } if (columnsBegin < minWidth && columnsEnd > maxWidth) { int newWidth = columnsEnd - columnsBegin + 1; if (newWidth > PublicInfo.GetColumns()) { return(-1); } } else if (columnsBegin < minWidth && columnsEnd <= maxWidth) { int newWidth = maxWidth - columnsBegin + 1; if (newWidth > PublicInfo.GetColumns()) { return(-1); } } else if (columnsBegin >= minWidth && columnsEnd > maxWidth) { int newWidth = columnsEnd - minWidth + 1; if (newWidth > PublicInfo.GetColumns()) { return(-1); } } if (columnsBegin - 1 >= 0) { if (grid[rows, columnsBegin - 1] != '\0') { return(-1); } } if (columnsEnd + 1 <= PublicInfo.GetFullColumns() - 1) { if (grid[rows, columnsEnd + 1] != '\0') { return(-1); } } for (int i = 0; i < word.GetWordContent().Length; i++) { result += WordInfo.nonIntersectingPointsPerLetter[word.GetWordContent()[i]]; if (grid[rows, columnsBegin + i] != '\0') { if (grid[rows, columnsBegin + i] != word.GetWordContent()[i]) { return(-1); } else { result += WordInfo.intersectingPointsPerLetter[word.GetWordContent()[i]]; result -= 2 * WordInfo.nonIntersectingPointsPerLetter[word.GetWordContent()[i]]; } // new if (columnsBegin + i + 1 <= PublicInfo.GetFullColumns() - 1) { if (grid[rows, columnsBegin + i + 1] != '\0') { return(-1); } } } else { if (rows != 0) { if (grid[rows - 1, columnsBegin + i] != '\0') { return(-1); } } if (rows != PublicInfo.GetFullRows() - 1) { if (grid[rows + 1, columnsBegin + i] != '\0') { return(-1); } } } } } else if (word.GetType().Equals("COLUMN")) { int columns = word.GetColumns() - 1; int rowsBegin = word.GetRows() - 1; int rowsEnd = word.GetRows() + word.GetWordContent().Length - 2; if (rowsBegin < 0) { return(-1); } if (rowsEnd > PublicInfo.GetFullRows() - 1) { return(-1); } if (columns > maxWidth) { int newWidth = columns - minWidth + 1; if (newWidth > PublicInfo.GetColumns()) { return(-1); } } if (columns < minWidth) { int newWidth = maxWidth - columns + 1; if (newWidth > PublicInfo.GetColumns()) { return(-1); } } if (rowsBegin < minHeight && rowsEnd > maxHeight) { int newHeight = rowsEnd - rowsBegin + 1; if (newHeight > PublicInfo.GetRows()) { return(-1); } } else if (rowsBegin < minHeight && rowsEnd <= maxHeight) { int newHeight = maxHeight - rowsBegin + 1; if (newHeight > PublicInfo.GetRows()) { return(-1); } } else if (rowsBegin >= minHeight && rowsEnd > maxHeight) { int newHeight = rowsEnd - minHeight + 1; if (newHeight > PublicInfo.GetRows()) { return(-1); } } if (rowsBegin - 1 >= 0) { if (grid[rowsBegin - 1, columns] != '\0') { return(-1); } } if (rowsEnd + 1 <= PublicInfo.GetFullRows() - 1) { if (grid[rowsEnd + 1, columns] != '\0') { return(-1); } } for (int i = 0; i < word.GetWordContent().Length; i++) { result += WordInfo.nonIntersectingPointsPerLetter[word.GetWordContent()[i]]; if (grid[rowsBegin + i, columns] != '\0') { if (grid[rowsBegin + i, columns] != word.GetWordContent()[i]) { return(-1); } else { result += WordInfo.intersectingPointsPerLetter[word.GetWordContent()[i]]; result -= 2 * WordInfo.nonIntersectingPointsPerLetter[word.GetWordContent()[i]]; } // new if (rowsBegin + i + 1 <= PublicInfo.GetFullRows() - 1) { if (grid[rowsBegin + i + 1, columns] != '\0') { return(-1); } } } else { if (columns != 0) { if (grid[rowsBegin + i, columns - 1] != '\0') { return(-1); } } if (columns != PublicInfo.GetFullColumns() - 1) { if (grid[rowsBegin + i, columns + 1] != '\0') { return(-1); } } } } } return(result); }