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
51 changes: 34 additions & 17 deletions src/NonoSharp/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,31 @@ public void SetCell(int x, int y, CellType value)
{
ValidateInputCoordinates(x, y);

if (grid[x, y] != CellType.Filled && value == CellType.Filled)
if (ChangeCell(x, y, value))
{
OnCellStateChanged(new([new(x, y)]));
}
}

/// <summary>
/// Changes the cell at (x, y) to <paramref name="newType"/>.
/// </summary>
/// <returns><c>true</c> if the cell was changed, <c>false</c> otherwise.</returns>
private bool ChangeCell(int x, int y, CellType newType)
{
if (grid[x, y] == newType) return false;

if (grid[x, y] != CellType.Filled && newType == CellType.Filled)
{
Filled++; // Keeps track of whether the same amount of cells are filled as the solution for efficiency
}
else if (grid[x, y] == CellType.Filled && value != CellType.Filled)
else if (grid[x, y] == CellType.Filled && newType != CellType.Filled)
{
Filled--;
}

grid[x, y] = value;
OnCellStateChanged(new([new(x, y)]));
grid[x, y] = newType;
return true;
}

/// <summary>
Expand Down Expand Up @@ -199,13 +213,16 @@ internal void SetRow(int row, CellType[] newRow)
List<CellPosition> changed = [];
for (int i = 0; i < Width; i++)
{
if (newRow[i] != this[i, row])
if (ChangeCell(i, row, newRow[i]))
{
grid[i, row] = newRow[i];
changed.Add(new(i, row));
}
}
OnCellStateChanged(new(changed));

if (changed.Count > 0)
{
OnCellStateChanged(new(changed));
}
}

internal void SetColumn(int column, CellType[] newColumn)
Expand All @@ -221,13 +238,16 @@ internal void SetColumn(int column, CellType[] newColumn)
List<CellPosition> changed = [];
for (int i = 0; i < Height; i++)
{
if (newColumn[i] != this[column, i])
if (ChangeCell(column, i, newColumn[i]))
{
grid[column, i] = newColumn[i];
changed.Add(new(column, i));
}
}
OnCellStateChanged(new(changed));

if (changed.Count > 0)
{
OnCellStateChanged(new(changed));
}
}

/// <summary>
Expand Down Expand Up @@ -308,13 +328,10 @@ internal LinkedList<int> GetGroupsInColumn(int col)
}
set
{
if (grid[x, y] == value) return;
if (grid[x, y] == CellType.Filled) Filled--;

grid[x, y] = value;

if (value == CellType.Filled) Filled++;
OnCellStateChanged(new([new(x, y)]));
if (ChangeCell(x, y, value))
{
OnCellStateChanged(new([new(x, y)]));
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/NonoSharp/MoveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ internal MoveManager(NonogramOptions? options, Puzzle puzzle)
/// <returns>List of CellPositions of the cells that were changed.</returns>
public List<CellPosition> DoMove(int x, int y, CellType newType)
{
if (puzzle.Grid.GetCell(x, y) == newType)
{
return [];
}

newType = DoAutoCorrect(x, y, newType);
// Check if auto-crosses are possible (i.e. a cell goes to filled or from filled)
bool mightAutoCross = newType == CellType.Filled || puzzle.Grid.GetCell(x, y) == CellType.Filled;
Expand Down
3 changes: 2 additions & 1 deletion src/NonoSharp/Nonogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ public void EmptyCell(CellPosition position)
public void SetCell(int x, int y, CellType newCellType)
{
var changes = moveManager.DoMove(x, y, newCellType);
OnCellStateChanged(new(changes));

if (changes.Count > 0) OnCellStateChanged(new(changes));
}

/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions tests/NonoSharp.Tests/GridTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,21 @@ public void TestGroups()
Assert.Equal(1, groups.First());
Assert.Equal(2, groups.Last());
}

[Fact]
public void TestSettingCellToSameStateDoesNotFireEvent()
{
Grid grid = new(1, 1);
bool eventFired = false;
grid.CellStateChanged += (s, e) => eventFired = true;

foreach (CellType type in Enum.GetValues<CellType>())
{
grid.SetCell(0, 0, type);
eventFired = false;
grid.SetCell(0, 0, type);
Assert.False(eventFired);
}
}
}
}
16 changes: 16 additions & 0 deletions tests/NonoSharp.Tests/NonogramEventsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,21 @@ public void TestResetEvent()
Assert.Equal(0, cells[0].X);
Assert.Equal(0, cells[0].Y);
}

[Fact]
public void TestSettingCellToSameStateDoesNotFireEvent()
{
Nonogram nonogram = new(new Puzzle(1, 1, null));
bool eventFired = false;
nonogram.CellStateChanged += (s, e) => eventFired = true;

foreach (CellType type in Enum.GetValues<CellType>())
{
nonogram.SetCell(0, 0, type);
eventFired = false;
nonogram.SetCell(0, 0, type);
Assert.False(eventFired);
}
}
}
}
Loading