diff --git a/.github/workflows/validate-examples.yml b/.github/workflows/validate-examples.yml new file mode 100644 index 0000000..61c8ce4 --- /dev/null +++ b/.github/workflows/validate-examples.yml @@ -0,0 +1,27 @@ +name: Validate examples + +on: + pull_request: + branches: [ "main", "v*.*.*" ] + push: + branches: [ "main", "v*.*.*" ] + +env: + API_PROJECT: src/NonoSharp/NonoSharp.csproj + EXAMPLE_VALIDATOR: docs/examples/NonoSharp.ExampleValidator/NonoSharp.ExampleValidator.csproj + +jobs: + validate-examples: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 10.0.x + - name: Build API project + run: dotnet build -c Release ${{ env.API_PROJECT }} + - name: Validate examples + run: dotnet run --project ./docs/examples/NonoSharp.ExampleValidator \ No newline at end of file diff --git a/.gitignore b/.gitignore index 67d3b0e..dc32237 100644 --- a/.gitignore +++ b/.gitignore @@ -362,12 +362,12 @@ MigrationBackup/ # Fody - auto-generated XML schema FodyWeavers.xsd -# Puzzle library JSON generated upon MAUI project build -src/NonoSharp.Maui/Resources/Raw/Puzzles/*.json - # DocFX _site/ api/ docs/_site/ docs/api/ docs/obj/ + +# Generated temporary files (e.g. for validating examples) +temp/ diff --git a/NonoSharp.slnx b/NonoSharp.slnx index 147e0b5..781b966 100644 --- a/NonoSharp.slnx +++ b/NonoSharp.slnx @@ -4,8 +4,11 @@ + + + + - diff --git a/README.md b/README.md index 954efdc..b6d1d90 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ Documentation for the API is found on this repo's GitHub pages, NonoSharp is designed to be easy to use. The example below shows how to create a random Nonogram puzzle, perform cell operations and check the puzzle's solved state. Furthermore, it shows how to access the clues for each row and column, and subscribes to one of the handy events. + ```csharp using NonoSharp; @@ -97,6 +98,8 @@ nonogram.CellStateChanged += (s, e) => { ### Nonogram creation NonoSharp's `NonogramBuilder` class allows you to create your own Nonogram puzzles quickly and easily, while ensuring puzzles remain uniquely solvable. + + ```csharp using NonoSharp; @@ -117,6 +120,8 @@ if (builder.IsSolvable()) ### Solving Nonograms As mentioned, NonoSharp comes with a custom Nonogram solver. This can be used to solve any Nonogram puzzle. + + ```csharp using NonoSharp; diff --git a/docs/examples.md b/docs/examples.md index 446b169..2aa44d6 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -2,107 +2,27 @@ ## Creating a random puzzle and basic cell operations The following example creates a new puzzle with a width of 5 and a height of 10. It then fills, crosses and clears the cell located at (0, 0). -```csharp -using NonoSharp; - -Nonogram nonogram = Nonogram.CreateRandomPuzzle(5, 10); -nonogram.FillCell(0, 0); -nonogram.CrossCell(0, 0); -nonogram.EmptyCell(0, 0); -``` +[!code-csharp[](examples/BasicOperations.cs)] ## Using Events The following example uses the @"NonoSharp.Nonogram.CellStateChanged" and @"NonoSharp.Nonogram.PuzzleSolved" events to get information about the changing puzzle state. -```csharp -using NonoSharp; - -Nonogram nonogram = Nonogram.CreateRandomPuzzle(5, 5); -nonogram.CellStateChanged += (s, e) => -{ - Console.WriteLine("A cell has changed states!"); - - // Checks if the first cell in e.Cells is now filled - CellPosition cell = e.Cells[0]; - Console.WriteLine($"First cell filled: {nonogram.IsCellFilled(cell.X, cell.Y)}"); -}; - -nonogram.PuzzleSolved += (s, e) => -{ - Console.WriteLine("The puzzle has been solved!"); -}; - -nonogram.FillCell(1, 1); -``` +[!code-csharp[](examples/Events.cs)] ## Saving/Loading solutions The following code creates a random puzzle, saves it to disk and then loads the [example puzzle](example_puzzle.ns). -```csharp -using NonoSharp; -Nonogram nonogram = Nonogram.CreateRandomPuzzle(5, 5); - -// Saves the puzzle solution to ./save_puzzle_example.ns -// (note that this might be in the bin folder when running the code) -// The title of the puzzle is "Example". This field can be left empty to not add a title -Console.WriteLine("Saving puzzle..."); -nonogram.SaveAsFile("save_puzzle_example.ns", "Example"); -Console.WriteLine("Puzzle successfully saved!"); -// Do not forget to catch the exceptions in your code! They have been removed in the example -// to save space - -// Loads the example_puzzle.ns file. Note that this might be loaded from the bin folder -// when running the code. -Console.WriteLine("Loading Example puzzle..."); -nonogram = Nonogram.LoadPuzzle("example_puzzle.ns"); -Console.WriteLine("Successfully loaded puzzle!"); -// Once again, do not forget to catch thrown exceptions. -``` +[!code-csharp[](examples/SavingLoading.cs)] ## Using NonogramBuilder With @"NonoSharp.NonogramBuilder", you can create your own puzzles and convert them to a @"NonoSharp.Nonogram" instance or save them to disk! The example below creates a 5×5 puzzle with a smiley face as solution and saves it to a file. -```csharp -using NonoSharp; -NonogramBuilder builder = new(5, 5); - -// Construct custom solution -builder.FillCell(1, 0); builder.FillCell(3, 0); +[!code-csharp[](examples/Builder.cs)] -builder.FillCell(1, 1); builder.FillCell(3, 1); - -builder.FillCell(0, 3); builder.FillCell(4, 3); - -builder.FillCell(0, 4); builder.FillCell(1, 4); builder.FillCell(2, 4); -builder.FillCell(3, 4); builder.FillCell(4, 4); - -// Save the constructed solution. Do not forget to catch the exceptions! -builder.SaveAsFile("builder_example.ns"); -``` - -### Using the built-in solver +## Using the built-in solver The following example uses the built-in solver to solve the 3×3 puzzle shown below. - Puzzle to solve - -```csharp -using NonoSharp; - -Clues[] columnClues = -[ - new Clues(3), - new Clues(1), - new Clues(3), -]; -Clues[] rowClues = -[ - new Clues(3), - new Clues(1, 1), - new Clues(1, 1), -]; - -var solvable = Solver.IsSolvable(3, 3, columnClues, rowClues, out var solution); -``` \ No newline at end of file +[!code-csharp[](examples/Solver.cs)] diff --git a/docs/examples/BasicOperations.cs b/docs/examples/BasicOperations.cs new file mode 100644 index 0000000..5727307 --- /dev/null +++ b/docs/examples/BasicOperations.cs @@ -0,0 +1,6 @@ +using NonoSharp; + +Nonogram nonogram = Nonogram.CreateRandomPuzzle(5, 10); +nonogram.FillCell(0, 0); +nonogram.CrossCell(0, 0); +nonogram.EmptyCell(0, 0); diff --git a/docs/examples/Builder.cs b/docs/examples/Builder.cs new file mode 100644 index 0000000..5dff789 --- /dev/null +++ b/docs/examples/Builder.cs @@ -0,0 +1,15 @@ +using NonoSharp; +NonogramBuilder builder = new(5, 5); + +// Construct custom solution +builder.FillCell(1, 0); builder.FillCell(3, 0); + +builder.FillCell(1, 1); builder.FillCell(3, 1); + +builder.FillCell(0, 3); builder.FillCell(4, 3); + +builder.FillCell(0, 4); builder.FillCell(1, 4); builder.FillCell(2, 4); +builder.FillCell(3, 4); builder.FillCell(4, 4); + +// Save the constructed solution. Do not forget to catch the exceptions! +builder.SaveAsFile("builder_example.ns"); diff --git a/docs/examples/Events.cs b/docs/examples/Events.cs new file mode 100644 index 0000000..223ab7d --- /dev/null +++ b/docs/examples/Events.cs @@ -0,0 +1,18 @@ +using NonoSharp; + +Nonogram nonogram = Nonogram.CreateRandomPuzzle(5, 5); +nonogram.CellStateChanged += (s, e) => +{ + Console.WriteLine("A cell has changed states!"); + + // Checks if the first cell in e.Cells is now filled + CellPosition cell = e.Cells[0]; + Console.WriteLine($"First cell filled: {nonogram.IsCellFilled(cell.X, cell.Y)}"); +}; + +nonogram.PuzzleSolved += (s, e) => +{ + Console.WriteLine("The puzzle has been solved!"); +}; + +nonogram.FillCell(1, 1); diff --git a/docs/examples/NonoSharp.ExampleValidator/NonoSharp.ExampleValidator.csproj b/docs/examples/NonoSharp.ExampleValidator/NonoSharp.ExampleValidator.csproj new file mode 100644 index 0000000..ed9781c --- /dev/null +++ b/docs/examples/NonoSharp.ExampleValidator/NonoSharp.ExampleValidator.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/docs/examples/NonoSharp.ExampleValidator/Program.cs b/docs/examples/NonoSharp.ExampleValidator/Program.cs new file mode 100644 index 0000000..7c2af66 --- /dev/null +++ b/docs/examples/NonoSharp.ExampleValidator/Program.cs @@ -0,0 +1,140 @@ +using System.Diagnostics; +using System.Text; +using System.Xml.Linq; + +var examplesDirectory = "docs/examples"; +var examples = Directory.GetFiles(examplesDirectory, "*.cs"); + +const string libraryProject = "src/NonoSharp/NonoSharp.csproj"; + +Console.WriteLine($"Found {examples.Length} in {examplesDirectory}"); + + +Directory.CreateDirectory("temp"); + +try +{ + + Process[] processes = new Process[examples.Length]; + + for (int i = 0; i < examples.Length; i++) + { + processes[i] = StartValidation(examples[i]); + } + + if (!WaitForProcesses(processes)) + { + Console.WriteLine("Failed to validate every example."); + return 1; + } + + Console.WriteLine($"All {examples.Length} examples successfully compile."); + return 0; +} +finally +{ + Directory.Delete("temp", true); +} + +static Process StartValidation(string examplePath) +{ + string exampleFileName = Path.GetFileName(examplePath); + string projectPath = CreateExampleProject(examplePath); + + Process process = new() + { + StartInfo = new() { FileName = "dotnet" } + }; + + process.StartInfo.ArgumentList.Add("build"); + process.StartInfo.ArgumentList.Add(projectPath); + process.StartInfo.ArgumentList.Add("-v"); + process.StartInfo.ArgumentList.Add("quiet"); + process.StartInfo.ArgumentList.Add("-c"); + process.StartInfo.ArgumentList.Add("Release"); + + Console.WriteLine($"Starting validation of {exampleFileName}..."); + process.Start(); + + return process; +} + +static string CreateExampleProject(string examplePath) +{ + // Create the project including the required csproj and .cs code example file + string exampleName = Path.GetFileNameWithoutExtension(examplePath); + string projectPath = Path.Combine("temp", exampleName); + Directory.CreateDirectory(projectPath); + CreateProjectFile(projectPath, examplePath); + + return projectPath; +} + +/// +/// Sets up the required csproj file to build an example. +/// +/// The path to create the project in +/// The path of the .cs file of the example from this repo's root +static void CreateProjectFile(string projectPath, string examplePath) +{ + // Create the csproj file required to build the example + + var project = new XElement("Project", new XAttribute("Sdk", "Microsoft.NET.Sdk")); + + var propertyGroup = new XElement( + "PropertyGroup", + new XElement("OutputType", "Exe"), + new XElement("ImplicitUsings", "enable"), + new XElement("TargetFramework", "net10.0") + ); + project.Add(propertyGroup); + + var itemGroup = new XElement( + "ItemGroup", + new XElement("ProjectReference", + new XAttribute("Include", Path.Combine(Directory.GetCurrentDirectory(), libraryProject)) + ), // Add NonoSharp to dependency + + new XElement("Compile", + new XAttribute("Include", Path.Combine(Directory.GetCurrentDirectory(), examplePath)) + ) + ); + project.Add(itemGroup); + + string csproj = Path.Combine(projectPath, Path.GetFileName(projectPath) + ".csproj"); + + var document = new XDocument(project); + document.Save(csproj); +} + +static bool WaitForProcesses(Process[] processes) +{ + bool success = true; + + var failureInfo = new List(); + foreach (Process process in processes) + { + process.WaitForExit(); + + string exampleName = Path.GetFileName(process.StartInfo.ArgumentList[1]); + if (process.ExitCode != 0) + { + failureInfo.Add($"{exampleName} (exit code {process.ExitCode})"); + success = false; + break; + } + } + + if (failureInfo.Count > 0) + { + StringBuilder sb = new(); + sb.AppendLine(new string('=', 10)); + sb.AppendLine("Failed to build the following examples:"); + foreach (var failure in failureInfo) + { + sb.AppendLine($"- {failure}"); + } + Console.WriteLine(sb.ToString()); + } + return success; +} diff --git a/docs/examples/README_basic.cs b/docs/examples/README_basic.cs new file mode 100644 index 0000000..a7916a0 --- /dev/null +++ b/docs/examples/README_basic.cs @@ -0,0 +1,38 @@ +using NonoSharp; + +// Creates a new random 10x10 puzzle. Generation is guaranteed to produce a solvable puzzle. +// This method is also available asynchronously via Nonogram.CreateRandomPuzzleAsync +var nonogram = Nonogram.CreateRandomPuzzle(10, 10); // (width x height) + +// Fill in or cross a cell (coordinates are zero-indexed, (0, 0) is top-left) +nonogram.FillCell(2, 3); +nonogram.CrossCell(0, 0); + +// Moves can be undone/redone +if (nonogram.CanUndo) +{ + nonogram.Undo(); +} + +// Check individual cell state +bool isFilled = nonogram.IsCellFilled(2, 3); + +// Check overall progress +if (nonogram.IsPuzzleSolved()) +{ + Console.WriteLine("Solved!"); +} +else +{ + Console.WriteLine("Not solved :("); +} + + +// The clues shown alongside the grid (e.g. "3 1" for a row) are available for building your own UI +Clues[] columnClues = nonogram.ColumnClues; +Clues[] rowClues = nonogram.RowClues; + +// There are also some events provided +nonogram.CellStateChanged += (s, e) => { + Console.WriteLine("A cell has changed states"); +}; diff --git a/docs/examples/README_builder.cs b/docs/examples/README_builder.cs new file mode 100644 index 0000000..650fdde --- /dev/null +++ b/docs/examples/README_builder.cs @@ -0,0 +1,15 @@ +using NonoSharp; + +NonogramBuilder builder = NonogramBuilder.FromString( + " O O \n" + + " \n" + + " \n" + + "O O\n" + + "OOOOO", + "Smiley"); + +if (builder.IsSolvable()) +{ + Nonogram puzzle = builder.ToNonogram(); + puzzle.SaveAsFile("smiley.ns"); +} diff --git a/docs/examples/README_solver.cs b/docs/examples/README_solver.cs new file mode 100644 index 0000000..86ecef6 --- /dev/null +++ b/docs/examples/README_solver.cs @@ -0,0 +1,16 @@ +using NonoSharp; + +Clues[] columnClues = +[ + new Clues(3), + new Clues(1), + new Clues(3), +]; +Clues[] rowClues = +[ + new Clues(3), + new Clues(1, 1), + new Clues(1, 1), +]; + +var solvable = Solver.IsSolvable(columnClues, rowClues, out var solution); diff --git a/docs/examples/SavingLoading.cs b/docs/examples/SavingLoading.cs new file mode 100644 index 0000000..31e6f44 --- /dev/null +++ b/docs/examples/SavingLoading.cs @@ -0,0 +1,18 @@ +using NonoSharp; +Nonogram nonogram = Nonogram.CreateRandomPuzzle(5, 5); + +// Saves the puzzle solution to ./save_puzzle_example.ns +// (note that this might be in the bin folder when running the code) +// The title of the puzzle is "Example". This field can be left empty to not add a title +Console.WriteLine("Saving puzzle..."); +nonogram.SaveAsFile("save_puzzle_example.ns", "Example"); +Console.WriteLine("Puzzle successfully saved!"); +// Do not forget to catch the exceptions in your code! They have been removed in the example +// to save space + +// Loads the example_puzzle.ns file. Note that this might be loaded from the bin folder +// when running the code. +Console.WriteLine("Loading Example puzzle..."); +nonogram = Nonogram.LoadPuzzle("example_puzzle.ns"); +Console.WriteLine("Successfully loaded puzzle!"); +// Once again, do not forget to catch thrown exceptions. diff --git a/docs/examples/Solver.cs b/docs/examples/Solver.cs new file mode 100644 index 0000000..86ecef6 --- /dev/null +++ b/docs/examples/Solver.cs @@ -0,0 +1,16 @@ +using NonoSharp; + +Clues[] columnClues = +[ + new Clues(3), + new Clues(1), + new Clues(3), +]; +Clues[] rowClues = +[ + new Clues(3), + new Clues(1, 1), + new Clues(1, 1), +]; + +var solvable = Solver.IsSolvable(columnClues, rowClues, out var solution); diff --git a/src/NonoSharp/README.md b/src/NonoSharp/README.md index 18e3947..19eb086 100644 --- a/src/NonoSharp/README.md +++ b/src/NonoSharp/README.md @@ -28,6 +28,8 @@ Documentation for the public API of the library is found on GitHub pages for the ## Example usage ### Nonogram playing + + ```csharp using NonoSharp; @@ -72,6 +74,8 @@ nonogram.CellStateChanged += (s, e) => { ### Nonogram creation NonoSharp's `NonogramBuilder` class allows you to create your own Nonogram puzzles quickly and easily, while ensuring puzzles remain uniquely solvable. + + ```csharp using NonoSharp; @@ -92,6 +96,8 @@ if (builder.IsSolvable()) ### Solving Nonograms As mentioned, NonoSharp comes with a custom Nonogram solver. This can be used to solve any Nonogram puzzle. + + ```csharp using NonoSharp;