From d30f9f49542e339dcbe2e57fa39e72ff7c1f45c3 Mon Sep 17 00:00:00 2001 From: deeredman1991 Date: Mon, 28 Jun 2021 06:31:15 -0400 Subject: [PATCH 1/8] Update main.js --- main.js | 83 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 21 deletions(-) diff --git a/main.js b/main.js index 359984a..0fe8614 100644 --- a/main.js +++ b/main.js @@ -48,7 +48,7 @@ $(document).ready(function() { $("." + square).find(".txt").html(" "); }; - var tagSquare = (x, y, val, steps) => { + var tagSquare = (type, x, y, val, steps, color=0) => { var sq = null; if (x >= 0 && x < 8 && y >= 0 && y < 8) { sq = board[x][y]; @@ -62,63 +62,104 @@ $(document).ready(function() { $("." + sq).addClass("heat-" + val); tagged[sq] = val; if (steps > 0) { - return {x: x, y: y, val: ++val, steps: --steps}; + return {type: type, x: x, y: y, val: ++val, steps: --steps, color: color}; } } } } - var renderPath = (x, y, val, steps) => { + var renderPath = (type, x, y, val, steps, color=0) => { var next = []; + var move = 7; - // knight! - next.push(tagSquare(x+1,y-2,val,steps)); - next.push(tagSquare(x+1,y+2,val,steps)); - next.push(tagSquare(x-1,y-2,val,steps)); - next.push(tagSquare(x-1,y+2,val,steps)); - next.push(tagSquare(x+2,y-1,val,steps)); - next.push(tagSquare(x+2,y+1,val,steps)); - next.push(tagSquare(x-2,y-1,val,steps)); - next.push(tagSquare(x-2,y+1,val,steps)); + if ('K'.includes(type)) { + move = 1; + } + if ('N'.includes(type)) { + next.push(tagSquare(type,x+1,y-2,val,steps,color=color)); + next.push(tagSquare(type,x+1,y+2,val,steps,color=color)); + next.push(tagSquare(type,x-1,y-2,val,steps,color=color)); + next.push(tagSquare(type,x-1,y+2,val,steps,color=color)); + next.push(tagSquare(type,x+2,y-1,val,steps,color=color)); + next.push(tagSquare(type,x+2,y+1,val,steps,color=color)); + next.push(tagSquare(type,x-2,y-1,val,steps,color=color)); + next.push(tagSquare(type,x-2,y+1,val,steps,color=color)); + } + if ('KBQ'.includes(type)) { + for (var i=0; i<=move; i++) { + next.push(tagSquare(type,x-i,y-i,val,steps,color=color)); + next.push(tagSquare(type,x+i,y+i,val,steps,color=color)); + next.push(tagSquare(type,x-i,y+i,val,steps,color=color)); + next.push(tagSquare(type,x+i,y-i,val,steps,color=color)); + } + } + if ('KRQ'.includes(type)) { + for (var i=0; i<=move; i++) { + next.push(tagSquare(type,x,y-i,val,steps,color=color)); + next.push(tagSquare(type,x,y+i,val,steps,color=color)); + next.push(tagSquare(type,x-i,y,val,steps,color=color)); + next.push(tagSquare(type,x+i,y,val,steps,color=color)); + } + } + if ('P'.includes(type)) { + if (color == 0) { + move = 1 + if (x > 5) { + move = 2 + next.push(tagSquare(type,x-1,y,val,steps,color=color)); + } + } else if (color == 1) { + move = -1 + if (x < 2) { + move = -2 + next.push(tagSquare(type,x+1,y,val,steps,color=color)); + } + } + next.push(tagSquare(type,x-move,y,val,steps,color=color)); + + } next.forEach((sq) => { - if (sq) renderPath(sq.x, sq.y, sq.val, sq.steps); + if (sq) renderPath(sq.type, sq.x, sq.y, sq.val, sq.steps, color=sq.color); }); } - function refresh(sq) { + function refresh(type, sq, color=0) { console.log('REFRESH', sq); currentPos = sq; initBoard(); - initPiece('N', sq); + initPiece(type, sq); var pos = index[sq]; var x = pos[0]; var y = pos[1]; - renderPath(x, y, 1, 5); + renderPath(type, x, y, 1, 6,color=color); + var sprite = ['K','Q', 'B', 'N', 'R', 'P'].indexOf(type) + 1 + color * 6 + $('.piece').css("background-image", "url('img/sprites_" + sprite.toString().padStart(2, '0') + ".png')"); $("." + sq).find(".txt").addClass("hide").html(" "); console.log(tagged); } - function toggleBlock(sq) { + function toggleBlock(type, sq) { if (blocked[sq]) { blocked[sq] = false; } else { blocked[sq] = true; } console.log("BLOCKED", blocked); - refresh(currentPos); + refresh(type, currentPos); } function clickHandler(event) { + var type = 'K' var target = $(event.currentTarget); var id = target.find(".identifier").text(); if (event.which == 1) { - refresh(id); + refresh(type, id); } else { event.preventDefault(); - toggleBlock(id); + toggleBlock(type, id); } } - refresh('g1'); + refresh('N', 'g1'); }); From cd6b3ef20a751d20374dd9893f3630f4595a1771 Mon Sep 17 00:00:00 2001 From: deeredman1991 Date: Mon, 28 Jun 2021 06:50:28 -0400 Subject: [PATCH 2/8] Update styles.css Needed another heat class to fully support the King piece implementation. --- styles.css | 1 + 1 file changed, 1 insertion(+) diff --git a/styles.css b/styles.css index 1cb2d68..a335c3a 100644 --- a/styles.css +++ b/styles.css @@ -76,6 +76,7 @@ body { opacity: 1; } +.heat-7 { background-color: rgb(90, 0, 0) !important; } .heat-6 { background-color: rgb(150, 0, 0) !important; } .heat-5 { background-color: rgb(255, 50, 0) !important; } .heat-4 { background-color: rgb(255, 150, 0) !important; } From 42329f73caaa649e24071236f0928cc1c7d8c129 Mon Sep 17 00:00:00 2001 From: deeredman1991 Date: Mon, 28 Jun 2021 06:52:04 -0400 Subject: [PATCH 3/8] bugfix Fixed a bug that I introduced that made the Knight turn into a King. More information available in last commit. --- main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.js b/main.js index 0fe8614..3ad89c7 100644 --- a/main.js +++ b/main.js @@ -150,7 +150,7 @@ $(document).ready(function() { } function clickHandler(event) { - var type = 'K' + var type = 'N' var target = $(event.currentTarget); var id = target.find(".identifier").text(); if (event.which == 1) { From d23529a7aee7fd7792ffc6c3e3a7a1758ec3f652 Mon Sep 17 00:00:00 2001 From: deeredman1991 Date: Mon, 28 Jun 2021 22:24:39 -0400 Subject: [PATCH 4/8] Fixed bug with Rook and Bishop. Fixed an off by one error with the Rook and Bishop where it was tagging the square that they were standing on with a 1. Now tags square with a 2. --- main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index 3ad89c7..c0afe24 100644 --- a/main.js +++ b/main.js @@ -86,7 +86,7 @@ $(document).ready(function() { next.push(tagSquare(type,x-2,y+1,val,steps,color=color)); } if ('KBQ'.includes(type)) { - for (var i=0; i<=move; i++) { + for (var i=1; i<=move; i++) { next.push(tagSquare(type,x-i,y-i,val,steps,color=color)); next.push(tagSquare(type,x+i,y+i,val,steps,color=color)); next.push(tagSquare(type,x-i,y+i,val,steps,color=color)); @@ -94,7 +94,7 @@ $(document).ready(function() { } } if ('KRQ'.includes(type)) { - for (var i=0; i<=move; i++) { + for (var i=1; i<=move; i++) { next.push(tagSquare(type,x,y-i,val,steps,color=color)); next.push(tagSquare(type,x,y+i,val,steps,color=color)); next.push(tagSquare(type,x-i,y,val,steps,color=color)); From ec4a31012eacfc31cbc485e534bd3a217d359fe6 Mon Sep 17 00:00:00 2001 From: deeredman1991 Date: Mon, 28 Jun 2021 23:05:14 -0400 Subject: [PATCH 5/8] Code readability commit. Cleaned up some issues with tabs and spaces and added comments to better explain what is going on. --- main.js | 100 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/main.js b/main.js index c0afe24..2e574b1 100644 --- a/main.js +++ b/main.js @@ -72,59 +72,59 @@ $(document).ready(function() { var next = []; var move = 7; - if ('K'.includes(type)) { - move = 1; + if ('K'.includes(type)) { // if type is King + move = 1; } - if ('N'.includes(type)) { - next.push(tagSquare(type,x+1,y-2,val,steps,color=color)); - next.push(tagSquare(type,x+1,y+2,val,steps,color=color)); - next.push(tagSquare(type,x-1,y-2,val,steps,color=color)); - next.push(tagSquare(type,x-1,y+2,val,steps,color=color)); - next.push(tagSquare(type,x+2,y-1,val,steps,color=color)); - next.push(tagSquare(type,x+2,y+1,val,steps,color=color)); - next.push(tagSquare(type,x-2,y-1,val,steps,color=color)); - next.push(tagSquare(type,x-2,y+1,val,steps,color=color)); - } - if ('KBQ'.includes(type)) { - for (var i=1; i<=move; i++) { - next.push(tagSquare(type,x-i,y-i,val,steps,color=color)); - next.push(tagSquare(type,x+i,y+i,val,steps,color=color)); - next.push(tagSquare(type,x-i,y+i,val,steps,color=color)); - next.push(tagSquare(type,x+i,y-i,val,steps,color=color)); - } - } - if ('KRQ'.includes(type)) { - for (var i=1; i<=move; i++) { - next.push(tagSquare(type,x,y-i,val,steps,color=color)); - next.push(tagSquare(type,x,y+i,val,steps,color=color)); - next.push(tagSquare(type,x-i,y,val,steps,color=color)); - next.push(tagSquare(type,x+i,y,val,steps,color=color)); - } - } - if ('P'.includes(type)) { - - if (color == 0) { - move = 1 - if (x > 5) { - move = 2 - next.push(tagSquare(type,x-1,y,val,steps,color=color)); - } - } else if (color == 1) { - move = -1 - if (x < 2) { - move = -2 - next.push(tagSquare(type,x+1,y,val,steps,color=color)); - } - } - next.push(tagSquare(type,x-move,y,val,steps,color=color)); - + if ('N'.includes(type)) { // if type is a Knight + next.push(tagSquare(type,x+1,y-2,val,steps,color=color)); + next.push(tagSquare(type,x+1,y+2,val,steps,color=color)); + next.push(tagSquare(type,x-1,y-2,val,steps,color=color)); + next.push(tagSquare(type,x-1,y+2,val,steps,color=color)); + next.push(tagSquare(type,x+2,y-1,val,steps,color=color)); + next.push(tagSquare(type,x+2,y+1,val,steps,color=color)); + next.push(tagSquare(type,x-2,y-1,val,steps,color=color)); + next.push(tagSquare(type,x-2,y+1,val,steps,color=color)); + } + if ('KBQ'.includes(type)) { // if type is a King, Bishop, or Queen + for (var i=1; i<=move; i++) { // push diagonals + next.push(tagSquare(type,x-i,y-i,val,steps,color=color)); + next.push(tagSquare(type,x+i,y+i,val,steps,color=color)); + next.push(tagSquare(type,x-i,y+i,val,steps,color=color)); + next.push(tagSquare(type,x+i,y-i,val,steps,color=color)); + } + } + if ('KRQ'.includes(type)) { // if type is a King, Bishop or Queen + for (var i=1; i<=move; i++) { // push files and colomns + next.push(tagSquare(type,x,y-i,val,steps,color=color)); + next.push(tagSquare(type,x,y+i,val,steps,color=color)); + next.push(tagSquare(type,x-i,y,val,steps,color=color)); + next.push(tagSquare(type,x+i,y,val,steps,color=color)); + } + } + if ('P'.includes(type)) { // if type is a Pawn + if (color == 0) { // if white + move = 1 + if (x > 5) { // on the first movement + move = 2 // allow double movement + + next.push(tagSquare(type,x-1,y,val,steps,color=color)); // Puts a 1 directly in front of pawn. + } + } else if (color == 1) { // if black + move = -1 + if (x < 2) { // on the first movement + move = -2 // allow double movement + + next.push(tagSquare(type,x+1,y,val,steps,color=color)); // Puts a 1 directly in front of pawn. + } } + next.push(tagSquare(type,x-move,y,val,steps,color=color)); // Follows the double-step line. + } next.forEach((sq) => { - if (sq) renderPath(sq.type, sq.x, sq.y, sq.val, sq.steps, color=sq.color); + if (sq) renderPath(sq.type, sq.x, sq.y, sq.val, sq.steps, color=sq.color); }); } - function refresh(type, sq, color=0) { + function refresh(type, sq, color=0) { //Change color here. 0 for white 1 for black. Useful for pawns. console.log('REFRESH', sq); currentPos = sq; initBoard(); @@ -132,8 +132,8 @@ $(document).ready(function() { var pos = index[sq]; var x = pos[0]; var y = pos[1]; - renderPath(type, x, y, 1, 6,color=color); - var sprite = ['K','Q', 'B', 'N', 'R', 'P'].indexOf(type) + 1 + color * 6 + renderPath(type, x, y, 1, 6, color=color); + var sprite = ['-', 'K', 'Q', 'B', 'N', 'R', 'P'].indexOf(type) + color * 6 $('.piece').css("background-image", "url('img/sprites_" + sprite.toString().padStart(2, '0') + ".png')"); $("." + sq).find(".txt").addClass("hide").html(" "); console.log(tagged); @@ -150,7 +150,7 @@ $(document).ready(function() { } function clickHandler(event) { - var type = 'N' + var type = 'N' // Change pieces here 'K', 'Q', 'B', 'N', 'R', and 'P' are supported. var target = $(event.currentTarget); var id = target.find(".identifier").text(); if (event.which == 1) { From f65e331bdd6a9c0504c6e1c92607ed3d9ba0aa9c Mon Sep 17 00:00:00 2001 From: deeredman1991 Date: Mon, 28 Jun 2021 23:58:07 -0400 Subject: [PATCH 6/8] Fixed indentation issues. Fixed issues with indentation. Code now conforms to the rest of the file. --- main.js | 82 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/main.js b/main.js index 2e574b1..e530d4c 100644 --- a/main.js +++ b/main.js @@ -72,55 +72,57 @@ $(document).ready(function() { var next = []; var move = 7; - if ('K'.includes(type)) { // if type is King - move = 1; + if ('K'.includes(type)) { // if type is a King + move = 1; } if ('N'.includes(type)) { // if type is a Knight - next.push(tagSquare(type,x+1,y-2,val,steps,color=color)); - next.push(tagSquare(type,x+1,y+2,val,steps,color=color)); - next.push(tagSquare(type,x-1,y-2,val,steps,color=color)); - next.push(tagSquare(type,x-1,y+2,val,steps,color=color)); - next.push(tagSquare(type,x+2,y-1,val,steps,color=color)); - next.push(tagSquare(type,x+2,y+1,val,steps,color=color)); - next.push(tagSquare(type,x-2,y-1,val,steps,color=color)); - next.push(tagSquare(type,x-2,y+1,val,steps,color=color)); + next.push(tagSquare(type,x+1,y-2,val,steps,color=color)); + next.push(tagSquare(type,x+1,y+2,val,steps,color=color)); + next.push(tagSquare(type,x-1,y-2,val,steps,color=color)); + next.push(tagSquare(type,x-1,y+2,val,steps,color=color)); + next.push(tagSquare(type,x+2,y-1,val,steps,color=color)); + next.push(tagSquare(type,x+2,y+1,val,steps,color=color)); + next.push(tagSquare(type,x-2,y-1,val,steps,color=color)); + next.push(tagSquare(type,x-2,y+1,val,steps,color=color)); } if ('KBQ'.includes(type)) { // if type is a King, Bishop, or Queen - for (var i=1; i<=move; i++) { // push diagonals - next.push(tagSquare(type,x-i,y-i,val,steps,color=color)); - next.push(tagSquare(type,x+i,y+i,val,steps,color=color)); - next.push(tagSquare(type,x-i,y+i,val,steps,color=color)); - next.push(tagSquare(type,x+i,y-i,val,steps,color=color)); - } + for (var i=1; i<=move; i++) { // push diagonals + next.push(tagSquare(type,x-i,y-i,val,steps,color=color)); + next.push(tagSquare(type,x+i,y+i,val,steps,color=color)); + next.push(tagSquare(type,x-i,y+i,val,steps,color=color)); + next.push(tagSquare(type,x+i,y-i,val,steps,color=color)); + } } if ('KRQ'.includes(type)) { // if type is a King, Bishop or Queen - for (var i=1; i<=move; i++) { // push files and colomns - next.push(tagSquare(type,x,y-i,val,steps,color=color)); - next.push(tagSquare(type,x,y+i,val,steps,color=color)); - next.push(tagSquare(type,x-i,y,val,steps,color=color)); - next.push(tagSquare(type,x+i,y,val,steps,color=color)); - } + for (var i=1; i<=move; i++) { // push files and colomns + next.push(tagSquare(type,x,y-i,val,steps,color=color)); + next.push(tagSquare(type,x,y+i,val,steps,color=color)); + next.push(tagSquare(type,x-i,y,val,steps,color=color)); + next.push(tagSquare(type,x+i,y,val,steps,color=color)); + } } if ('P'.includes(type)) { // if type is a Pawn - if (color == 0) { // if white - move = 1 - if (x > 5) { // on the first movement - move = 2 // allow double movement - - next.push(tagSquare(type,x-1,y,val,steps,color=color)); // Puts a 1 directly in front of pawn. - } - } else if (color == 1) { // if black - move = -1 - if (x < 2) { // on the first movement - move = -2 // allow double movement - - next.push(tagSquare(type,x+1,y,val,steps,color=color)); // Puts a 1 directly in front of pawn. - } - } - next.push(tagSquare(type,x-move,y,val,steps,color=color)); // Follows the double-step line. + if (color == 0) { // if white + move = 1 + if (x > 5) { // on the first movement + move = 2 // allow double movement + + next.push(tagSquare(type,x-1,y,val,steps,color=color)); // Puts a 1 directly in front of pawn. + } + } else if (color == 1) { // if black + move = -1 + if (x < 2) { // on the first movement + move = -2 // allow double movement + + next.push(tagSquare(type,x+1,y,val,steps,color=color)); // Puts a 1 directly in front of pawn. + } + } + + next.push(tagSquare(type,x-move,y,val,steps,color=color)); // Follows the double-step line. } - next.forEach((sq) => { - if (sq) renderPath(sq.type, sq.x, sq.y, sq.val, sq.steps, color=sq.color); + + next.forEach((sq) => { // Recursively calls renderPath if there are viable squares in next to move to. + if (sq) renderPath(sq.type, sq.x, sq.y, sq.val, sq.steps, color=sq.color); }); } From 2c1cc0591d4dafe2890dbc071f7eebcc8a648378 Mon Sep 17 00:00:00 2001 From: deeredman1991 Date: Mon, 28 Jun 2021 23:59:55 -0400 Subject: [PATCH 7/8] Last (Hopefully) indentation fix. Found another indentation issue. --- main.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/main.js b/main.js index e530d4c..23fae40 100644 --- a/main.js +++ b/main.js @@ -76,14 +76,14 @@ $(document).ready(function() { move = 1; } if ('N'.includes(type)) { // if type is a Knight - next.push(tagSquare(type,x+1,y-2,val,steps,color=color)); - next.push(tagSquare(type,x+1,y+2,val,steps,color=color)); - next.push(tagSquare(type,x-1,y-2,val,steps,color=color)); - next.push(tagSquare(type,x-1,y+2,val,steps,color=color)); - next.push(tagSquare(type,x+2,y-1,val,steps,color=color)); - next.push(tagSquare(type,x+2,y+1,val,steps,color=color)); - next.push(tagSquare(type,x-2,y-1,val,steps,color=color)); - next.push(tagSquare(type,x-2,y+1,val,steps,color=color)); + next.push(tagSquare(type,x+1,y-2,val,steps,color=color)); + next.push(tagSquare(type,x+1,y+2,val,steps,color=color)); + next.push(tagSquare(type,x-1,y-2,val,steps,color=color)); + next.push(tagSquare(type,x-1,y+2,val,steps,color=color)); + next.push(tagSquare(type,x+2,y-1,val,steps,color=color)); + next.push(tagSquare(type,x+2,y+1,val,steps,color=color)); + next.push(tagSquare(type,x-2,y-1,val,steps,color=color)); + next.push(tagSquare(type,x-2,y+1,val,steps,color=color)); } if ('KBQ'.includes(type)) { // if type is a King, Bishop, or Queen for (var i=1; i<=move; i++) { // push diagonals From 4878bc662e1d64774d3908edd9b3878dfc426915 Mon Sep 17 00:00:00 2001 From: deeredman1991 Date: Tue, 29 Jun 2021 00:06:54 -0400 Subject: [PATCH 8/8] Fixed comment on line 96 --- main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.js b/main.js index 23fae40..9277208 100644 --- a/main.js +++ b/main.js @@ -93,7 +93,7 @@ $(document).ready(function() { next.push(tagSquare(type,x+i,y-i,val,steps,color=color)); } } - if ('KRQ'.includes(type)) { // if type is a King, Bishop or Queen + if ('KRQ'.includes(type)) { // if type is a King, Rook or Queen for (var i=1; i<=move; i++) { // push files and colomns next.push(tagSquare(type,x,y-i,val,steps,color=color)); next.push(tagSquare(type,x,y+i,val,steps,color=color));