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