1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396 |
- import { _decorator } from "cc";
- const { ccclass } = _decorator;
- @ccclass("ChessPos")
- export default class ChessPos {
- clearBoard = function() {
- this.sdPlayer = 0;
- this.squares = []; //这就是一维棋局数组。
- for (var sq = 0; sq < 256; sq ++) {
- this.squares.push(0);
- }
- this.zobristKey = this.zobristLock = 0;
- this.vlWhite = this.vlBlack = 0;
- };
- mvList:any[] = []
- pcList:any[] = []
- keyList:any[] = []
- chkList:any[] = []
- squares:any[] = []
- distance:number = 0
- vlWhite:number = 0
- vlBlack:number = 0
- zobristKey:number = 0
- zobristLock:number = 0
- sdPlayer:number = 0
- setIrrev() {
- this.mvList = [0];
- this.pcList = [0];
- this.keyList = [0];
- this.chkList = [this.checked()];
- this.distance = 0;
- }
- // 如果bDel为false,则将棋子pc添加进棋局中的sp位置;如果bDel为true,则删除sp位置的棋子。
- private addPiece(sq, pc, bDel) {
- var pcAdjust;
- this.squares[sq] = bDel ? 0 : pc;
- if(pc<8){
- console.log("addPiece error",pc)
- }
- if (pc < 16) {
- pcAdjust = pc - 8;
- this.vlWhite += bDel ? -PIECE_VALUE[pcAdjust][sq] :
- PIECE_VALUE[pcAdjust][sq];
-
- } else {
- pcAdjust = pc - 16;
- this.vlBlack += bDel ? -PIECE_VALUE[pcAdjust][SQUARE_FLIP(sq)] :
- PIECE_VALUE[pcAdjust][SQUARE_FLIP(sq)];
- pcAdjust += 7;
- }
- this.zobristKey ^= PreGen_zobristKeyTable[pcAdjust][sq];
- this.zobristLock ^= PreGen_zobristLockTable[pcAdjust][sq];
- }
- public pushMvList(mv){
- // var sqSrc = SRC(mv);
- // var sqDst = DST(mv);
- this.mvList.push(mv);
- this.distance ++;
- }
- private movePiece(mv) {
- var sqSrc = SRC(mv);
- var sqDst = DST(mv);
- var pc = this.squares[sqDst];
- this.pcList.push(pc);
- if (pc > 0) {
- this.addPiece(sqDst, pc, DEL_PIECE);
- }
- pc = this.squares[sqSrc];
- if(pc==0){
- console.log("pc error == ",this.squares,sqDst)
- }else{
- this.addPiece(sqSrc, pc, DEL_PIECE);
- this.addPiece(sqDst, pc, ADD_PIECE);
- }
- this.mvList.push(mv);
- }
-
- private undoMovePiece() {
- var mv = this.mvList.pop();
- var sqSrc = SRC(mv);
- var sqDst = DST(mv);
- var pc = this.squares[sqDst];
- this.addPiece(sqDst, pc, DEL_PIECE);
- this.addPiece(sqSrc, pc, ADD_PIECE);
- pc = this.pcList.pop();
- if (pc > 0) {
- this.addPiece(sqDst, pc, ADD_PIECE);
- }
- }
-
- public changeSide() {
- this.sdPlayer = 1 - this.sdPlayer;
- this.zobristKey ^= PreGen_zobristKeyPlayer;
- this.zobristLock ^= PreGen_zobristLockPlayer;
- }
-
- public makeMove(mv) {
- var zobristKey = this.zobristKey;
- this.movePiece(mv);
- // 检查走棋是否被将军。如果是,说明这是在送死,撤销走棋并返回false。
- if (this.checked()) {
- this.undoMovePiece(); // 撤销棋子移动
- return false;
- }
- this.keyList.push(zobristKey);
- this.changeSide();
- this.chkList.push(this.checked());
- this.distance ++;
- return true;
- }
-
- public undoMakeMove() {
- this.distance --;
- this.chkList.pop();
- this.changeSide();
- this.keyList.pop();
- this.undoMovePiece();
- }
-
- public nullMove() {
- this.mvList.push(0);
- this.pcList.push(0);
- this.keyList.push(this.zobristKey);
- this.changeSide();
- this.chkList.push(false);
- this.distance ++;
- }
-
- public undoNullMove() {
- this.distance --;
- this.chkList.pop();
- this.changeSide();
- this.keyList.pop();
- this.pcList.pop();
- this.mvList.pop();
- }
- //通过FEN串初始化棋局,也就是将参数fen表示的棋局,转化为一维棋局数组squares表示的棋局。
- public fromFen(fen) {
- this.clearBoard();
- var y = RANK_TOP;
- var x = FILE_LEFT;
- var index = 0;
- if (index == fen.length) {
- this.setIrrev();
- return;
- }
- var c = fen.charAt(index);
- while (c != " ") {
- if (c == "/") {
- x = FILE_LEFT;
- y ++;
- if (y > RANK_BOTTOM) {
- break;
- }
- } else if (c >= "1" && c <= "9") {
- x += (ASC(c) - ASC("0"));
- } else if (c >= "A" && c <= "Z") {
- if (x <= FILE_RIGHT) {
- var pt = CHAR_TO_PIECE(c);
- if (pt >= 0) {
- this.addPiece(COORD_XY(x, y), pt + 8,null);
- }
- x ++;
- }
- } else if (c >= "a" && c <= "z") {
- if (x <= FILE_RIGHT) {
- var pt = CHAR_TO_PIECE(CHR(ASC(c) + ASC("A") - ASC("a")));
- if (pt >= 0) {
- this.addPiece(COORD_XY(x, y), pt + 16,null);
- }
- x ++;
- }
- }
- index ++;
- if (index == fen.length) {
- this.setIrrev();
- return;
- }
- c = fen.charAt(index);
- }
- index ++;
- if (index == fen.length) {
- this.setIrrev();
- return;
- }
- if (this.sdPlayer == (fen.charAt(index) == "b" ? 0 : 1)) {
- this.changeSide();
- }
- this.setIrrev();
- }
-
- public toFen() {
- var fen = "";
- for (var y = RANK_TOP; y <= RANK_BOTTOM; y ++) {
- var k = 0;
- for (var x = FILE_LEFT; x <= FILE_RIGHT; x ++) {
- var pc = this.squares[COORD_XY(x, y)];
- if (pc > 0) {
- if (k > 0) {
- fen += CHR(ASC("0") + k);
- k = 0;
- }
- fen += FEN_PIECE.charAt(pc);
- } else {
- k ++;
- }
- }
- if (k > 0) {
- fen += CHR(ASC("0") + k);
- }
- fen += "/";
- }
- return fen.substring(0, fen.length - 1) +
- (this.sdPlayer == 0 ? " w" : " b");
- }
-
- public generateMoves(vls) {
- var mvs = [];
- var pcSelfSide = SIDE_TAG(this.sdPlayer);
- var pcOppSide = OPP_SIDE_TAG(this.sdPlayer);
- for (var sqSrc = 0; sqSrc < 256; sqSrc ++) {
- var pcSrc = this.squares[sqSrc];
- if ((pcSrc & pcSelfSide) == 0) {
- continue;
- }
- switch (pcSrc - pcSelfSide) {
- case PIECE_KING:
- for (var i = 0; i < 4; i ++) {
- var sqDst = sqSrc + KING_DELTA[i];
- if (!IN_FORT(sqDst)) {
- continue;
- }
- var pcDst = this.squares[sqDst];
- if (vls == null) {
- if ((pcDst & pcSelfSide) == 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- }
- } else if ((pcDst & pcOppSide) != 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- vls.push(MVV_LVA(pcDst, 5));
- }
- }
- break;
- case PIECE_ADVISOR:
- for (var i = 0; i < 4; i ++) {
- var sqDst = sqSrc + ADVISOR_DELTA[i];
- if (!IN_FORT(sqDst)) {
- continue;
- }
- var pcDst = this.squares[sqDst];
- if (vls == null) {
- if ((pcDst & pcSelfSide) == 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- }
- } else if ((pcDst & pcOppSide) != 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- vls.push(MVV_LVA(pcDst, 1));
- }
- }
- break;
- case PIECE_BISHOP:
- for (var i = 0; i < 4; i ++) {
- var sqDst = sqSrc + ADVISOR_DELTA[i];
- if (!(IN_BOARD(sqDst) && HOME_HALF(sqDst, this.sdPlayer) &&
- this.squares[sqDst] == 0)) {
- continue;
- }
- sqDst += ADVISOR_DELTA[i];
- var pcDst = this.squares[sqDst];
- if (vls == null) {
- if ((pcDst & pcSelfSide) == 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- }
- } else if ((pcDst & pcOppSide) != 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- vls.push(MVV_LVA(pcDst, 1));
- }
- }
- break;
- case PIECE_KNIGHT:
- for (var i = 0; i < 4; i ++) {
- var sqDst = sqSrc + KING_DELTA[i];
- if (this.squares[sqDst] > 0) {
- continue;
- }
- for (var j = 0; j < 2; j ++) {
- sqDst = sqSrc + KNIGHT_DELTA[i][j];
- if (!IN_BOARD(sqDst)) {
- continue;
- }
- var pcDst = this.squares[sqDst];
- if (vls == null) {
- if ((pcDst & pcSelfSide) == 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- }
- } else if ((pcDst & pcOppSide) != 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- vls.push(MVV_LVA(pcDst, 1));
- }
- }
- }
- break;
- case PIECE_ROOK:
- for (var i = 0; i < 4; i ++) {
- var delta = KING_DELTA[i];
- var sqDst = sqSrc + delta;
- while (IN_BOARD(sqDst)) {
- var pcDst = this.squares[sqDst];
- if (pcDst == 0) {
- if (vls == null) {
- mvs.push(MOVE(sqSrc, sqDst));
- }
- } else {
- if ((pcDst & pcOppSide) != 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- if (vls != null) {
- vls.push(MVV_LVA(pcDst, 4));
- }
- }
- break;
- }
- sqDst += delta;
- }
- }
- break;
- case PIECE_CANNON:
- for (var i = 0; i < 4; i ++) {
- var delta = KING_DELTA[i];
- var sqDst = sqSrc + delta;
- while (IN_BOARD(sqDst)) {
- var pcDst = this.squares[sqDst];
- if (pcDst == 0) {
- if (vls == null) {
- mvs.push(MOVE(sqSrc, sqDst));
- }
- } else {
- break;
- }
- sqDst += delta;
- }
- sqDst += delta;
- while (IN_BOARD(sqDst)) {
- var pcDst = this.squares[sqDst];
- if (pcDst > 0) {
- if ((pcDst & pcOppSide) != 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- if (vls != null) {
- vls.push(MVV_LVA(pcDst, 4));
- }
- }
- break;
- }
- sqDst += delta;
- }
- }
- break;
- case PIECE_PAWN:
- var sqDst = SQUARE_FORWARD(sqSrc, this.sdPlayer);
- if (IN_BOARD(sqDst)) {
- var pcDst = this.squares[sqDst];
- if (vls == null) {
- if ((pcDst & pcSelfSide) == 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- }
- } else if ((pcDst & pcOppSide) != 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- vls.push(MVV_LVA(pcDst, 2));
- }
- }
- if (AWAY_HALF(sqSrc, this.sdPlayer)) {
- for (var delta = -1; delta <= 1; delta += 2) {
- sqDst = sqSrc + delta;
- if (IN_BOARD(sqDst)) {
- var pcDst = this.squares[sqDst];
- if (vls == null) {
- if ((pcDst & pcSelfSide) == 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- }
- } else if ((pcDst & pcOppSide) != 0) {
- mvs.push(MOVE(sqSrc, sqDst));
- vls.push(MVV_LVA(pcDst, 2));
- }
- }
- }
- }
- break;
- }
- }
- return mvs;
- }
- public findAllCanMove(sq){
- var sqSrc = sq;
- var pcSrc = this.squares[sqSrc];
- var temp_array = [];
- var pcSelfSide = SIDE_TAG(this.sdPlayer);
- var pcOppSide = OPP_SIDE_TAG(this.sdPlayer);
- if ((pcSrc & pcSelfSide) == 0) {
- return temp_array;
- }
- switch (pcSrc - pcSelfSide) {
- case PIECE_KING: //寻找当前将所有可以走的路
- {
- var des_array = []
- des_array.push(sqSrc - 16)
- des_array.push(sqSrc - 1)
- des_array.push(sqSrc + 16)
- des_array.push(sqSrc + 1)
- for(var i=0;i<des_array.length;i++){
- if(IN_BOARD(des_array[i])){
- var b =this.legalMove(MOVE(sqSrc,des_array[i]))
- if(b){
- temp_array.push(des_array[i])
- }
- }
- }
- }
- break;
- case PIECE_ADVISOR:// 寻找当前士所有可以走的路
- {
- var des_array = []
- des_array.push(sqSrc - 17)
- des_array.push(sqSrc - 15)
- des_array.push(sqSrc + 17)
- des_array.push(sqSrc + 15)
- for(var i=0;i<des_array.length;i++){
- if(IN_BOARD(des_array[i])){
- var b =this.legalMove(MOVE(sqSrc,des_array[i]))
- if(b){
- temp_array.push(des_array[i])
- }
- }
- }
- }
- break;
- case PIECE_BISHOP: // 寻找当前象所有可以走的路
- {
- var des_array = []
- des_array.push(sqSrc - 34)
- des_array.push(sqSrc - 30)
- des_array.push(sqSrc + 30)
- des_array.push(sqSrc + 34)
- for(var i=0;i<des_array.length;i++){
- if(IN_BOARD(des_array[i])){
- var b =this.legalMove(MOVE(sqSrc,des_array[i]))
- if(b){
- temp_array.push(des_array[i])
- }
- }
- }
- }
- break;
- case PIECE_KNIGHT:// 寻找当前马所有可以走的路
- {
- var des_array = []
- des_array.push(sqSrc - 33)
- des_array.push(sqSrc - 31)
- des_array.push(sqSrc - 18)
- des_array.push(sqSrc + 14)
- des_array.push(sqSrc - 14)
- des_array.push(sqSrc + 18)
- des_array.push(sqSrc + 31)
- des_array.push(sqSrc + 33)
- for(var i=0;i<des_array.length;i++){
- if(IN_BOARD(des_array[i])){
- var b =this.legalMove(MOVE(sqSrc,des_array[i]))
- if(b){
- temp_array.push(des_array[i])
- }
- }
- }
- }
- break;
- case PIECE_CANNON:
- case PIECE_ROOK: //寻找当前 车 所有可以走的路
- {
- //var des_array = []
- //遍历列
- var left_index = 1;
- var isCannon = false;
- while (IN_BOARD(sqSrc-left_index)) {
- var pcLeftDst = this.squares[sqSrc-left_index]; //
- if(pcLeftDst!=0){
- if ((pcLeftDst & pcSelfSide) != 0) {
- //说明找到自己的棋子
- if((pcSrc - pcSelfSide) ==PIECE_ROOK ||isCannon){
- break;
- }else{
- isCannon = true;
- }
-
- }else if((pcLeftDst & pcOppSide) != 0){
- //说明找到敌方的棋子
- if((pcSrc - pcSelfSide) ==PIECE_ROOK){
- temp_array.push(sqSrc-left_index)
- break;
- }else{
- if(isCannon){
- temp_array.push(sqSrc-left_index)
- break;
- }else{
- isCannon = true;
- }
- }
- }
- }else{
- if(!isCannon){
- temp_array.push(sqSrc-left_index)
- }
- }
- left_index++;
- }
- var right_index = 1;
- isCannon = false;
- while (IN_BOARD(sqSrc+right_index)) {
- var pcRightDst = this.squares[sqSrc+right_index]; //
- if(pcRightDst!=0){
- if ((pcRightDst & pcSelfSide) != 0) {
- //说明找到自己的棋子
- if((pcSrc - pcSelfSide) ==PIECE_ROOK ||isCannon){
- break;
- }else{
- isCannon = true;
- }
- }else if((pcRightDst & pcOppSide) != 0){
- //说明找到敌方的棋子
- if((pcSrc - pcSelfSide) ==PIECE_ROOK){
- temp_array.push(sqSrc+right_index)
- break;
- }else{
- if(isCannon){
- temp_array.push(sqSrc+right_index)
- break;
- }else{
- isCannon = true;
- }
- }
- }
- }else{
- if(!isCannon){
- temp_array.push(sqSrc+right_index)
- }
- }
- right_index++;
- }
- //遍历行
- var top_index = 16;
- isCannon = false;
- while (IN_BOARD(sqSrc-top_index)) {
- var pcTopDst = this.squares[sqSrc-top_index]; //
- if(pcTopDst!=0){
- if ((pcTopDst & pcSelfSide) != 0) {
- //说明找到自己的棋子
- if((pcSrc - pcSelfSide) ==PIECE_ROOK ||isCannon){
- break;
- }else{
- isCannon = true;
- }
- }else if((pcTopDst & pcOppSide) != 0){
- //说明找到敌方的棋子
- if((pcSrc - pcSelfSide) ==PIECE_ROOK){
- temp_array.push(sqSrc-top_index)
- break;
- }else{
- if(isCannon){
- temp_array.push(sqSrc-top_index)
- break;
- }else{
- isCannon = true;
- }
- }
- }
- }else{
- if(!isCannon){
- temp_array.push(sqSrc-top_index)
- }
- }
- top_index+=16;
- }
- var bottom_index = 16;
- isCannon = false;
- while (IN_BOARD(sqSrc+bottom_index)) {
- var pcBottomDst = this.squares[sqSrc+bottom_index]; //
- if(pcBottomDst!=0){
- if ((pcBottomDst & pcSelfSide) != 0) {
- //说明找到自己的棋子
- if((pcSrc - pcSelfSide) ==PIECE_ROOK ||isCannon){
- break;
- }else{
- isCannon = true;
- }
- }else if((pcBottomDst & pcOppSide) != 0){
- //说明找到敌方的棋子
- if((pcSrc - pcSelfSide) ==PIECE_ROOK){
- temp_array.push(sqSrc+bottom_index)
- break;
- }else{
- if(isCannon){
- temp_array.push(sqSrc+bottom_index)
- break;
- }else{
- isCannon = true;
- }
- }
- }
- }else{
- if(!isCannon){
- temp_array.push(sqSrc+bottom_index)
- }
- }
- bottom_index+=16;
- }
- }
- break;
- case PIECE_PAWN: //寻找当前卒 所有可以走的路
- {
- var des_array = []
- des_array.push(sqSrc - 16)
- des_array.push(sqSrc - 1)
- des_array.push(sqSrc + 16)
- des_array.push(sqSrc + 1)
- for(i=0;i<des_array.length;i++){
- if(IN_BOARD(des_array[i])){
- var b =this.legalMove(MOVE(sqSrc,des_array[i]))
- if(b){
- temp_array.push(des_array[i])
- }
- }
- }
- }
- break;
- default:
- return temp_array;
- }
- return temp_array;
- }
- //判断步骤是否合法。合法返回true,非法返回false。
- public legalMove(mv) {
- var sqSrc = SRC(mv);
- var pcSrc = this.squares[sqSrc];
- var pcSelfSide = SIDE_TAG(this.sdPlayer);
- if ((pcSrc & pcSelfSide) == 0) {
- return false;
- }
-
- var sqDst = DST(mv);
- var pcDst = this.squares[sqDst];
- if ((pcDst & pcSelfSide) != 0) {
- return false;
- }
-
- switch (pcSrc - pcSelfSide) {
- case PIECE_KING:
- return IN_FORT(sqDst) && KING_SPAN(sqSrc, sqDst);
- case PIECE_ADVISOR:
- return IN_FORT(sqDst) && ADVISOR_SPAN(sqSrc, sqDst);
- case PIECE_BISHOP:
- return SAME_HALF(sqSrc, sqDst) && BISHOP_SPAN(sqSrc, sqDst) &&
- this.squares[BISHOP_PIN(sqSrc, sqDst)] == 0;
- case PIECE_KNIGHT:
- var sqPin = KNIGHT_PIN(sqSrc, sqDst);
- return sqPin != sqSrc && this.squares[sqPin] == 0;
- case PIECE_ROOK:
- case PIECE_CANNON:
- var delta;
- if (SAME_RANK(sqSrc, sqDst)) {
- delta = (sqDst < sqSrc ? -1 : 1);
- } else if (SAME_FILE(sqSrc, sqDst)) {
- delta = (sqDst < sqSrc ? -16 : 16);
- } else {
- return false;
- }
- var sqPin = sqSrc + delta;
- while (sqPin != sqDst && this.squares[sqPin] == 0) {
- sqPin += delta;
- }
- if (sqPin == sqDst) {
- return pcDst == 0 || pcSrc - pcSelfSide == PIECE_ROOK;
- }
- if (pcDst == 0 || pcSrc - pcSelfSide != PIECE_CANNON) {
- return false;
- }
- sqPin += delta;
- while (sqPin != sqDst && this.squares[sqPin] == 0) {
- sqPin += delta;
- }
- return sqPin == sqDst;
- case PIECE_PAWN:
- if (AWAY_HALF(sqDst, this.sdPlayer) && (sqDst == sqSrc - 1 || sqDst == sqSrc + 1)) {
- return true;
- }
- return sqDst == SQUARE_FORWARD(sqSrc, this.sdPlayer);
- default:
- return false;
- }
- }
-
- private checked() {
- var pcSelfSide = SIDE_TAG(this.sdPlayer);
- var pcOppSide = OPP_SIDE_TAG(this.sdPlayer);
- for (var sqSrc = 0; sqSrc < 256; sqSrc ++) {
- if (this.squares[sqSrc] != pcSelfSide + PIECE_KING) {
- continue;
- }
- if (this.squares[SQUARE_FORWARD(sqSrc, this.sdPlayer)] == pcOppSide + PIECE_PAWN) {
- return true;
- }
- for (var delta = -1; delta <= 1; delta += 2) {
- if (this.squares[sqSrc + delta] == pcOppSide + PIECE_PAWN) {
- return true;
- }
- }
- for (var i = 0; i < 4; i ++) {
- if (this.squares[sqSrc + ADVISOR_DELTA[i]] != 0) {
- continue;
- }
- for (var j = 0; j < 2; j ++) {
- var pcDst = this.squares[sqSrc + KNIGHT_CHECK_DELTA[i][j]];
- if (pcDst == pcOppSide + PIECE_KNIGHT) {
- return true;
- }
- }
- }
- for (var i = 0; i < 4; i ++) {
- var delta = KING_DELTA[i];
- var sqDst = sqSrc + delta;
- while (IN_BOARD(sqDst)) {
- var pcDst = this.squares[sqDst];
- if (pcDst > 0) {
- if (pcDst == pcOppSide + PIECE_ROOK || pcDst == pcOppSide + PIECE_KING) {
- return true;
- }
- break;
- }
- sqDst += delta;
- }
- sqDst += delta;
- while (IN_BOARD(sqDst)) {
- var pcDst = this.squares[sqDst];
- if (pcDst > 0) {
- if (pcDst == pcOppSide + PIECE_CANNON) {
- return true;
- }
- break;
- }
- sqDst += delta;
- }
- }
- return false;
- }
- return false;
- }
-
- isMate() {
- var mvs = this.generateMoves(null);
- for (var i = 0; i < mvs.length; i ++) {
- if (this.makeMove(mvs[i])) {
- this.undoMakeMove();
- return false;
- }
- }
- return true;
- }
-
- mateValue() {
- return this.distance - MATE_VALUE;
- }
-
- banValue() {
- return this.distance - BAN_VALUE;
- }
-
- drawValue() {
- return (this.distance & 1) == 0 ? -DRAW_VALUE : DRAW_VALUE;
- }
-
- evaluate() {
- var vl = (this.sdPlayer == 0 ? this.vlWhite - this.vlBlack :
- this.vlBlack - this.vlWhite) + ADVANCED_VALUE;
- return vl == this.drawValue() ? vl - 1 : vl;
- }
-
- nullOkay() {
- return (this.sdPlayer == 0 ? this.vlWhite : this.vlBlack) > NULL_OKAY_MARGIN;
- }
-
- nullSafe() {
- return (this.sdPlayer == 0 ? this.vlWhite : this.vlBlack) > NULL_SAFE_MARGIN;
- }
-
- inCheck() {
- return this.chkList[this.chkList.length - 1];
- }
-
- captured() {
- return this.pcList[this.pcList.length - 1] > 0;
- }
-
- repValue(vlRep) {
- var vlReturn = ((vlRep & 2) == 0 ? 0 : this.banValue()) +
- ((vlRep & 4) == 0 ? 0 : -this.banValue());
- return vlReturn == 0 ? this.drawValue() : vlReturn;
- }
-
- repStatus(recur_) {
- var recur = recur_;
- var selfSide = false;
- var perpCheck = true;
- var oppPerpCheck = true;
- var index = this.mvList.length - 1;
- while (this.mvList[index] > 0 && this.pcList[index] == 0) {
- if (selfSide) {
- perpCheck = perpCheck && this.chkList[index];
- if (this.keyList[index] == this.zobristKey) {
- recur --;
- if (recur == 0) {
- return 1 + (perpCheck ? 2 : 0) + (oppPerpCheck ? 4 : 0);
- }
- }
- } else {
- oppPerpCheck = oppPerpCheck && this.chkList[index];
- }
- selfSide = !selfSide;
- index --;
- }
- return 0;
- }
-
- mirror() {
- var pos = new ChessPos();
- pos.clearBoard();
- for (var sq = 0; sq < 256; sq ++) {
- var pc = this.squares[sq];
- if (pc > 0) {
- pos.addPiece(MIRROR_SQUARE(sq), pc,null);
- }
- }
- if (this.sdPlayer == 1) {
- pos.changeSide();
- }
- return pos;
- }
-
- bookMove() {
- return 0;
- // if (typeof BOOK_DAT != "object" || BOOK_DAT.length == 0) {
- // return 0;
- // }
- // var mirror = false;
- // var lock = this.zobristLock >>> 1; // Convert into Unsigned
- // var index = binarySearch(BOOK_DAT, lock);
- // if (index < 0) {
- // mirror = true;
- // lock = this.mirror().zobristLock >>> 1; // Convert into Unsigned
- // index = binarySearch(BOOK_DAT, lock);
- // }
- // if (index < 0) {
- // return 0;
- // }
- // index --;
- // while (index >= 0 && BOOK_DAT[index][0] == lock) {
- // index --;
- // }
- // var mvs = [], vls = [];
- // var value = 0;
- // index ++;
- // while (index < BOOK_DAT.length && BOOK_DAT[index][0] == lock) {
- // var mv = BOOK_DAT[index][1];
- // mv = (mirror ? MIRROR_MOVE(mv) : mv);
- // if (this.legalMove(mv)) {
- // mvs.push(mv);
- // var vl = BOOK_DAT[index][2];
- // vls.push(vl);
- // value += vl;
- // }
- // index ++;
- // }
- // if (value == 0) {
- // return 0;
- // }
- // value = Math.floor(Math.random() * value);
- // for (index = 0; index < mvs.length; index ++) {
- // value -= vls[index];
- // if (value < 0) {
- // break;
- // }
- // }
- // return mvs[index];
- }
-
- historyIndex(mv) {
- return ((this.squares[SRC(mv)] - 8) << 8) + DST(mv);
- }
- }
- function binarySearch(vlss, vl) {
- var low = 0;
- var high = vlss.length - 1;
- while (low <= high) {
- var mid = (low + high) >> 1;
- if (vlss[mid][0] < vl) {
- low = mid + 1;
- } else if (vlss[mid][0] > vl) {
- high = mid - 1;
- } else {
- return mid;
- }
- }
- return -1;
- }
- var MATE_VALUE = 10000;
- var BAN_VALUE = MATE_VALUE - 100;
- var WIN_VALUE = MATE_VALUE - 200;
- var NULL_SAFE_MARGIN = 400;
- var NULL_OKAY_MARGIN = 200;
- var DRAW_VALUE = 20;
- var ADVANCED_VALUE = 3;
- /** !#en This is a KING.
- !#zh 这是一个 將 */
- var PIECE_KING = 0;
- /** !#en This is a ADVISOR.
- !#zh 这是一个 士 */
- var PIECE_ADVISOR = 1;
- /** !#en This is a BISHOP.
- !#zh 这是一个 象 */
- var PIECE_BISHOP = 2;
- /** !#en This is a KNIGHT.
- !#zh 这是一个 馬 */
- var PIECE_KNIGHT = 3;
- /** !#en This is a ROOK.
- !#zh 这是一个 车 */
- var PIECE_ROOK = 4;// 车
- /** !#en This is a CANNON.
- !#zh 这是一个 炮 */
- var PIECE_CANNON = 5;
- /** !#en This is a PAWN.
- !#zh 这是一个 卒 */
- var PIECE_PAWN = 6;
- var RANK_TOP = 3;
- var RANK_BOTTOM = 12;
- var FILE_LEFT = 3;
- var FILE_RIGHT = 11;
- var ADD_PIECE = false;
- var DEL_PIECE = true;
- /** !#en This is a init board 16*16 size.
- !#zh 这是一个初始棋盤的大小 在程序中,棋局被表示为大小为256的一维数组,一半棋盘位于0到127,另一半位于128到255。
- 128的二进制是1000 0000,右起第八位是1。128到255这些数的二进制,右起第8位都是1;0到127这些数的二进制,右起第8位都是0。 */
- var IN_BOARD_ = [
- /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //15
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //31
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //47
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1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0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0,
- ];
- var KING_DELTA = [-16, -1, 1, 16];
- var ADVISOR_DELTA = [-17, -15, 15, 17];
- var KNIGHT_DELTA = [[-33, -31], [-18, 14], [-14, 18], [31, 33]];
- var KNIGHT_CHECK_DELTA = [[-33, -18], [-31, -14], [14, 31], [18, 33]];
- var MVV_VALUE = [50, 10, 10, 30, 40, 30, 20, 0];
- var PIECE_VALUE = [
- [
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 9, 9, 9, 11, 13, 11, 9, 9, 9, 0, 0, 0, 0,
- 0, 0, 0, 19, 24, 34, 42, 44, 42, 34, 24, 19, 0, 0, 0, 0,
- 0, 0, 0, 19, 24, 32, 37, 37, 37, 32, 24, 19, 0, 0, 0, 0,
- 0, 0, 0, 19, 23, 27, 29, 30, 29, 27, 23, 19, 0, 0, 0, 0,
- 0, 0, 0, 14, 18, 20, 27, 29, 27, 20, 18, 14, 0, 0, 0, 0,
- 0, 0, 0, 7, 0, 13, 0, 16, 0, 13, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 90, 90, 90, 96, 90, 96, 90, 90, 90, 0, 0, 0, 0,
- 0, 0, 0, 90, 96,103, 97, 94, 97,103, 96, 90, 0, 0, 0, 0,
- 0, 0, 0, 92, 98, 99,103, 99,103, 99, 98, 92, 0, 0, 0, 0,
- 0, 0, 0, 93,108,100,107,100,107,100,108, 93, 0, 0, 0, 0,
- 0, 0, 0, 90,100, 99,103,104,103, 99,100, 90, 0, 0, 0, 0,
- 0, 0, 0, 90, 98,101,102,103,102,101, 98, 90, 0, 0, 0, 0,
- 0, 0, 0, 92, 94, 98, 95, 98, 95, 98, 94, 92, 0, 0, 0, 0,
- 0, 0, 0, 93, 92, 94, 95, 92, 95, 94, 92, 93, 0, 0, 0, 0,
- 0, 0, 0, 85, 90, 92, 93, 78, 93, 92, 90, 85, 0, 0, 0, 0,
- 0, 0, 0, 88, 85, 90, 88, 90, 88, 90, 85, 88, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- ], [
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0,206,208,207,213,214,213,207,208,206, 0, 0, 0, 0,
- 0, 0, 0,206,212,209,216,233,216,209,212,206, 0, 0, 0, 0,
- 0, 0, 0,206,208,207,214,216,214,207,208,206, 0, 0, 0, 0,
- 0, 0, 0,206,213,213,216,216,216,213,213,206, 0, 0, 0, 0,
- 0, 0, 0,208,211,211,214,215,214,211,211,208, 0, 0, 0, 0,
- 0, 0, 0,208,212,212,214,215,214,212,212,208, 0, 0, 0, 0,
- 0, 0, 0,204,209,204,212,214,212,204,209,204, 0, 0, 0, 0,
- 0, 0, 0,198,208,204,212,212,212,204,208,198, 0, 0, 0, 0,
- 0, 0, 0,200,208,206,212,200,212,206,208,200, 0, 0, 0, 0,
- 0, 0, 0,194,206,204,212,200,212,204,206,194, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- ], [
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0,100,100, 96, 91, 90, 91, 96,100,100, 0, 0, 0, 0,
- 0, 0, 0, 98, 98, 96, 92, 89, 92, 96, 98, 98, 0, 0, 0, 0,
- 0, 0, 0, 97, 97, 96, 91, 92, 91, 96, 97, 97, 0, 0, 0, 0,
- 0, 0, 0, 96, 99, 99, 98,100, 98, 99, 99, 96, 0, 0, 0, 0,
- 0, 0, 0, 96, 96, 96, 96,100, 96, 96, 96, 96, 0, 0, 0, 0,
- 0, 0, 0, 95, 96, 99, 96,100, 96, 99, 96, 95, 0, 0, 0, 0,
- 0, 0, 0, 96, 96, 96, 96, 96, 96, 96, 96, 96, 0, 0, 0, 0,
- 0, 0, 0, 97, 96,100, 99,101, 99,100, 96, 97, 0, 0, 0, 0,
- 0, 0, 0, 96, 97, 98, 98, 98, 98, 98, 97, 96, 0, 0, 0, 0,
- 0, 0, 0, 96, 96, 97, 99, 99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}
- function IN_FORT(sq) {
- return IN_FORT_[sq] != 0;
- }
- function RANK_Y(sq) {
- return sq >> 4;
- }
- function FILE_X(sq) {
- return sq & 15;
- }
- // 将二维矩阵转换为一维矩阵
- function COORD_XY(x, y) {
- return x + (y << 4);
- }
- function SQUARE_FLIP(sq) {
- return 254 - sq;
- }
- function FILE_FLIP(x) {
- return 14 - x;
- }
- function RANK_FLIP(y) {
- return 15 - y;
- }
- function MIRROR_SQUARE(sq) {
- return COORD_XY(FILE_FLIP(FILE_X(sq)), RANK_Y(sq));
- }
- function SQUARE_FORWARD(sq, sd) {
- return sq - 16 + (sd << 5);
- }
- function KING_SPAN(sqSrc, sqDst) {
- return LEGAL_SPAN[sqDst - sqSrc + 256] == 1;
- }
- function ADVISOR_SPAN(sqSrc, sqDst) {
- return LEGAL_SPAN[sqDst - sqSrc + 256] == 2;
- }
- function BISHOP_SPAN(sqSrc, sqDst) {
- return LEGAL_SPAN[sqDst - sqSrc + 256] == 3;
- }
- function BISHOP_PIN(sqSrc, sqDst) {
- return (sqSrc + sqDst) >> 1;
- }
- function KNIGHT_PIN(sqSrc, sqDst) {
- return sqSrc + KNIGHT_PIN_[sqDst - sqSrc + 256];
- }
- function HOME_HALF(sq, sd) {
- return (sq & 0x80) != (sd << 7);
- }
- function AWAY_HALF(sq, sd) {
- return (sq & 0x80) == (sd << 7);
- }
- // 如果从起点sqSrc到终点sqDst没有过河,则返回true;否则返回false
- function SAME_HALF(sqSrc, sqDst) {
- return ((sqSrc ^ sqDst) & 0x80) == 0;
- }
- function SAME_RANK(sqSrc, sqDst) {
- return ((sqSrc ^ sqDst) & 0xf0) == 0;
- }
- function SAME_FILE(sqSrc, sqDst) {
- return ((sqSrc ^ sqDst) & 0x0f) == 0;
- }
- function SIDE_TAG(sd) {
- return 8 + (sd << 3);
- }
- function OPP_SIDE_TAG(sd) {
- return 16 - (sd << 3);
- }
- function SRC(mv) {
- return mv & 255;
- }
- function DST(mv) {
- return mv >> 8;
- }
- function MOVE(sqSrc, sqDst) {
- return sqSrc + (sqDst << 8);
- }
- function MIRROR_MOVE(mv) {
- return MOVE(MIRROR_SQUARE(SRC(mv)), MIRROR_SQUARE(DST(mv)));
- }
- function MVV_LVA(pc, lva) {
- return MVV_VALUE[pc & 7] - lva;
- }
- function CHR(n) {
- return String.fromCharCode(n);
- }
- function ASC(c) {
- return c.charCodeAt(0);
- }
- var FEN_PIECE = " KABNRCP kabnrcp ";
- function CHAR_TO_PIECE(c) {
- switch (c) {
- case "K":
- return PIECE_KING; // 将
- case "A":
- return PIECE_ADVISOR; // 士
- case "B":
- case "E":
- return PIECE_BISHOP; // 象
- case "H":
- case "N":
- return PIECE_KNIGHT; // 马
- case "R":
- return PIECE_ROOK; // 车
- case "C":
- return PIECE_CANNON; // 炮
- case "P":
- return PIECE_PAWN; //卒
- default:
- return -1;
- }
- }
- function RC4(key) {
- this.x = this.y = 0;
- this.state = [];
- for (var i = 0; i < 256; i ++) {
- this.state.push(i);
- }
- var j = 0;
- for (var i = 0; i < 256; i ++) {
- j = (j + this.state[i] + key[i % key.length]) & 0xff;
- this.swap(i, j);
- }
- }
- RC4.prototype.swap = function(i, j) {
- var t = this.state[i];
- this.state[i] = this.state[j];
- this.state[j] = t;
- }
- RC4.prototype.nextByte = function() {
- this.x = (this.x + 1) & 0xff;
- this.y = (this.y + this.state[this.x]) & 0xff;
- this.swap(this.x, this.y);
- var t = (this.state[this.x] + this.state[this.y]) & 0xff;
- return this.state[t];
- }
- RC4.prototype.nextLong = function() {
- var n0 = this.nextByte();
- var n1 = this.nextByte();
- var n2 = this.nextByte();
- var n3 = this.nextByte();
- return n0 + (n1 << 8) + (n2 << 16) + ((n3 << 24) & 0xffffffff);
- }
- var PreGen_zobristKeyPlayer, PreGen_zobristLockPlayer;
- var PreGen_zobristKeyTable = [], PreGen_zobristLockTable = [];
- var rc4 = new RC4([0]);
- PreGen_zobristKeyPlayer = rc4.nextLong();
- rc4.nextLong();
- PreGen_zobristLockPlayer = rc4.nextLong();
- for (var i = 0; i < 14; i ++) {
- var keys = [];
- var locks = [];
- for (var j = 0; j < 256; j ++) {
- keys.push(rc4.nextLong());
- rc4.nextLong();
- locks.push(rc4.nextLong());
- }
- PreGen_zobristKeyTable.push(keys);
- PreGen_zobristLockTable.push(locks);
- }
- // function Position() {
- // // sdPlayer, zobristKey, zobristLock, vlWhite, vlBlack, distance;
- // // squares, mvList, pcList, keyList, chkList;
- // }
- //module.exports = Position;
|