Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outline shaded cells: autodetect exterior shade #489

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions src/puzzle/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,53 @@ pzpr.classmgr.makeCommon({
}
}
return dlist;
},

// 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like this part of the comment to be rewritten. The exterior shade should be treated as the more common case, with the default value of 2 mentioned afterwards. Also, line 908 is currently too long. (we don't have strict lint rules for this, but it currently sticks out compared to the rest)

//
// 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 ? exteriorShade : c0.qsub;
var qsub2 = c1.isnull ? exteriorShade : c1.qsub;
if (qsub1 === 0 || qsub2 === 0) {
return;
}
if (qsub1 === qsub2) {
b.setLineVal(0);
} else {
b.setLine();
}
});

this.puzzle.redraw();
}
}
});
24 changes: 0 additions & 24 deletions src/variety/myopia.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
},

Expand Down
24 changes: 0 additions & 24 deletions src/variety/slither.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
},

Expand Down
50 changes: 0 additions & 50 deletions test/variety/myopia_test.js

This file was deleted.

54 changes: 23 additions & 31 deletions test/variety/slither_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Loading