diff --git a/src/NonoSharp/Grid.cs b/src/NonoSharp/Grid.cs
index c9e827b..b36394c 100644
--- a/src/NonoSharp/Grid.cs
+++ b/src/NonoSharp/Grid.cs
@@ -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)]));
+ }
+ }
+
+ ///
+ /// Changes the cell at (x, y) to .
+ ///
+ /// true if the cell was changed, false otherwise.
+ 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;
}
///
@@ -199,13 +213,16 @@ internal void SetRow(int row, CellType[] newRow)
List 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)
@@ -221,13 +238,16 @@ internal void SetColumn(int column, CellType[] newColumn)
List 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));
+ }
}
///
@@ -308,13 +328,10 @@ internal LinkedList 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)]));
+ }
}
}
diff --git a/src/NonoSharp/MoveManager.cs b/src/NonoSharp/MoveManager.cs
index 1de56bf..b276817 100644
--- a/src/NonoSharp/MoveManager.cs
+++ b/src/NonoSharp/MoveManager.cs
@@ -31,6 +31,11 @@ internal MoveManager(NonogramOptions? options, Puzzle puzzle)
/// List of CellPositions of the cells that were changed.
public List 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;
diff --git a/src/NonoSharp/Nonogram.cs b/src/NonoSharp/Nonogram.cs
index d44fc26..54833d8 100644
--- a/src/NonoSharp/Nonogram.cs
+++ b/src/NonoSharp/Nonogram.cs
@@ -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));
}
///
diff --git a/tests/NonoSharp.Tests/GridTests.cs b/tests/NonoSharp.Tests/GridTests.cs
index e488c5b..96d028e 100644
--- a/tests/NonoSharp.Tests/GridTests.cs
+++ b/tests/NonoSharp.Tests/GridTests.cs
@@ -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())
+ {
+ grid.SetCell(0, 0, type);
+ eventFired = false;
+ grid.SetCell(0, 0, type);
+ Assert.False(eventFired);
+ }
+ }
}
}
diff --git a/tests/NonoSharp.Tests/NonogramEventsTests.cs b/tests/NonoSharp.Tests/NonogramEventsTests.cs
index dfd6e91..90da4bd 100644
--- a/tests/NonoSharp.Tests/NonogramEventsTests.cs
+++ b/tests/NonoSharp.Tests/NonogramEventsTests.cs
@@ -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())
+ {
+ nonogram.SetCell(0, 0, type);
+ eventFired = false;
+ nonogram.SetCell(0, 0, type);
+ Assert.False(eventFired);
+ }
+ }
}
}