Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HTMLDivElement.prototype.findLegalMoves = function() {
- var legalMoves = []; // Make the array
- var enemy = (this.side === 1)?2:1;
- switch (this.type) { // Determine which one to use
- case "F": // Flag
- var diff = (this.side === 1) ? 1 : -1; //Check south if black, north if white
- /** General Moveset
- * ===============
- * A Flag can move one square forwards, without capturing.
- */
- if (seeContents(this.xpos, this.ypos + diff) === 0) {
- //If one square ahead is empty
- legalMoves.push([this.xpos, this.ypos + diff]);
- }
- /** Capture choice
- * ===============
- * Once per game per Flag, it may capture an ememy piece.
- */
- if (seeContents(this.xpos, this.ypos + diff) === enemy && this.flagHasCaptured === false) {
- //If the one on the north/southeast square is an enemy piece and it has not taken a piece yet
- legalMoves.push([this.xpos, this.ypos + diff]);
- }
- /** Diagonal choice
- * ===============
- * Once per game per Flag, it can move along the diagonal lines.
- * They can do so in any direction.
- */
- if (this.flagHasDiagonal === false) {
- if (this.xpos === 4) { //File T
- if (seeContents(5, this.ypos + 1) === 0 || ((seeContents(5, this.ypos + 1) === enemy && this.flagHasCaptured === false))) {
- console.log("Diagonal square northeast is empty OR Diag sq. NE contains enemy piece AND Flag has not captured yet");
- legalMoves.push([5, this.ypos + 1]);
- }
- if (seeContents(5, this.ypos - 1) === 0 || ((seeContents(5, this.ypos - 1) === enemy && this.flagHasCaptured === false))) {
- console.log("Diagonal square southeast is empty OR (contains enemy piece AND Flag has not captured yet)");
- legalMoves.push([5, this.ypos - 1]);
- }
- }
- else if (this.xpos === 5) { //File Œ
- if (seeContents(4, this.ypos + 1) === 0 || ((seeContents(4, this.ypos + 1) === enemy && this.flagHasCaptured === false))) {
- console.log("Diagonal square northwest is empty OR (contains enemy piece AND Flag has not captured yet)");
- legalMoves.push([4, this.ypos + 1]);
- }
- if (seeContents(4, this.ypos - 1) === 0 || ((seeContents(4, this.ypos - 1) === enemy && this.flagHasCaptured === false))) {
- console.log("Diagonal square southwest is empty OR (contains enemy piece AND Flag has not captured yet)");
- legalMoves.push([4, this.ypos - 1]);
- }
- }
- // Else it has no legal moves. A Flag outside of files T and Œ is stranded.
- }
- break;
- case "R": // Rook
- var currpos = [this.xpos, this.ypos]
- var distance = 0;
- //Move north!
- while (true) {
- currpos[1] = currpos[1] + 1; // Move tester one square north
- if (seeContents(currpos[0], currpos[1]) === 0) {
- console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
- legalMoves.push([currpos[0], currpos[1]]);
- }
- else if (seeContents(currpos[0], currpos[1]) === enemy) {
- console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
- legalMoves.push([currpos[0], currpos[1]]);
- break;
- }
- else {
- console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
- break;
- }
- distance = distance + 1;
- if (distance === 6) {
- console.log("Six squares have passed. Stopping.")
- break;
- }
- }
- currpos = [this.xpos, this.ypos];
- distance = 0;
- // Move East!
- while (true) {
- currpos[0] = currpos[0] + 1; // Move tester one square east
- if (seeContents(currpos[0], currpos[1]) === 0) {
- console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
- legalMoves.push([currpos[0], currpos[1]]);
- }
- else if (seeContents(currpos[0], currpos[1]) === enemy) {
- console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
- legalMoves.push([currpos[0], currpos[1]]);
- break;
- }
- else {
- console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
- break;
- }
- distance = distance + 1;
- if (distance === 6) {
- console.log("Six squares have passed. Stopping.")
- break;
- }
- }
- currpos = [this.xpos, this.ypos];
- distance = 0;
- // Move South!
- while (true) {
- currpos[1] = currpos[1] - 1; // Move tester one square south
- if (seeContents(currpos[0], currpos[1]) === 0) {
- console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
- legalMoves.push([currpos[0], currpos[1]]);
- }
- else if (seeContents(currpos[0], currpos[1]) === enemy) {
- console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
- legalMoves.push([currpos[0], currpos[1]]);
- break;
- }
- else {
- console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
- break;
- }
- distance = distance + 1;
- if (distance === 6) {
- console.log("Six squares have passed. Stopping.")
- break;
- }
- }
- currpos = [this.xpos, this.ypos];
- distance = 0;
- // Move West!
- while (true) {
- currpos[0] = currpos[0] - 1; // Move tester one square west
- if (seeContents(currpos[0], currpos[1]) === 0) {
- console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
- legalMoves.push([currpos[0], currpos[1]]);
- }
- else if (seeContents(currpos[0], currpos[1]) === enemy) {
- console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
- legalMoves.push([currpos[0], currpos[1]]);
- break;
- }
- else {
- console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
- break;
- }
- distance = distance + 1;
- if (distance === 6) {
- console.log("Six squares have passed. Stopping.")
- break;
- }
- }
- break;
- case "B": // Bishop
- var currpos = [this.xpos, this.ypos]
- var distance = 0;
- //Move northeast!
- while (true) {
- currpos[1] = currpos[1] + 1; // Move tester one square north
- currpos[0] = currpos[0] + 1; // Move tester one square east
- if (seeContents(currpos[0], currpos[1]) === 0) {
- console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
- legalMoves.push([currpos[0], currpos[1]]);
- }
- else if (seeContents(currpos[0], currpos[1]) === enemy) {
- console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
- legalMoves.push([currpos[0], currpos[1]]);
- break;
- }
- else {
- console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
- break;
- }
- distance = distance + 1;
- if (distance === 7) {
- console.log("Seven squares have passed. Stopping.")
- break;
- }
- }
- currpos = [this.xpos, this.ypos];
- distance = 0;
- // Move Southeast!
- while (true) {
- currpos[1] = currpos[1] - 1; // Move tester one square south
- currpos[0] = currpos[0] + 1; // Move tester one square east
- if (seeContents(currpos[0], currpos[1]) === 0) {
- console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
- legalMoves.push([currpos[0], currpos[1]]);
- }
- else if (seeContents(currpos[0], currpos[1]) === enemy) {
- console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
- legalMoves.push([currpos[0], currpos[1]]);
- break;
- }
- else {
- console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
- break;
- }
- distance = distance + 1;
- if (distance === 7) {
- console.log("Seven squares have passed. Stopping.")
- break;
- }
- }
- currpos = [this.xpos, this.ypos];
- distance = 0;
- // Move Southwest!
- while (true) {
- currpos[1] = currpos[1] - 1; // Move tester one square south
- currpos[0] = currpos[0] - 1; // Move tester one square west
- if (seeContents(currpos[0], currpos[1]) === 0) {
- console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
- legalMoves.push([currpos[0], currpos[1]]);
- }
- else if (seeContents(currpos[0], currpos[1]) === enemy) {
- console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
- legalMoves.push([currpos[0], currpos[1]]);
- break;
- }
- else {
- console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
- break;
- }
- distance = distance + 1;
- if (distance === 7) {
- console.log("Seven squares have passed. Stopping.")
- break;
- }
- }
- currpos = [this.xpos, this.ypos];
- distance = 0;
- // Move Northwest!
- while (true) {
- currpos[1] = currpos[1] + 1; // Move tester one square north
- currpos[0] = currpos[0] - 1; // Move tester one square west
- if (seeContents(currpos[0], currpos[1]) === 0) {
- console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
- legalMoves.push([currpos[0], currpos[1]]);
- }
- else if (seeContents(currpos[0], currpos[1]) === enemy) {
- console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
- legalMoves.push([currpos[0], currpos[1]]);
- break;
- }
- else {
- console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
- break;
- }
- distance = distance + 1;
- if (distance === 7) {
- console.log("Seven squares have passed. Stopping.")
- break;
- }
- }
- break;
- case "P": // Pawn
- var diff = (this.side === 1) ? 1 : -1; //Check south if black, north if white
- if (seeContents(this.xpos, this.ypos + 1) === 0) {
- //If one square north is empty
- legalMoves.push([this.xpos, this.ypos + 1]);
- }
- if (seeContents(this.xpos, this.ypos - 1) === 0) {
- //If one square south is empty
- legalMoves.push([this.xpos, this.ypos - 1]);
- }
- if (seeContents(this.xpos + 1, this.ypos) === 0) {
- //If one square east is empty
- legalMoves.push([this.xpos + 1, this.ypos]);
- }
- if (seeContents(this.xpos - 1, this.ypos) === 0) {
- //If one square west is empty
- legalMoves.push([this.xpos - 1, this.ypos]);
- }
- if (seeContents(this.xpos + 1, this.ypos - diff) === 0) {
- //If one square north/southeast is empty
- legalMoves.push([this.xpos + 1, this.ypos - diff]);
- }
- if (seeContents(this.xpos - 1, this.ypos - diff) === 0) {
- //If one square north/southwest is empty
- legalMoves.push([this.xpos - 1, this.ypos - diff]);
- }
- break;
- }
- return legalMoves;
- };
Advertisement
Add Comment
Please, Sign In to add comment