Advertisement
Guest User

Untitled

a guest
Mar 28th, 2021
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. function twoFens(fen_a, fen_b){
  2. var i, j, temp, obj_a, obj_b, from_squares, to_squares, coord, is_long_castle, king_rank;
  3.  
  4. obj_a=Ic.fenGet(fen_a, "squares activeColor");
  5. obj_b=Ic.fenGet(fen_b, "squares activeColor");
  6.  
  7. if(!obj_a || !obj_b || obj_a.activeColor===obj_b.activeColor){
  8. return "";
  9. }
  10.  
  11. from_squares=[];
  12. to_squares=[];
  13.  
  14. for(i=0; i<8; i++){
  15. for(j=0; j<8; j++){
  16. coord=Ic.toBos([i, j]);
  17.  
  18. if(obj_a.squares[coord].val!==obj_b.squares[coord].val){
  19. if(obj_b.squares[coord].val===0){
  20. //this excludes en passant capture
  21. //a.val can't be 0 here, no problem with inverted logic >0 being <=0
  22. if((obj_a.activeColor==="w")===(obj_a.squares[coord].val>0)){
  23. from_squares.push(coord);
  24. }
  25. }else{
  26. to_squares.push(coord);
  27. }
  28. }
  29. }
  30. }
  31.  
  32. //4 changes (len=2 + len=2) mean king castled
  33. //3 changes would mean en passant capture (but we prevented this)
  34. //1 change (len=1 + len=1) is normal case of moving to empty or normal capture
  35. if(from_squares.length===2 && to_squares.length===2){
  36. //you can infer which castling happened in 4 ways:
  37. //using from_squares and a-file changed = is long castle (used below)
  38. //using from_squares and h-file changed = is short castle
  39. //using to_squares and d-file changed = is long castle
  40. //using to_squares and f-file changed = is short castle
  41. is_long_castle=(from_squares.join("").indexOf("a")!==-1);
  42. king_rank=(obj_a.activeColor==="w" ? 1 : 8);
  43.  
  44. to_squares=[(is_long_castle ? "c" : "g")+king_rank];
  45. from_squares=["e"+(obj_a.activeColor==="w" ? 1 : 8)];
  46. }
  47.  
  48. if(from_squares.length!==1 || to_squares.length!==1){
  49. return "";
  50. }
  51.  
  52. temp=Ic.fenApply(fen_a, "playMove", [[from_squares[0], to_squares[0]]]);
  53.  
  54. return (temp ? temp.san : "");
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement