Advertisement
svetlai

Chess

Jun 12th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(params) {
  2.     var rows = parseInt(params[0]),
  3.         cols = parseInt(params[1]),
  4.         tests = parseInt(params[rows + 2]), i, j, move,
  5.         board = [],
  6.         answer = [];
  7.  
  8.     for (var j = 0; j < rows; j += 1) {
  9.         board[j] = params[j + 2].split('');
  10.     }
  11.  
  12.     for (i = 0; i < tests; i++) {
  13.         move = params[rows + 3 + i].split(' ');
  14.         var currentRow = rows - move[0][1] * 1,
  15.             currentCol = move[0][0].charCodeAt() - 97,
  16.             targetRow = rows - move[1][1] * 1,
  17.             targetCol = move[1][0].charCodeAt() - 97;
  18.  
  19.         var figure = board[currentRow][currentCol];
  20.  
  21.         if (currentRow === targetRow && currentCol === targetCol) {
  22.             answer.push('no');
  23.             continue;
  24.         }
  25.  
  26.         while (true) {
  27.             if (figure === 'Q') {
  28.                 if (targetRow !== currentRow && targetCol !== currentCol) {
  29.                     if (Math.abs(targetRow - currentRow) !== Math.abs(targetCol - currentCol)) {
  30.                         answer.push('no');
  31.                         break;
  32.                     }
  33.                 }
  34.                 if (Math.abs(targetRow - currentRow) === Math.abs(targetCol - currentCol)) {
  35.                     moveDiagonally();
  36.                 } else {
  37.                     moveStraight()
  38.                 }
  39.             } else if (figure === 'B') {
  40.                 if (targetRow === currentRow || targetCol === currentCol) {
  41.                     answer.push('no');
  42.                     break;
  43.                 }
  44.                 if (Math.abs(targetRow - currentRow) !== Math.abs(targetCol - currentCol)) {
  45.                     answer.push('no');
  46.                     break;
  47.                 }
  48.  
  49.                 moveDiagonally();
  50.             } else if (figure === 'R') {
  51.                 if (targetRow !== currentRow && targetCol != currentCol) {
  52.                     answer.push('no');
  53.                     break;
  54.                 }
  55.  
  56.                 moveStraight()
  57.             } else {
  58.                 answer.push('no');
  59.                 break;
  60.             }
  61.  
  62.             if (board[currentRow][currentCol] === 'R' || board[currentRow][currentCol] === 'B' || board[currentRow][currentCol] === 'Q') { // check '-'
  63.                 answer.push('no');
  64.                 break;
  65.             }
  66.  
  67.             if (targetRow === currentRow && targetCol === currentCol) {
  68.                 answer.push('yes');
  69.                 break;
  70.             }
  71.         }
  72.     }
  73.  
  74.     function moveDiagonally() {
  75.         if (targetRow < currentRow) {
  76.             if (targetCol < currentCol) {
  77.                 currentRow--;
  78.                 currentCol--;
  79.             } else {
  80.                 currentRow--;
  81.                 currentCol++;
  82.             }
  83.         } else {
  84.             if (targetCol < currentCol) {
  85.                 currentRow++;
  86.                 currentCol--;
  87.             } else {
  88.                 currentRow++;
  89.                 currentCol++;
  90.             }
  91.         }
  92.     }
  93.  
  94.     function moveStraight() {
  95.         if (targetRow < currentRow) {
  96.             currentRow--;
  97.         } else if (targetRow > currentRow) {
  98.             currentRow++;
  99.         } else if (targetCol < currentCol) {
  100.             currentCol--
  101.         } else if (targetCol > currentCol) {
  102.             currentCol++;
  103.         }
  104.     }
  105.  
  106.     return answer.join('\n').trim();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement