Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/validate-examples.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
5 changes: 4 additions & 1 deletion NonoSharp.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
<BuildType Name="Release" />
<BuildType Name="Tests" />
</Configurations>
<Project Path="docs/examples/NonoSharp.ExampleValidator/NonoSharp.ExampleValidator.csproj" Id="5430789a-3d6a-4d7e-84b6-6aa802ab1529">
<BuildType Solution="Tests|*" Project="Debug" />
</Project>
<Project Path="src/NonoSharp/NonoSharp.csproj" />
<Project Path="tests/NonoSharp.Tests/NonoSharp.Tests.csproj" Id="74a9d335-a696-4a68-86e8-a69356e548e2">
<Build Solution="Release|*" Project="false" />
</Project>
<Project Path="src/NonoSharp/NonoSharp.csproj" />
</Solution>
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- WHEN EDIING THIS CODE BLOCK, ALSO EDIT docs/examples/README_basic.cs -->
```csharp
using NonoSharp;

Expand Down Expand Up @@ -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.

<!-- WHEN EDIING THIS CODE BLOCK, ALSO EDIT docs/examples/README_builder.cs -->
```csharp
using NonoSharp;

Expand All @@ -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.

<!-- WHEN EDIING THIS CODE BLOCK, ALSO EDIT docs/examples/README_solver.cs -->
```csharp
using NonoSharp;

Expand Down
92 changes: 6 additions & 86 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


<img src="solverExample.png" alt="Puzzle to solve" width="300"/>


```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);
```
[!code-csharp[](examples/Solver.cs)]
6 changes: 6 additions & 0 deletions docs/examples/BasicOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using NonoSharp;

Nonogram nonogram = Nonogram.CreateRandomPuzzle(5, 10);
nonogram.FillCell(0, 0);
nonogram.CrossCell(0, 0);
nonogram.EmptyCell(0, 0);
15 changes: 15 additions & 0 deletions docs/examples/Builder.cs
Original file line number Diff line number Diff line change
@@ -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");
18 changes: 18 additions & 0 deletions docs/examples/Events.cs
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
140 changes: 140 additions & 0 deletions docs/examples/NonoSharp.ExampleValidator/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}

/// <summary>
/// Sets up the required csproj file to build an example.
/// </summary>
/// <param name="projectPath">The path to create the project in</param>
/// <param name="examplePath">The path of the .cs file of the example from this repo's root</param>
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<string>();
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;
}
Loading
Loading