Skip to content

Commit

Permalink
Include default dictionary as part of solver
Browse files Browse the repository at this point in the history
Conceptually this makes sense. The Wordament dictionary is essential to
how boards are solved, so we need to start maintaing a dictionary and
working closer to what Wordament actually uses.
  • Loading branch information
davghouse committed Mar 18, 2017
1 parent 7f15510 commit 9a67a4b
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 79 deletions.
9 changes: 0 additions & 9 deletions Daves.WordamentSolver.UI/App.config

This file was deleted.

9 changes: 4 additions & 5 deletions Daves.WordamentSolver.UI/ApplicationEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Windows.Forms;
using Daves.WordamentSolver.UI.Helpers;
using Daves.WordamentSolver.UI.Presenters;
using Daves.WordamentSolver.UI.Presenters;
using Daves.WordamentSolver.UI.Views;
using System;
using System.Windows.Forms;

namespace Daves.WordamentSolver.UI
{
Expand All @@ -16,7 +15,7 @@ private static void Main()

try
{
Solution.SetDictionary(FileHelper.ReadDictionaryFile());
Solution.SetDictionary();
}
catch (Exception e)
{
Expand Down
4 changes: 0 additions & 4 deletions Daves.WordamentSolver.UI/Daves.WordamentSolver.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="App.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand All @@ -103,9 +102,6 @@
</ItemGroup>
<ItemGroup>
<None Include="Resources\BigWIcon.ico" />
<Content Include="TWL06Dictionary.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Daves.WordamentSolver\Daves.WordamentSolver.csproj">
Expand Down
8 changes: 0 additions & 8 deletions Daves.WordamentSolver.UI/Helpers/FileHelper.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
using System.Collections.Generic;
using System.Configuration;
using System.IO;

namespace Daves.WordamentSolver.UI.Helpers
{
// Not sure where to do file access, but keeping it here seems better than doing it all throughout the app.
public static class FileHelper
{
public static IEnumerable<string> ReadDictionaryFile()
=> ReadLines(ConfigurationManager.AppSettings["DictionaryFilePath"]);

public static IEnumerable<string> ReadLines(string filePath)
=> File.ReadLines(filePath);

public static string[] ReadAllLines(string filePath)
=> File.ReadAllLines(filePath);

Expand Down
9 changes: 4 additions & 5 deletions Daves.WordamentSolver.UI/Presenters/SolverPresenter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using Daves.WordamentSolver.UI.Contracts;
using Daves.WordamentSolver.UI.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using Daves.WordamentSolver.UI.Contracts;
using Daves.WordamentSolver.UI.Helpers;

namespace Daves.WordamentSolver.UI.Presenters
{
Expand Down Expand Up @@ -111,8 +111,7 @@ private void View_LoadFromFile(string filePath)
int?[] tilePoints = new int?[BoardSize];
for (int i = 0; i < BoardSize; ++i)
{
int points;
if (int.TryParse(lines[i + BoardSize], out points))
if (int.TryParse(lines[i + BoardSize], out int points))
{
tilePoints[i] = points;
}
Expand Down
18 changes: 8 additions & 10 deletions Daves.WordamentSolver.UI/Views/SolverForm.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using Daves.WordamentSolver.UI.Contracts;
using Daves.WordamentSolver.UI.Helpers;
using Daves.WordamentSolver.UI.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Daves.WordamentSolver.UI.Contracts;
using Daves.WordamentSolver.UI.Helpers;
using Daves.WordamentSolver.UI.Properties;

namespace Daves.WordamentSolver.UI.Views
{
Expand Down Expand Up @@ -156,10 +156,9 @@ private void ClearBoardButton_Click(object sender, EventArgs e)

private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog
var dialog = new SaveFileDialog
{
Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
FilterIndex = 1
Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
};

if (dialog.ShowDialog() == DialogResult.OK)
Expand All @@ -170,10 +169,9 @@ private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)

private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog
var dialog = new OpenFileDialog
{
Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
FilterIndex = 1
Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
};

if (dialog.ShowDialog() == DialogResult.OK)
Expand Down
4 changes: 1 addition & 3 deletions Daves.WordamentSolver.UnitTests/AssemblyInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Linq;

namespace Daves.WordamentSolver.UnitTests
{
Expand All @@ -9,6 +7,6 @@ public static class AssemblyInitializer
{
[AssemblyInitialize]
public static void InitializeAssembly(TestContext context)
=> Solution.SetDictionary(File.ReadLines(@"TWL06Dictionary.txt"));
=> Solution.SetDictionary();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@
<Name>Daves.WordamentSolver</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="..\Daves.WordamentSolver.UI\TWL06Dictionary.txt">
<Link>TWL06Dictionary.txt</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Daves.WordamentSolver.WordSorters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using Daves.WordamentSolver.WordSorters;

namespace Daves.WordamentSolver.UnitTests.WordSorters
{
Expand Down
1 change: 1 addition & 0 deletions Daves.WordamentSolver.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
LICENSE = LICENSE
PackagingInstructions.txt = PackagingInstructions.txt
README.md = README.md
SampleBoard.txt = SampleBoard.txt
EndProjectSection
Expand Down
8 changes: 4 additions & 4 deletions Daves.WordamentSolver/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Board(int boardHeight, int boardWidth,
{
BoardHeight = boardHeight;
BoardWidth = boardWidth;
_basicTilesValues = basicTileValues ?? EnglishBasicTileValues;
_basicTilesValues = basicTileValues ?? BasicTileValues;

Tiles = tileStrings.Zip(tilePoints, (s, p) => new { @string = s, points = p })
.Select((a, i) => CreateTile(
Expand Down Expand Up @@ -49,7 +49,7 @@ public Board(string[,] tileStrings, int?[,] tilePoints,
{
BoardHeight = tileStrings.GetLength(0);
BoardWidth = tileStrings.GetLength(1);
_basicTilesValues = basicTileValues ?? EnglishBasicTileValues;
_basicTilesValues = basicTileValues ?? BasicTileValues;

var tiles = new Tile[BoardSize];
for (int r = 0; r < BoardHeight; ++r)
Expand All @@ -74,7 +74,7 @@ public Board(string[,] tileStrings, int?[,] tilePoints,
public int BoardSize => BoardHeight * BoardWidth;
public virtual IReadOnlyList<Tile> Tiles { get; protected set; }

public virtual Tile CreateTile(int row, int column, int position, string @string, int? points)
protected virtual Tile CreateTile(int row, int column, int position, string @string, int? points)
=> BasicTile.TryCreate(row, column, position, @string, points, _basicTilesValues)
?? DigramTile.TryCreate(row, column, position, @string, points, _basicTilesValues)
?? PrefixTile.TryCreate(row, column, position, @string, points, _basicTilesValues)
Expand Down Expand Up @@ -124,7 +124,7 @@ public virtual IEnumerable<Tile> GetTilesNoMoreThanOneMoveAway(Tile tile)
}
}

public static readonly IReadOnlyDictionary<char, int> EnglishBasicTileValues = new Dictionary<char, int>(new CaseInsensitiveCharEqualityComparer())
public static readonly IReadOnlyDictionary<char, int> BasicTileValues = new Dictionary<char, int>(new CaseInsensitiveCharEqualityComparer())
{{'A', 2}, {'B', 5}, {'C', 3}, {'D', 3}, {'E', 1}, {'F', 5}, {'G', 4}, {'H', 4}, {'I', 2},
{'J', 10}, {'K', 6}, {'L', 3}, {'M', 4}, {'N', 2}, {'O', 2}, {'P', 4}, {'Q', 8}, {'R', 2},
{'S', 2}, {'T', 2}, {'U', 4}, {'V', 6}, {'W', 6}, {'X', 9}, {'Y', 5}, {'Z', 8}};
Expand Down
15 changes: 11 additions & 4 deletions Daves.WordamentSolver/Daves.WordamentSolver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
<RepositoryUrl>https://github.com/davghouse/Daves.WordamentSolver.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>wordament, solver, trie, boggle, board</PackageTags>
<PackageReleaseNotes>2.0.0: Support many-to-many word-path relationships, case-insensitivity, language extensibility, and a faster simple solution alternative
<PackageReleaseNotes>2.1.0: Included default dictionary
2.0.0: Support many-to-many word-path relationships, case-insensitivity, language extensibility, and a faster simple solution alternative
1.4.0: Implement IReadOnlyList/ToString for convenience
1.3.0: Solution tweaks
1.2.0: Better Board extensibility
1.1.0: More flexible Board constructors</PackageReleaseNotes>
<Version>2.0.0</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.0.0</FileVersion>
<Version>2.1.0</Version>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<Content Include="Dictionary.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>false</Pack>
</Content>
</ItemGroup>
</Project>
File renamed without changes.
2 changes: 2 additions & 0 deletions Daves.WordamentSolver/SimpleSolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class SimpleSolution

public SimpleSolution(Board board)
{
if (Solution.Dictionary == null) { Solution.SetDictionary(); }

_board = board;

foreach (Tile tile in _board.Tiles)
Expand Down
9 changes: 6 additions & 3 deletions Daves.WordamentSolver/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Daves.WordamentSolver
{
public class Solution : IReadOnlyList<Word>
{
protected internal static Trie Dictionary { get; protected set; }
public static void SetDictionary(IEnumerable<string> @strings)
=> Dictionary = new Trie(@strings.Where(s => s.Length > 2), new CaseInsensitiveCharEqualityComparer());
public static void SetDictionary(IEnumerable<string> @strings = null)
=> Dictionary = new Trie((@strings ?? File.ReadLines("Dictionary.txt")).Where(s => s.Length > 2), new CaseInsensitiveCharEqualityComparer());

protected readonly Board _board;
protected readonly Dictionary<string, Word> _stringWordMap = new Dictionary<string, Word>(StringComparer.OrdinalIgnoreCase);
Expand All @@ -23,6 +24,8 @@ public Solution()

public Solution(Board board, WordSorter wordSorter = null)
{
if (Dictionary == null) { SetDictionary(); }

_board = board;

foreach (Tile tile in _board.Tiles)
Expand Down Expand Up @@ -114,7 +117,7 @@ public virtual void SortWords(WordSorter wordSorter)
}

public override string ToString()
=> $"Total points: {TotalPoints}, Total words: {TotalWords}";
=> $"Total points: {TotalPoints}, total words: {TotalWords}";

Word IReadOnlyList<Word>.this[int index] => Words[index];
int IReadOnlyCollection<Word>.Count => TotalWords;
Expand Down
2 changes: 1 addition & 1 deletion Daves.WordamentSolver/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected Tile(int row, int column, int position, string @string, int? points,
Position = position;
String = @string;
Points = points;
_basicTilesValues = basicTileValues ?? Board.EnglishBasicTileValues;
_basicTilesValues = basicTileValues ?? Board.BasicTileValues;
}

public int Row { get; }
Expand Down
2 changes: 1 addition & 1 deletion Daves.WordamentSolver/Tiles/BasicTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected BasicTile(int row, int column, int position, string @string, int? poin
public static BasicTile TryCreate(int row, int column, int position, string @string, int? points,
IReadOnlyDictionary<char, int> basicTileValues = null)
{
basicTileValues = basicTileValues ?? Board.EnglishBasicTileValues;
basicTileValues = basicTileValues ?? Board.BasicTileValues;

return @string != null
&& @string.Length == 1
Expand Down
2 changes: 1 addition & 1 deletion Daves.WordamentSolver/Tiles/DigramTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected DigramTile(int row, int column, int position, string @string, int? poi
public static DigramTile TryCreate(int row, int column, int position, string @string, int? points,
IReadOnlyDictionary<char, int> basicTileValues = null)
{
basicTileValues = basicTileValues ?? Board.EnglishBasicTileValues;
basicTileValues = basicTileValues ?? Board.BasicTileValues;

return @string != null
&& @string.Length == 2
Expand Down
2 changes: 1 addition & 1 deletion Daves.WordamentSolver/Tiles/EitherOrTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected EitherOrTile(int row, int column, int position, string @string, int? p
public static EitherOrTile TryCreate(int row, int column, int position, string @string, int? points,
IReadOnlyDictionary<char, int> basicTileValues = null)
{
basicTileValues = basicTileValues ?? Board.EnglishBasicTileValues;
basicTileValues = basicTileValues ?? Board.BasicTileValues;

return @string != null
&& @string.Length == 3
Expand Down
2 changes: 1 addition & 1 deletion Daves.WordamentSolver/Tiles/PrefixTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected PrefixTile(int row, int column, int position, string @string, int? poi
public static PrefixTile TryCreate(int row, int column, int position, string @string, int? points,
IReadOnlyDictionary<char, int> basicTileValues = null)
{
basicTileValues = basicTileValues ?? Board.EnglishBasicTileValues;
basicTileValues = basicTileValues ?? Board.BasicTileValues;

return @string != null
&& @string.Length >= 3
Expand Down
2 changes: 1 addition & 1 deletion Daves.WordamentSolver/Tiles/SuffixTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected SuffixTile(int row, int column, int position, string @string, int? poi
public static SuffixTile TryCreate(int row, int column, int position, string @string, int? points,
IReadOnlyDictionary<char, int> basicTileValues = null)
{
basicTileValues = basicTileValues ?? Board.EnglishBasicTileValues;
basicTileValues = basicTileValues ?? Board.BasicTileValues;

return @string != null
&& @string.Length >= 3
Expand Down
52 changes: 52 additions & 0 deletions PackagingInstructions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
I had trouble configuring the package such that the Dictionary.txt file would
appear automatically in the referencing project's output folder, next to the .dll.

It's set to Content, Copy if newer in Daves.WordamentSolver which makes sense, but in
the .csproj I also set <Pack>false</Pack> to prevent the file from being included in
the root directory of referencing projects.

Then I use Visual Studio to build the .nupkg, change the extension to .zip, unzip, edit
the .nuspec to look something like this (note the <files> tag; that gets added by hand):

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Daves.WordamentSolver</id>
<version>2.1.0</version>
<authors>Dave</authors>
<owners>Dave</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<licenseUrl>https://opensource.org/licenses/MIT</licenseUrl>
<projectUrl>https://github.com/davghouse/Daves.WordamentSolver</projectUrl>
<description>Wordament solver that handles an arbitrary number of special tiles, finds the many-to-many word-path relationships, and approximates a best path.</description>
<releaseNotes>2.1.0: Included default dictionary
2.0.0: Support many-to-many word-path relationships, case-insensitivity, language extensibility, and a faster simple solution alternative
1.4.0: Implement IReadOnlyList/ToString for convenience
1.3.0: Solution tweaks
1.2.0: Better Board extensibility
1.1.0: More flexible Board constructors</releaseNotes>
<copyright>� 2017 Dave</copyright>
<tags>wordament, solver, trie, boggle, board</tags>
<repository type="git" url="https://github.com/davghouse/Daves.WordamentSolver.git" />
<dependencies>
<group targetFramework=".NETFramework4.6" />
</dependencies>
</metadata>
<files>
<file src="build\**" target="build" />
</files>
</package>

Then I add that build folder (at the same folder level as the .nuspec), and it contains
the Dictionary.txt file and a file called Daves.WordamentSolver.targets with these contents:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)Dictionary.txt">
<Link>Dictionary.txt</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

Then zip the ~6 things in the unzipped folder again and change the extension back to .nupkg.
Loading

0 comments on commit 9a67a4b

Please sign in to comment.