Advertisement
RandomGuy32

allowsEnPassant()

Apr 3rd, 2019
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function allowsEnPassant(piece, movement, targetColumn, targetRow) {
  2.     // this is utter garbage
  3.     // don't look at it
  4.    
  5.     var lastMove = moveHistory[moveHistory.length - 1];
  6.    
  7.     if (lastMove != null) {
  8.         var lastPiece = new ChessPiece(lastMove.piece.type.nameID, lastMove.piece.colour, lastMove.origin[0], lastMove.origin[1], true);
  9.        
  10.         if (enPassantTargets.includes(lastPiece.type.nameID)) {
  11.             if (liesBetween([lastMove.origin[0], lastMove.origin[1]], [targetColumn, targetRow], [lastMove.target[0], lastMove.target[1]], true)) {
  12.                 return true;
  13.             }
  14.         }
  15.     }
  16.    
  17.     return false;
  18. }
  19.  
  20. function liesBetween(pivotA, cell, pivotB, strict) {
  21.     if (strict && (pivotA.equals(cell) || pivotB.equals(cell))) {
  22.         return false;
  23.     }
  24.    
  25.     return pivotA[0] == cell[0] && pivotB[0] == cell[0] && sameSign(pivotA[1] - cell[1], cell[1] - pivotB[1]) ||
  26.            pivotA[1] == cell[1] && pivotB[1] == cell[1] && sameSign(pivotA[0] - cell[0], cell[0] - pivotB[0]);
  27. }
  28.  
  29. function sameSign(a, b) {
  30.     return a <= 0 && b <= 0 || a >= 0 && b >= 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement