From 64adc9b841d20e95c8d35fceda3bd90300ab9464 Mon Sep 17 00:00:00 2001 From: Lily Chung Date: Tue, 29 Aug 2023 19:06:57 -0400 Subject: [PATCH 1/3] Move shared code from slither and myopia into Board --- src/puzzle/Board.js | 21 +++++++++++++++++++++ src/variety/myopia.js | 24 ------------------------ src/variety/slither.js | 24 ------------------------ 3 files changed, 21 insertions(+), 48 deletions(-) diff --git a/src/puzzle/Board.js b/src/puzzle/Board.js index 4f621f81d..5ba585915 100644 --- a/src/puzzle/Board.js +++ b/src/puzzle/Board.js @@ -898,6 +898,27 @@ pzpr.classmgr.makeCommon({ } } return dlist; + }, + + // Draw lines on borders where the adjacent cells have inequal, nonzero aux shades; + // erase lines where they have equal nonzero aux shades. + // Cells outside the grid are treated as having aux shade 2. + // Used by slither and myopia. + outlineShaded: function() { + this.border.each(function(b) { + var c0 = b.sidecell[0], + c1 = b.sidecell[1]; + var qsub1 = c0.isnull ? 2 : c0.qsub; + var qsub2 = c1.isnull ? 2 : c1.qsub; + if (qsub1 === 0 || qsub2 === 0) { + return; + } + if (qsub1 === qsub2) { + b.setLineVal(0); + } else { + b.setLine(); + } + }); } } }); diff --git a/src/variety/myopia.js b/src/variety/myopia.js index 1215c08bc..6e5fbebd1 100644 --- a/src/variety/myopia.js +++ b/src/variety/myopia.js @@ -180,30 +180,6 @@ this.common.operate.call(this, type); break; } - }, - - outlineShaded: function() { - this.border.each(function(border) { - border.updateShaded(); - }); - } - }, - - Border: { - updateShaded: function() { - var c0 = this.sidecell[0], - c1 = this.sidecell[1]; - var qsub1 = c0.isnull ? 2 : c0.qsub; - var qsub2 = c1.isnull ? 2 : c1.qsub; - if (qsub1 === 0 || qsub2 === 0) { - return; - } - if (qsub1 === qsub2) { - this.setLineVal(0); - } else { - this.setLine(); - } - this.draw(); } }, diff --git a/src/variety/slither.js b/src/variety/slither.js index 1f10e09c3..f8494ab1a 100644 --- a/src/variety/slither.js +++ b/src/variety/slither.js @@ -106,30 +106,6 @@ this.common.operate.call(this, type); break; } - }, - - outlineShaded: function() { - this.border.each(function(border) { - border.updateShaded(); - }); - } - }, - - Border: { - updateShaded: function() { - var c0 = this.sidecell[0], - c1 = this.sidecell[1]; - var qsub1 = c0.isnull ? 2 : c0.qsub; - var qsub2 = c1.isnull ? 2 : c1.qsub; - if (qsub1 === 0 || qsub2 === 0) { - return; - } - if (qsub1 === qsub2) { - this.setLineVal(0); - } else { - this.setLine(); - } - this.draw(); } }, From 940058cd9df1316c873f80f05b1368b491ef1890 Mon Sep 17 00:00:00 2001 From: Lily Chung Date: Tue, 29 Aug 2023 19:33:25 -0400 Subject: [PATCH 2/3] remove redundant myopia test --- test/variety/myopia_test.js | 50 ------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 test/variety/myopia_test.js diff --git a/test/variety/myopia_test.js b/test/variety/myopia_test.js deleted file mode 100644 index 3c111cad7..000000000 --- a/test/variety/myopia_test.js +++ /dev/null @@ -1,50 +0,0 @@ -// test/variety/myopia_test.js - -var assert = require("assert"); - -var pzpr = require("../../"); - -var puzzle = new pzpr.Puzzle(); - -describe("Variety:myopia", function() { - it("Outline shaded cells functions correctly", function() { - puzzle.open("myopia/3/3/g6j1h"); - puzzle.setMode("play"); - puzzle.mouse.setInputMode("auto"); - puzzle.mouse.inputPath("left", 0, 2, 0, 0, 2, 0, 2, 2, 4, 2); - puzzle.mouse.inputPath("left", 0, 4, 2, 4, 2, 6, 0, 6); - puzzle.mouse.inputPath("left", 1, 2); - puzzle.mouse.inputPath("left", 3, 4); - puzzle.mouse.inputPath("left", 6, 1); - puzzle.mouse.inputPath("left", 6, 3); - puzzle.mouse.setInputMode("bgcolor1"); - puzzle.mouse.inputPath("left", 1, 1, 1, 3, 5, 3, 5, 5); - puzzle.mouse.setInputMode("bgcolor2"); - puzzle.mouse.inputPath("left", 1, 5, 3, 5); - puzzle.mouse.inputPath("left", 5, 1); - puzzle.board.operate("outlineshaded"); - - var bd = puzzle.board.freezecopy(); - - puzzle.open("myopia/3/3/g6j1h"); - puzzle.setMode("play"); - puzzle.mouse.setInputMode("auto"); - puzzle.mouse.inputPath("left", 0, 0, 2, 0, 2, 2, 6, 2, 6, 6); - puzzle.mouse.inputPath("left", 6, 6, 4, 6, 4, 4, 0, 4, 0, 0); - puzzle.mouse.inputPath("left", 1, 2); - puzzle.mouse.inputPath("left", 6, 1); - puzzle.mouse.setInputMode("bgcolor1"); - puzzle.mouse.inputPath("left", 1, 1, 1, 3, 5, 3, 5, 5); - puzzle.mouse.setInputMode("bgcolor2"); - puzzle.mouse.inputPath("left", 1, 5, 3, 5); - puzzle.mouse.inputPath("left", 5, 1); - - puzzle.board.compareData(bd, function(group, c, a) { - assert.equal( - bd[group][c][a], - puzzle.board[group][c][a], - group + "[" + c + "]." + a - ); - }); - }); -}); From e84434ff219035d2192b1b5a8a53ab88de611e57 Mon Sep 17 00:00:00 2001 From: Lily Chung Date: Tue, 29 Aug 2023 19:52:41 -0400 Subject: [PATCH 3/3] autodetect exterior shade for outline shaded cells --- src/puzzle/Board.js | 36 ++++++++++++++++++++---- test/variety/slither_test.js | 54 +++++++++++++++--------------------- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/puzzle/Board.js b/src/puzzle/Board.js index 5ba585915..9cddb8cb9 100644 --- a/src/puzzle/Board.js +++ b/src/puzzle/Board.js @@ -900,16 +900,40 @@ pzpr.classmgr.makeCommon({ return dlist; }, - // Draw lines on borders where the adjacent cells have inequal, nonzero aux shades; - // erase lines where they have equal nonzero aux shades. - // Cells outside the grid are treated as having aux shade 2. + // Draw borders between opposite-shaded cells. + // A cell is shaded if its qsub value is nonzero; unshaded cells are ignored. + // A line is drawn between two shaded cells with opposite shades, + // or erased between shaded cells with the same shade. + // "Cells" outside the grid are treated as having shade 2, + // unless there is a cross mark between a shaded cell and the exterior, in which case that cell's shade is used as the exterior shade. + // // Used by slither and myopia. outlineShaded: function() { + // determine the exterior shade + var exteriorShade = 2; + for (var i = 0; i < this.border.length; i++) { + var b = this.border[i]; + if (b.qsub !== 2) { + continue; + } + var c0 = b.sidecell[0], + c1 = b.sidecell[1]; + if (c0.isnull && c1.qsub !== 0) { + exteriorShade = c1.qsub; + break; + } + if (c1.isnull && c0.qsub !== 0) { + exteriorShade = c0.qsub; + break; + } + } + + // draw borders this.border.each(function(b) { var c0 = b.sidecell[0], c1 = b.sidecell[1]; - var qsub1 = c0.isnull ? 2 : c0.qsub; - var qsub2 = c1.isnull ? 2 : c1.qsub; + var qsub1 = c0.isnull ? exteriorShade : c0.qsub; + var qsub2 = c1.isnull ? exteriorShade : c1.qsub; if (qsub1 === 0 || qsub2 === 0) { return; } @@ -919,6 +943,8 @@ pzpr.classmgr.makeCommon({ b.setLine(); } }); + + this.puzzle.redraw(); } } }); diff --git a/test/variety/slither_test.js b/test/variety/slither_test.js index c960d4742..1b935b057 100644 --- a/test/variety/slither_test.js +++ b/test/variety/slither_test.js @@ -8,40 +8,32 @@ var puzzle = new pzpr.Puzzle(); describe("Variety:slither", function() { it("Outline shaded cells functions correctly", function() { - puzzle.open("slither/2/4/dbh"); - puzzle.setMode("play"); - puzzle.mouse.setInputMode("auto"); - puzzle.mouse.inputPath("left", 4, 0, 4, 2); - puzzle.mouse.inputPath("left", 2, 4, 4, 4); - puzzle.mouse.inputPath("left", 2, 6, 4, 6); - puzzle.mouse.inputPath("left", 0, 8, 4, 8); - puzzle.mouse.inputPath("left", 2, 3); - puzzle.mouse.inputPath("left", 2, 5); - puzzle.mouse.inputPath("left", 1, 4); - puzzle.mouse.inputPath("left", 1, 6); - puzzle.mouse.setInputMode("bgcolor1"); - puzzle.mouse.inputPath("left", 1, 1, 1, 7); - puzzle.mouse.setInputMode("bgcolor2"); - puzzle.mouse.inputPath("left", 3, 1); - puzzle.mouse.inputPath("left", 3, 5); - + puzzle.open( + "pzprv3.1/slither/4/2/3 . /. 1 /. . /. . /1 2 /1 0 /1 2 /1 0 /0 0 1 /0 -1 0 /0 -1 0 /0 0 0 /0 0 /0 0 /-1 1 /-1 1 /1 1 //" + ); puzzle.board.operate("outlineshaded"); var bd = puzzle.board.freezecopy(); + puzzle.open( + "pzprv3.1/slither/4/2/3 . /. 1 /. . /. . /1 2 /1 0 /1 2 /1 0 /1 1 0 /1 -1 0 /1 1 0 /1 0 0 /1 0 /0 0 /-1 1 /-1 1 /1 1 //" + ); + puzzle.board.compareData(bd, function(group, c, a) { + assert.equal( + bd[group][c][a], + puzzle.board[group][c][a], + group + "[" + c + "]." + a + ); + }); + }); - puzzle.open("slither/2/4/dbh"); - puzzle.setMode("play"); - puzzle.mouse.setInputMode("auto"); - puzzle.mouse.inputPath("left", 2, 2, 2, 0, 0, 0, 0, 8, 4, 8); - puzzle.mouse.inputPath("left", 4, 4, 2, 4, 2, 6, 4, 6); - puzzle.mouse.inputPath("left", 2, 3); - puzzle.mouse.inputPath("left", 1, 4); - puzzle.mouse.inputPath("left", 1, 6); - puzzle.mouse.setInputMode("bgcolor1"); - puzzle.mouse.inputPath("left", 1, 1, 1, 7); - puzzle.mouse.setInputMode("bgcolor2"); - puzzle.mouse.inputPath("left", 3, 1); - puzzle.mouse.inputPath("left", 3, 5); - + it("Outline shaded cells autodetects exterior shade", function() { + puzzle.open( + "pzprv3.1/slither/4/2/3 . /. 1 /. . /. . /1 2 /1 0 /1 2 /1 0 /0 0 1 /0 -1 0 /0 -1 0 /0 0 0 /-1 0 /0 0 /-1 1 /-1 1 /1 1 //" + ); + puzzle.board.operate("outlineshaded"); + var bd = puzzle.board.freezecopy(); + puzzle.open( + "pzprv3.1/slither/4/2/3 . /. 1 /. . /. . /1 2 /1 0 /1 2 /1 0 /0 1 1 /0 -1 0 /0 1 1 /0 0 0 /-1 1 /0 0 /-1 1 /-1 1 /0 1 //" + ); puzzle.board.compareData(bd, function(group, c, a) { assert.equal( bd[group][c][a],