Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function twoFens(fen_a, fen_b){
- var i, j, temp, obj_a, obj_b, from_squares, to_squares, coord, is_long_castle, king_rank;
- obj_a=Ic.fenGet(fen_a, "squares activeColor");
- obj_b=Ic.fenGet(fen_b, "squares activeColor");
- if(!obj_a || !obj_b || obj_a.activeColor===obj_b.activeColor){
- return "";
- }
- from_squares=[];
- to_squares=[];
- for(i=0; i<8; i++){
- for(j=0; j<8; j++){
- coord=Ic.toBos([i, j]);
- if(obj_a.squares[coord].val!==obj_b.squares[coord].val){
- if(obj_b.squares[coord].val===0){
- //this excludes en passant capture
- //a.val can't be 0 here, no problem with inverted logic >0 being <=0
- if((obj_a.activeColor==="w")===(obj_a.squares[coord].val>0)){
- from_squares.push(coord);
- }
- }else{
- to_squares.push(coord);
- }
- }
- }
- }
- //4 changes (len=2 + len=2) mean king castled
- //3 changes would mean en passant capture (but we prevented this)
- //1 change (len=1 + len=1) is normal case of moving to empty or normal capture
- if(from_squares.length===2 && to_squares.length===2){
- //you can infer which castling happened in 4 ways:
- //using from_squares and a-file changed = is long castle (used below)
- //using from_squares and h-file changed = is short castle
- //using to_squares and d-file changed = is long castle
- //using to_squares and f-file changed = is short castle
- is_long_castle=(from_squares.join("").indexOf("a")!==-1);
- king_rank=(obj_a.activeColor==="w" ? 1 : 8);
- to_squares=[(is_long_castle ? "c" : "g")+king_rank];
- from_squares=["e"+(obj_a.activeColor==="w" ? 1 : 8)];
- }
- if(from_squares.length!==1 || to_squares.length!==1){
- return "";
- }
- temp=Ic.fenApply(fen_a, "playMove", [[from_squares[0], to_squares[0]]]);
- return (temp ? temp.san : "");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement