Isoraqathedh

findLegalMoves() fixed spacing

Jan 15th, 2012
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. HTMLDivElement.prototype.findLegalMoves = function() {
  2.     var legalMoves = []; // Make the array
  3.     var enemy = (this.side === 1)?2:1;
  4.     switch (this.type) { // Determine which one to use
  5.         case "F": // Flag
  6.             var diff = (this.side === 1) ? 1 : -1; //Check south if black, north if white
  7.             /** General Moveset
  8.              *  ===============
  9.              *  A Flag can move one square forwards, without capturing.
  10.              */
  11.             if (seeContents(this.xpos, this.ypos + diff) === 0) {
  12.                 //If one square ahead is empty
  13.                 legalMoves.push([this.xpos, this.ypos + diff]);
  14.             }
  15.             /** Capture choice
  16.              *  ===============
  17.              *  Once per game per Flag, it may capture an ememy piece.
  18.              */
  19.             if (seeContents(this.xpos, this.ypos + diff) === enemy && this.flagHasCaptured === false) {
  20.                 //If the one on the north/southeast square is an enemy piece and it has not taken a piece yet
  21.                 legalMoves.push([this.xpos, this.ypos + diff]);
  22.             }
  23.             /** Diagonal choice
  24.              *  ===============
  25.              *  Once per game per Flag, it can move along the diagonal lines.
  26.              *  They can do so in any direction.
  27.              */
  28.             if (this.flagHasDiagonal === false) {
  29.                 if (this.xpos === 4) { //File T    
  30.                     if (seeContents(5, this.ypos + 1) === 0 || ((seeContents(5, this.ypos + 1) === enemy && this.flagHasCaptured === false))) {
  31.                         console.log("Diagonal square northeast is empty OR Diag sq. NE contains enemy piece AND Flag has not captured yet");
  32.                         legalMoves.push([5, this.ypos + 1]);
  33.                     }
  34.                     if (seeContents(5, this.ypos - 1) === 0 || ((seeContents(5, this.ypos - 1) === enemy && this.flagHasCaptured === false))) {
  35.                         console.log("Diagonal square southeast is empty OR (contains enemy piece AND Flag has not captured yet)");
  36.                         legalMoves.push([5, this.ypos - 1]);
  37.                     }
  38.                 }
  39.                 else if (this.xpos === 5) { //File Œ
  40.                     if (seeContents(4, this.ypos + 1) === 0 || ((seeContents(4, this.ypos + 1) === enemy && this.flagHasCaptured === false))) {
  41.                         console.log("Diagonal square northwest is empty OR (contains enemy piece AND Flag has not captured yet)");
  42.                         legalMoves.push([4, this.ypos + 1]);
  43.                     }
  44.                     if (seeContents(4, this.ypos - 1) === 0 || ((seeContents(4, this.ypos - 1) === enemy && this.flagHasCaptured === false))) {
  45.                         console.log("Diagonal square southwest is empty OR (contains enemy piece AND Flag has not captured yet)");
  46.                         legalMoves.push([4, this.ypos - 1]);
  47.                     }
  48.                 }
  49.                 // Else it has no legal moves. A Flag outside of files T and Œ is stranded.
  50.             }
  51.         break;
  52.         case "R": // Rook
  53.             var currpos = [this.xpos, this.ypos]
  54.             var distance = 0;
  55.             //Move north!
  56.             while (true) {
  57.                 currpos[1] = currpos[1] + 1; // Move tester one square north
  58.                 if (seeContents(currpos[0], currpos[1]) === 0) {
  59.                     console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
  60.                     legalMoves.push([currpos[0], currpos[1]]);
  61.                 }
  62.                 else if (seeContents(currpos[0], currpos[1]) === enemy) {
  63.                     console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
  64.                     legalMoves.push([currpos[0], currpos[1]]);
  65.                     break;
  66.                 }
  67.                 else {
  68.                     console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
  69.                     break;
  70.                 }
  71.                 distance = distance + 1;
  72.                 if (distance === 6) {
  73.                     console.log("Six squares have passed. Stopping.")
  74.                     break;
  75.                 }
  76.             }
  77.             currpos = [this.xpos, this.ypos];
  78.             distance = 0;
  79.             // Move East!
  80.             while (true) {
  81.                 currpos[0] = currpos[0] + 1; // Move tester one square east
  82.                 if (seeContents(currpos[0], currpos[1]) === 0) {
  83.                     console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
  84.                     legalMoves.push([currpos[0], currpos[1]]);
  85.                 }
  86.                 else if (seeContents(currpos[0], currpos[1]) === enemy) {
  87.                     console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
  88.                     legalMoves.push([currpos[0], currpos[1]]);
  89.                     break;
  90.                 }
  91.                 else {
  92.                     console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
  93.                     break;
  94.                 }
  95.                 distance = distance + 1;
  96.                 if (distance === 6) {
  97.                     console.log("Six squares have passed. Stopping.")
  98.                     break;
  99.                 }
  100.             }
  101.             currpos = [this.xpos, this.ypos];
  102.             distance = 0;
  103.             // Move South!
  104.             while (true) {
  105.                 currpos[1] = currpos[1] - 1; // Move tester one square south
  106.                 if (seeContents(currpos[0], currpos[1]) === 0) {
  107.                     console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
  108.                     legalMoves.push([currpos[0], currpos[1]]);
  109.                 }
  110.                 else if (seeContents(currpos[0], currpos[1]) === enemy) {
  111.                     console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
  112.                     legalMoves.push([currpos[0], currpos[1]]);
  113.                     break;
  114.                 }
  115.                 else {
  116.                     console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
  117.                     break;
  118.                 }
  119.                 distance = distance + 1;
  120.                 if (distance === 6) {
  121.                     console.log("Six squares have passed. Stopping.")
  122.                     break;
  123.                 }
  124.             }
  125.             currpos = [this.xpos, this.ypos];
  126.             distance = 0;
  127.             // Move West!
  128.             while (true) {
  129.                 currpos[0] = currpos[0] - 1; // Move tester one square west
  130.                 if (seeContents(currpos[0], currpos[1]) === 0) {
  131.                     console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
  132.                     legalMoves.push([currpos[0], currpos[1]]);
  133.                 }
  134.                 else if (seeContents(currpos[0], currpos[1]) === enemy) {
  135.                     console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
  136.                     legalMoves.push([currpos[0], currpos[1]]);
  137.                     break;
  138.                 }
  139.                 else {
  140.                     console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
  141.                     break;
  142.                 }
  143.                 distance = distance + 1;
  144.                 if (distance === 6) {
  145.                     console.log("Six squares have passed. Stopping.")
  146.                     break;
  147.                 }
  148.             }
  149.         break;
  150.         case "B": // Bishop
  151.             var currpos = [this.xpos, this.ypos]
  152.             var distance = 0;
  153.             //Move northeast!
  154.             while (true) {
  155.                 currpos[1] = currpos[1] + 1; // Move tester one square north
  156.                 currpos[0] = currpos[0] + 1; // Move tester one square east
  157.                 if (seeContents(currpos[0], currpos[1]) === 0) {
  158.                     console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
  159.                     legalMoves.push([currpos[0], currpos[1]]);
  160.                 }
  161.                 else if (seeContents(currpos[0], currpos[1]) === enemy) {
  162.                     console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
  163.                     legalMoves.push([currpos[0], currpos[1]]);
  164.                     break;
  165.                 }
  166.                 else {
  167.                     console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
  168.                     break;
  169.                 }
  170.                 distance = distance + 1;
  171.                 if (distance === 7) {
  172.                     console.log("Seven squares have passed. Stopping.")
  173.                     break;
  174.                 }
  175.             }
  176.             currpos = [this.xpos, this.ypos];
  177.             distance = 0;
  178.             // Move Southeast!
  179.             while (true) {
  180.                 currpos[1] = currpos[1] - 1; // Move tester one square south
  181.                 currpos[0] = currpos[0] + 1; // Move tester one square east
  182.                 if (seeContents(currpos[0], currpos[1]) === 0) {
  183.                     console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
  184.                     legalMoves.push([currpos[0], currpos[1]]);
  185.                 }
  186.                 else if (seeContents(currpos[0], currpos[1]) === enemy) {
  187.                     console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
  188.                     legalMoves.push([currpos[0], currpos[1]]);
  189.                     break;
  190.                 }
  191.                 else {
  192.                     console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
  193.                     break;
  194.                 }
  195.                 distance = distance + 1;
  196.                 if (distance === 7) {
  197.                     console.log("Seven squares have passed. Stopping.")
  198.                     break;
  199.                 }
  200.             }
  201.             currpos = [this.xpos, this.ypos];
  202.             distance = 0;
  203.             // Move Southwest!
  204.             while (true) {
  205.                 currpos[1] = currpos[1] - 1; // Move tester one square south
  206.                 currpos[0] = currpos[0] - 1; // Move tester one square west
  207.                 if (seeContents(currpos[0], currpos[1]) === 0) {
  208.                     console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
  209.                     legalMoves.push([currpos[0], currpos[1]]);
  210.                 }
  211.                 else if (seeContents(currpos[0], currpos[1]) === enemy) {
  212.                     console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
  213.                     legalMoves.push([currpos[0], currpos[1]]);
  214.                     break;
  215.                 }
  216.                 else {
  217.                     console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
  218.                     break;
  219.                 }
  220.                 distance = distance + 1;
  221.                 if (distance === 7) {
  222.                     console.log("Seven squares have passed. Stopping.")
  223.                     break;
  224.                 }
  225.             }
  226.             currpos = [this.xpos, this.ypos];
  227.             distance = 0;
  228.             // Move Northwest!
  229.             while (true) {
  230.                 currpos[1] = currpos[1] + 1; // Move tester one square north
  231.                 currpos[0] = currpos[0] - 1; // Move tester one square west
  232.                 if (seeContents(currpos[0], currpos[1]) === 0) {
  233.                     console.log("At " + boardnot[currpos[1]][currpos[0]] + ", cell empty and continuing");
  234.                     legalMoves.push([currpos[0], currpos[1]]);
  235.                 }
  236.                 else if (seeContents(currpos[0], currpos[1]) === enemy) {
  237.                     console.warn("At " + boardnot[currpos[1]][currpos[0]] + ", cell occupied by an enemy");
  238.                     legalMoves.push([currpos[0], currpos[1]]);
  239.                     break;
  240.                 }
  241.                 else {
  242.                     console.warn("Cell occupied by friendly piece or edge of board. Stopping.");
  243.                     break;
  244.                 }
  245.                 distance = distance + 1;
  246.                 if (distance === 7) {
  247.                     console.log("Seven squares have passed. Stopping.")
  248.                     break;
  249.                 }
  250.             }
  251.         break;
  252.         case "P": // Pawn
  253.             var diff = (this.side === 1) ? 1 : -1; //Check south if black, north if white
  254.             if (seeContents(this.xpos, this.ypos + 1) === 0) {
  255.                 //If one square north is empty
  256.                 legalMoves.push([this.xpos, this.ypos + 1]);
  257.             }
  258.             if (seeContents(this.xpos, this.ypos - 1) === 0) {
  259.                 //If one square south is empty
  260.                 legalMoves.push([this.xpos, this.ypos - 1]);
  261.             }
  262.             if (seeContents(this.xpos + 1, this.ypos) === 0) {
  263.                 //If one square east is empty
  264.                 legalMoves.push([this.xpos + 1, this.ypos]);
  265.             }
  266.             if (seeContents(this.xpos - 1, this.ypos) === 0) {
  267.                 //If one square west is empty
  268.                 legalMoves.push([this.xpos - 1, this.ypos]);
  269.             }
  270.             if (seeContents(this.xpos + 1, this.ypos - diff) === 0) {
  271.                 //If one square north/southeast is empty
  272.                 legalMoves.push([this.xpos + 1, this.ypos - diff]);
  273.             }
  274.             if (seeContents(this.xpos - 1, this.ypos - diff) === 0) {
  275.                 //If one square north/southwest is empty
  276.                 legalMoves.push([this.xpos - 1, this.ypos - diff]);
  277.             }
  278.         break;
  279.     }
  280.     return legalMoves;
  281. };
Advertisement
Add Comment
Please, Sign In to add comment