ChessPos.ts 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. import { _decorator } from "cc";
  2. const { ccclass } = _decorator;
  3. @ccclass("ChessPos")
  4. export default class ChessPos {
  5. clearBoard = function() {
  6. this.sdPlayer = 0;
  7. this.squares = []; //这就是一维棋局数组。
  8. for (var sq = 0; sq < 256; sq ++) {
  9. this.squares.push(0);
  10. }
  11. this.zobristKey = this.zobristLock = 0;
  12. this.vlWhite = this.vlBlack = 0;
  13. };
  14. mvList:any[] = []
  15. pcList:any[] = []
  16. keyList:any[] = []
  17. chkList:any[] = []
  18. squares:any[] = []
  19. distance:number = 0
  20. vlWhite:number = 0
  21. vlBlack:number = 0
  22. zobristKey:number = 0
  23. zobristLock:number = 0
  24. sdPlayer:number = 0
  25. setIrrev() {
  26. this.mvList = [0];
  27. this.pcList = [0];
  28. this.keyList = [0];
  29. this.chkList = [this.checked()];
  30. this.distance = 0;
  31. }
  32. // 如果bDel为false,则将棋子pc添加进棋局中的sp位置;如果bDel为true,则删除sp位置的棋子。
  33. private addPiece(sq, pc, bDel) {
  34. var pcAdjust;
  35. this.squares[sq] = bDel ? 0 : pc;
  36. if(pc<8){
  37. console.log("addPiece error",pc)
  38. }
  39. if (pc < 16) {
  40. pcAdjust = pc - 8;
  41. this.vlWhite += bDel ? -PIECE_VALUE[pcAdjust][sq] :
  42. PIECE_VALUE[pcAdjust][sq];
  43. } else {
  44. pcAdjust = pc - 16;
  45. this.vlBlack += bDel ? -PIECE_VALUE[pcAdjust][SQUARE_FLIP(sq)] :
  46. PIECE_VALUE[pcAdjust][SQUARE_FLIP(sq)];
  47. pcAdjust += 7;
  48. }
  49. this.zobristKey ^= PreGen_zobristKeyTable[pcAdjust][sq];
  50. this.zobristLock ^= PreGen_zobristLockTable[pcAdjust][sq];
  51. }
  52. public pushMvList(mv){
  53. // var sqSrc = SRC(mv);
  54. // var sqDst = DST(mv);
  55. this.mvList.push(mv);
  56. this.distance ++;
  57. }
  58. private movePiece(mv) {
  59. var sqSrc = SRC(mv);
  60. var sqDst = DST(mv);
  61. var pc = this.squares[sqDst];
  62. this.pcList.push(pc);
  63. if (pc > 0) {
  64. this.addPiece(sqDst, pc, DEL_PIECE);
  65. }
  66. pc = this.squares[sqSrc];
  67. if(pc==0){
  68. console.log("pc error == ",this.squares,sqDst)
  69. }else{
  70. this.addPiece(sqSrc, pc, DEL_PIECE);
  71. this.addPiece(sqDst, pc, ADD_PIECE);
  72. }
  73. this.mvList.push(mv);
  74. }
  75. private undoMovePiece() {
  76. var mv = this.mvList.pop();
  77. var sqSrc = SRC(mv);
  78. var sqDst = DST(mv);
  79. var pc = this.squares[sqDst];
  80. this.addPiece(sqDst, pc, DEL_PIECE);
  81. this.addPiece(sqSrc, pc, ADD_PIECE);
  82. pc = this.pcList.pop();
  83. if (pc > 0) {
  84. this.addPiece(sqDst, pc, ADD_PIECE);
  85. }
  86. }
  87. public changeSide() {
  88. this.sdPlayer = 1 - this.sdPlayer;
  89. this.zobristKey ^= PreGen_zobristKeyPlayer;
  90. this.zobristLock ^= PreGen_zobristLockPlayer;
  91. }
  92. public makeMove(mv) {
  93. var zobristKey = this.zobristKey;
  94. this.movePiece(mv);
  95. // 检查走棋是否被将军。如果是,说明这是在送死,撤销走棋并返回false。
  96. if (this.checked()) {
  97. this.undoMovePiece(); // 撤销棋子移动
  98. return false;
  99. }
  100. this.keyList.push(zobristKey);
  101. this.changeSide();
  102. this.chkList.push(this.checked());
  103. this.distance ++;
  104. return true;
  105. }
  106. public undoMakeMove() {
  107. this.distance --;
  108. this.chkList.pop();
  109. this.changeSide();
  110. this.keyList.pop();
  111. this.undoMovePiece();
  112. }
  113. public nullMove() {
  114. this.mvList.push(0);
  115. this.pcList.push(0);
  116. this.keyList.push(this.zobristKey);
  117. this.changeSide();
  118. this.chkList.push(false);
  119. this.distance ++;
  120. }
  121. public undoNullMove() {
  122. this.distance --;
  123. this.chkList.pop();
  124. this.changeSide();
  125. this.keyList.pop();
  126. this.pcList.pop();
  127. this.mvList.pop();
  128. }
  129. //通过FEN串初始化棋局,也就是将参数fen表示的棋局,转化为一维棋局数组squares表示的棋局。
  130. public fromFen(fen) {
  131. this.clearBoard();
  132. var y = RANK_TOP;
  133. var x = FILE_LEFT;
  134. var index = 0;
  135. if (index == fen.length) {
  136. this.setIrrev();
  137. return;
  138. }
  139. var c = fen.charAt(index);
  140. while (c != " ") {
  141. if (c == "/") {
  142. x = FILE_LEFT;
  143. y ++;
  144. if (y > RANK_BOTTOM) {
  145. break;
  146. }
  147. } else if (c >= "1" && c <= "9") {
  148. x += (ASC(c) - ASC("0"));
  149. } else if (c >= "A" && c <= "Z") {
  150. if (x <= FILE_RIGHT) {
  151. var pt = CHAR_TO_PIECE(c);
  152. if (pt >= 0) {
  153. this.addPiece(COORD_XY(x, y), pt + 8,null);
  154. }
  155. x ++;
  156. }
  157. } else if (c >= "a" && c <= "z") {
  158. if (x <= FILE_RIGHT) {
  159. var pt = CHAR_TO_PIECE(CHR(ASC(c) + ASC("A") - ASC("a")));
  160. if (pt >= 0) {
  161. this.addPiece(COORD_XY(x, y), pt + 16,null);
  162. }
  163. x ++;
  164. }
  165. }
  166. index ++;
  167. if (index == fen.length) {
  168. this.setIrrev();
  169. return;
  170. }
  171. c = fen.charAt(index);
  172. }
  173. index ++;
  174. if (index == fen.length) {
  175. this.setIrrev();
  176. return;
  177. }
  178. if (this.sdPlayer == (fen.charAt(index) == "b" ? 0 : 1)) {
  179. this.changeSide();
  180. }
  181. this.setIrrev();
  182. }
  183. public toFen() {
  184. var fen = "";
  185. for (var y = RANK_TOP; y <= RANK_BOTTOM; y ++) {
  186. var k = 0;
  187. for (var x = FILE_LEFT; x <= FILE_RIGHT; x ++) {
  188. var pc = this.squares[COORD_XY(x, y)];
  189. if (pc > 0) {
  190. if (k > 0) {
  191. fen += CHR(ASC("0") + k);
  192. k = 0;
  193. }
  194. fen += FEN_PIECE.charAt(pc);
  195. } else {
  196. k ++;
  197. }
  198. }
  199. if (k > 0) {
  200. fen += CHR(ASC("0") + k);
  201. }
  202. fen += "/";
  203. }
  204. return fen.substring(0, fen.length - 1) +
  205. (this.sdPlayer == 0 ? " w" : " b");
  206. }
  207. public generateMoves(vls) {
  208. var mvs = [];
  209. var pcSelfSide = SIDE_TAG(this.sdPlayer);
  210. var pcOppSide = OPP_SIDE_TAG(this.sdPlayer);
  211. for (var sqSrc = 0; sqSrc < 256; sqSrc ++) {
  212. var pcSrc = this.squares[sqSrc];
  213. if ((pcSrc & pcSelfSide) == 0) {
  214. continue;
  215. }
  216. switch (pcSrc - pcSelfSide) {
  217. case PIECE_KING:
  218. for (var i = 0; i < 4; i ++) {
  219. var sqDst = sqSrc + KING_DELTA[i];
  220. if (!IN_FORT(sqDst)) {
  221. continue;
  222. }
  223. var pcDst = this.squares[sqDst];
  224. if (vls == null) {
  225. if ((pcDst & pcSelfSide) == 0) {
  226. mvs.push(MOVE(sqSrc, sqDst));
  227. }
  228. } else if ((pcDst & pcOppSide) != 0) {
  229. mvs.push(MOVE(sqSrc, sqDst));
  230. vls.push(MVV_LVA(pcDst, 5));
  231. }
  232. }
  233. break;
  234. case PIECE_ADVISOR:
  235. for (var i = 0; i < 4; i ++) {
  236. var sqDst = sqSrc + ADVISOR_DELTA[i];
  237. if (!IN_FORT(sqDst)) {
  238. continue;
  239. }
  240. var pcDst = this.squares[sqDst];
  241. if (vls == null) {
  242. if ((pcDst & pcSelfSide) == 0) {
  243. mvs.push(MOVE(sqSrc, sqDst));
  244. }
  245. } else if ((pcDst & pcOppSide) != 0) {
  246. mvs.push(MOVE(sqSrc, sqDst));
  247. vls.push(MVV_LVA(pcDst, 1));
  248. }
  249. }
  250. break;
  251. case PIECE_BISHOP:
  252. for (var i = 0; i < 4; i ++) {
  253. var sqDst = sqSrc + ADVISOR_DELTA[i];
  254. if (!(IN_BOARD(sqDst) && HOME_HALF(sqDst, this.sdPlayer) &&
  255. this.squares[sqDst] == 0)) {
  256. continue;
  257. }
  258. sqDst += ADVISOR_DELTA[i];
  259. var pcDst = this.squares[sqDst];
  260. if (vls == null) {
  261. if ((pcDst & pcSelfSide) == 0) {
  262. mvs.push(MOVE(sqSrc, sqDst));
  263. }
  264. } else if ((pcDst & pcOppSide) != 0) {
  265. mvs.push(MOVE(sqSrc, sqDst));
  266. vls.push(MVV_LVA(pcDst, 1));
  267. }
  268. }
  269. break;
  270. case PIECE_KNIGHT:
  271. for (var i = 0; i < 4; i ++) {
  272. var sqDst = sqSrc + KING_DELTA[i];
  273. if (this.squares[sqDst] > 0) {
  274. continue;
  275. }
  276. for (var j = 0; j < 2; j ++) {
  277. sqDst = sqSrc + KNIGHT_DELTA[i][j];
  278. if (!IN_BOARD(sqDst)) {
  279. continue;
  280. }
  281. var pcDst = this.squares[sqDst];
  282. if (vls == null) {
  283. if ((pcDst & pcSelfSide) == 0) {
  284. mvs.push(MOVE(sqSrc, sqDst));
  285. }
  286. } else if ((pcDst & pcOppSide) != 0) {
  287. mvs.push(MOVE(sqSrc, sqDst));
  288. vls.push(MVV_LVA(pcDst, 1));
  289. }
  290. }
  291. }
  292. break;
  293. case PIECE_ROOK:
  294. for (var i = 0; i < 4; i ++) {
  295. var delta = KING_DELTA[i];
  296. var sqDst = sqSrc + delta;
  297. while (IN_BOARD(sqDst)) {
  298. var pcDst = this.squares[sqDst];
  299. if (pcDst == 0) {
  300. if (vls == null) {
  301. mvs.push(MOVE(sqSrc, sqDst));
  302. }
  303. } else {
  304. if ((pcDst & pcOppSide) != 0) {
  305. mvs.push(MOVE(sqSrc, sqDst));
  306. if (vls != null) {
  307. vls.push(MVV_LVA(pcDst, 4));
  308. }
  309. }
  310. break;
  311. }
  312. sqDst += delta;
  313. }
  314. }
  315. break;
  316. case PIECE_CANNON:
  317. for (var i = 0; i < 4; i ++) {
  318. var delta = KING_DELTA[i];
  319. var sqDst = sqSrc + delta;
  320. while (IN_BOARD(sqDst)) {
  321. var pcDst = this.squares[sqDst];
  322. if (pcDst == 0) {
  323. if (vls == null) {
  324. mvs.push(MOVE(sqSrc, sqDst));
  325. }
  326. } else {
  327. break;
  328. }
  329. sqDst += delta;
  330. }
  331. sqDst += delta;
  332. while (IN_BOARD(sqDst)) {
  333. var pcDst = this.squares[sqDst];
  334. if (pcDst > 0) {
  335. if ((pcDst & pcOppSide) != 0) {
  336. mvs.push(MOVE(sqSrc, sqDst));
  337. if (vls != null) {
  338. vls.push(MVV_LVA(pcDst, 4));
  339. }
  340. }
  341. break;
  342. }
  343. sqDst += delta;
  344. }
  345. }
  346. break;
  347. case PIECE_PAWN:
  348. var sqDst = SQUARE_FORWARD(sqSrc, this.sdPlayer);
  349. if (IN_BOARD(sqDst)) {
  350. var pcDst = this.squares[sqDst];
  351. if (vls == null) {
  352. if ((pcDst & pcSelfSide) == 0) {
  353. mvs.push(MOVE(sqSrc, sqDst));
  354. }
  355. } else if ((pcDst & pcOppSide) != 0) {
  356. mvs.push(MOVE(sqSrc, sqDst));
  357. vls.push(MVV_LVA(pcDst, 2));
  358. }
  359. }
  360. if (AWAY_HALF(sqSrc, this.sdPlayer)) {
  361. for (var delta = -1; delta <= 1; delta += 2) {
  362. sqDst = sqSrc + delta;
  363. if (IN_BOARD(sqDst)) {
  364. var pcDst = this.squares[sqDst];
  365. if (vls == null) {
  366. if ((pcDst & pcSelfSide) == 0) {
  367. mvs.push(MOVE(sqSrc, sqDst));
  368. }
  369. } else if ((pcDst & pcOppSide) != 0) {
  370. mvs.push(MOVE(sqSrc, sqDst));
  371. vls.push(MVV_LVA(pcDst, 2));
  372. }
  373. }
  374. }
  375. }
  376. break;
  377. }
  378. }
  379. return mvs;
  380. }
  381. public findAllCanMove(sq){
  382. var sqSrc = sq;
  383. var pcSrc = this.squares[sqSrc];
  384. var temp_array = [];
  385. var pcSelfSide = SIDE_TAG(this.sdPlayer);
  386. var pcOppSide = OPP_SIDE_TAG(this.sdPlayer);
  387. if ((pcSrc & pcSelfSide) == 0) {
  388. return temp_array;
  389. }
  390. switch (pcSrc - pcSelfSide) {
  391. case PIECE_KING: //寻找当前将所有可以走的路
  392. {
  393. var des_array = []
  394. des_array.push(sqSrc - 16)
  395. des_array.push(sqSrc - 1)
  396. des_array.push(sqSrc + 16)
  397. des_array.push(sqSrc + 1)
  398. for(var i=0;i<des_array.length;i++){
  399. if(IN_BOARD(des_array[i])){
  400. var b =this.legalMove(MOVE(sqSrc,des_array[i]))
  401. if(b){
  402. temp_array.push(des_array[i])
  403. }
  404. }
  405. }
  406. }
  407. break;
  408. case PIECE_ADVISOR:// 寻找当前士所有可以走的路
  409. {
  410. var des_array = []
  411. des_array.push(sqSrc - 17)
  412. des_array.push(sqSrc - 15)
  413. des_array.push(sqSrc + 17)
  414. des_array.push(sqSrc + 15)
  415. for(var i=0;i<des_array.length;i++){
  416. if(IN_BOARD(des_array[i])){
  417. var b =this.legalMove(MOVE(sqSrc,des_array[i]))
  418. if(b){
  419. temp_array.push(des_array[i])
  420. }
  421. }
  422. }
  423. }
  424. break;
  425. case PIECE_BISHOP: // 寻找当前象所有可以走的路
  426. {
  427. var des_array = []
  428. des_array.push(sqSrc - 34)
  429. des_array.push(sqSrc - 30)
  430. des_array.push(sqSrc + 30)
  431. des_array.push(sqSrc + 34)
  432. for(var i=0;i<des_array.length;i++){
  433. if(IN_BOARD(des_array[i])){
  434. var b =this.legalMove(MOVE(sqSrc,des_array[i]))
  435. if(b){
  436. temp_array.push(des_array[i])
  437. }
  438. }
  439. }
  440. }
  441. break;
  442. case PIECE_KNIGHT:// 寻找当前马所有可以走的路
  443. {
  444. var des_array = []
  445. des_array.push(sqSrc - 33)
  446. des_array.push(sqSrc - 31)
  447. des_array.push(sqSrc - 18)
  448. des_array.push(sqSrc + 14)
  449. des_array.push(sqSrc - 14)
  450. des_array.push(sqSrc + 18)
  451. des_array.push(sqSrc + 31)
  452. des_array.push(sqSrc + 33)
  453. for(var i=0;i<des_array.length;i++){
  454. if(IN_BOARD(des_array[i])){
  455. var b =this.legalMove(MOVE(sqSrc,des_array[i]))
  456. if(b){
  457. temp_array.push(des_array[i])
  458. }
  459. }
  460. }
  461. }
  462. break;
  463. case PIECE_CANNON:
  464. case PIECE_ROOK: //寻找当前 车 所有可以走的路
  465. {
  466. //var des_array = []
  467. //遍历列
  468. var left_index = 1;
  469. var isCannon = false;
  470. while (IN_BOARD(sqSrc-left_index)) {
  471. var pcLeftDst = this.squares[sqSrc-left_index]; //
  472. if(pcLeftDst!=0){
  473. if ((pcLeftDst & pcSelfSide) != 0) {
  474. //说明找到自己的棋子
  475. if((pcSrc - pcSelfSide) ==PIECE_ROOK ||isCannon){
  476. break;
  477. }else{
  478. isCannon = true;
  479. }
  480. }else if((pcLeftDst & pcOppSide) != 0){
  481. //说明找到敌方的棋子
  482. if((pcSrc - pcSelfSide) ==PIECE_ROOK){
  483. temp_array.push(sqSrc-left_index)
  484. break;
  485. }else{
  486. if(isCannon){
  487. temp_array.push(sqSrc-left_index)
  488. break;
  489. }else{
  490. isCannon = true;
  491. }
  492. }
  493. }
  494. }else{
  495. if(!isCannon){
  496. temp_array.push(sqSrc-left_index)
  497. }
  498. }
  499. left_index++;
  500. }
  501. var right_index = 1;
  502. isCannon = false;
  503. while (IN_BOARD(sqSrc+right_index)) {
  504. var pcRightDst = this.squares[sqSrc+right_index]; //
  505. if(pcRightDst!=0){
  506. if ((pcRightDst & pcSelfSide) != 0) {
  507. //说明找到自己的棋子
  508. if((pcSrc - pcSelfSide) ==PIECE_ROOK ||isCannon){
  509. break;
  510. }else{
  511. isCannon = true;
  512. }
  513. }else if((pcRightDst & pcOppSide) != 0){
  514. //说明找到敌方的棋子
  515. if((pcSrc - pcSelfSide) ==PIECE_ROOK){
  516. temp_array.push(sqSrc+right_index)
  517. break;
  518. }else{
  519. if(isCannon){
  520. temp_array.push(sqSrc+right_index)
  521. break;
  522. }else{
  523. isCannon = true;
  524. }
  525. }
  526. }
  527. }else{
  528. if(!isCannon){
  529. temp_array.push(sqSrc+right_index)
  530. }
  531. }
  532. right_index++;
  533. }
  534. //遍历行
  535. var top_index = 16;
  536. isCannon = false;
  537. while (IN_BOARD(sqSrc-top_index)) {
  538. var pcTopDst = this.squares[sqSrc-top_index]; //
  539. if(pcTopDst!=0){
  540. if ((pcTopDst & pcSelfSide) != 0) {
  541. //说明找到自己的棋子
  542. if((pcSrc - pcSelfSide) ==PIECE_ROOK ||isCannon){
  543. break;
  544. }else{
  545. isCannon = true;
  546. }
  547. }else if((pcTopDst & pcOppSide) != 0){
  548. //说明找到敌方的棋子
  549. if((pcSrc - pcSelfSide) ==PIECE_ROOK){
  550. temp_array.push(sqSrc-top_index)
  551. break;
  552. }else{
  553. if(isCannon){
  554. temp_array.push(sqSrc-top_index)
  555. break;
  556. }else{
  557. isCannon = true;
  558. }
  559. }
  560. }
  561. }else{
  562. if(!isCannon){
  563. temp_array.push(sqSrc-top_index)
  564. }
  565. }
  566. top_index+=16;
  567. }
  568. var bottom_index = 16;
  569. isCannon = false;
  570. while (IN_BOARD(sqSrc+bottom_index)) {
  571. var pcBottomDst = this.squares[sqSrc+bottom_index]; //
  572. if(pcBottomDst!=0){
  573. if ((pcBottomDst & pcSelfSide) != 0) {
  574. //说明找到自己的棋子
  575. if((pcSrc - pcSelfSide) ==PIECE_ROOK ||isCannon){
  576. break;
  577. }else{
  578. isCannon = true;
  579. }
  580. }else if((pcBottomDst & pcOppSide) != 0){
  581. //说明找到敌方的棋子
  582. if((pcSrc - pcSelfSide) ==PIECE_ROOK){
  583. temp_array.push(sqSrc+bottom_index)
  584. break;
  585. }else{
  586. if(isCannon){
  587. temp_array.push(sqSrc+bottom_index)
  588. break;
  589. }else{
  590. isCannon = true;
  591. }
  592. }
  593. }
  594. }else{
  595. if(!isCannon){
  596. temp_array.push(sqSrc+bottom_index)
  597. }
  598. }
  599. bottom_index+=16;
  600. }
  601. }
  602. break;
  603. case PIECE_PAWN: //寻找当前卒 所有可以走的路
  604. {
  605. var des_array = []
  606. des_array.push(sqSrc - 16)
  607. des_array.push(sqSrc - 1)
  608. des_array.push(sqSrc + 16)
  609. des_array.push(sqSrc + 1)
  610. for(i=0;i<des_array.length;i++){
  611. if(IN_BOARD(des_array[i])){
  612. var b =this.legalMove(MOVE(sqSrc,des_array[i]))
  613. if(b){
  614. temp_array.push(des_array[i])
  615. }
  616. }
  617. }
  618. }
  619. break;
  620. default:
  621. return temp_array;
  622. }
  623. return temp_array;
  624. }
  625. //判断步骤是否合法。合法返回true,非法返回false。
  626. public legalMove(mv) {
  627. var sqSrc = SRC(mv);
  628. var pcSrc = this.squares[sqSrc];
  629. var pcSelfSide = SIDE_TAG(this.sdPlayer);
  630. if ((pcSrc & pcSelfSide) == 0) {
  631. return false;
  632. }
  633. var sqDst = DST(mv);
  634. var pcDst = this.squares[sqDst];
  635. if ((pcDst & pcSelfSide) != 0) {
  636. return false;
  637. }
  638. switch (pcSrc - pcSelfSide) {
  639. case PIECE_KING:
  640. return IN_FORT(sqDst) && KING_SPAN(sqSrc, sqDst);
  641. case PIECE_ADVISOR:
  642. return IN_FORT(sqDst) && ADVISOR_SPAN(sqSrc, sqDst);
  643. case PIECE_BISHOP:
  644. return SAME_HALF(sqSrc, sqDst) && BISHOP_SPAN(sqSrc, sqDst) &&
  645. this.squares[BISHOP_PIN(sqSrc, sqDst)] == 0;
  646. case PIECE_KNIGHT:
  647. var sqPin = KNIGHT_PIN(sqSrc, sqDst);
  648. return sqPin != sqSrc && this.squares[sqPin] == 0;
  649. case PIECE_ROOK:
  650. case PIECE_CANNON:
  651. var delta;
  652. if (SAME_RANK(sqSrc, sqDst)) {
  653. delta = (sqDst < sqSrc ? -1 : 1);
  654. } else if (SAME_FILE(sqSrc, sqDst)) {
  655. delta = (sqDst < sqSrc ? -16 : 16);
  656. } else {
  657. return false;
  658. }
  659. var sqPin = sqSrc + delta;
  660. while (sqPin != sqDst && this.squares[sqPin] == 0) {
  661. sqPin += delta;
  662. }
  663. if (sqPin == sqDst) {
  664. return pcDst == 0 || pcSrc - pcSelfSide == PIECE_ROOK;
  665. }
  666. if (pcDst == 0 || pcSrc - pcSelfSide != PIECE_CANNON) {
  667. return false;
  668. }
  669. sqPin += delta;
  670. while (sqPin != sqDst && this.squares[sqPin] == 0) {
  671. sqPin += delta;
  672. }
  673. return sqPin == sqDst;
  674. case PIECE_PAWN:
  675. if (AWAY_HALF(sqDst, this.sdPlayer) && (sqDst == sqSrc - 1 || sqDst == sqSrc + 1)) {
  676. return true;
  677. }
  678. return sqDst == SQUARE_FORWARD(sqSrc, this.sdPlayer);
  679. default:
  680. return false;
  681. }
  682. }
  683. private checked() {
  684. var pcSelfSide = SIDE_TAG(this.sdPlayer);
  685. var pcOppSide = OPP_SIDE_TAG(this.sdPlayer);
  686. for (var sqSrc = 0; sqSrc < 256; sqSrc ++) {
  687. if (this.squares[sqSrc] != pcSelfSide + PIECE_KING) {
  688. continue;
  689. }
  690. if (this.squares[SQUARE_FORWARD(sqSrc, this.sdPlayer)] == pcOppSide + PIECE_PAWN) {
  691. return true;
  692. }
  693. for (var delta = -1; delta <= 1; delta += 2) {
  694. if (this.squares[sqSrc + delta] == pcOppSide + PIECE_PAWN) {
  695. return true;
  696. }
  697. }
  698. for (var i = 0; i < 4; i ++) {
  699. if (this.squares[sqSrc + ADVISOR_DELTA[i]] != 0) {
  700. continue;
  701. }
  702. for (var j = 0; j < 2; j ++) {
  703. var pcDst = this.squares[sqSrc + KNIGHT_CHECK_DELTA[i][j]];
  704. if (pcDst == pcOppSide + PIECE_KNIGHT) {
  705. return true;
  706. }
  707. }
  708. }
  709. for (var i = 0; i < 4; i ++) {
  710. var delta = KING_DELTA[i];
  711. var sqDst = sqSrc + delta;
  712. while (IN_BOARD(sqDst)) {
  713. var pcDst = this.squares[sqDst];
  714. if (pcDst > 0) {
  715. if (pcDst == pcOppSide + PIECE_ROOK || pcDst == pcOppSide + PIECE_KING) {
  716. return true;
  717. }
  718. break;
  719. }
  720. sqDst += delta;
  721. }
  722. sqDst += delta;
  723. while (IN_BOARD(sqDst)) {
  724. var pcDst = this.squares[sqDst];
  725. if (pcDst > 0) {
  726. if (pcDst == pcOppSide + PIECE_CANNON) {
  727. return true;
  728. }
  729. break;
  730. }
  731. sqDst += delta;
  732. }
  733. }
  734. return false;
  735. }
  736. return false;
  737. }
  738. isMate() {
  739. var mvs = this.generateMoves(null);
  740. for (var i = 0; i < mvs.length; i ++) {
  741. if (this.makeMove(mvs[i])) {
  742. this.undoMakeMove();
  743. return false;
  744. }
  745. }
  746. return true;
  747. }
  748. mateValue() {
  749. return this.distance - MATE_VALUE;
  750. }
  751. banValue() {
  752. return this.distance - BAN_VALUE;
  753. }
  754. drawValue() {
  755. return (this.distance & 1) == 0 ? -DRAW_VALUE : DRAW_VALUE;
  756. }
  757. evaluate() {
  758. var vl = (this.sdPlayer == 0 ? this.vlWhite - this.vlBlack :
  759. this.vlBlack - this.vlWhite) + ADVANCED_VALUE;
  760. return vl == this.drawValue() ? vl - 1 : vl;
  761. }
  762. nullOkay() {
  763. return (this.sdPlayer == 0 ? this.vlWhite : this.vlBlack) > NULL_OKAY_MARGIN;
  764. }
  765. nullSafe() {
  766. return (this.sdPlayer == 0 ? this.vlWhite : this.vlBlack) > NULL_SAFE_MARGIN;
  767. }
  768. inCheck() {
  769. return this.chkList[this.chkList.length - 1];
  770. }
  771. captured() {
  772. return this.pcList[this.pcList.length - 1] > 0;
  773. }
  774. repValue(vlRep) {
  775. var vlReturn = ((vlRep & 2) == 0 ? 0 : this.banValue()) +
  776. ((vlRep & 4) == 0 ? 0 : -this.banValue());
  777. return vlReturn == 0 ? this.drawValue() : vlReturn;
  778. }
  779. repStatus(recur_) {
  780. var recur = recur_;
  781. var selfSide = false;
  782. var perpCheck = true;
  783. var oppPerpCheck = true;
  784. var index = this.mvList.length - 1;
  785. while (this.mvList[index] > 0 && this.pcList[index] == 0) {
  786. if (selfSide) {
  787. perpCheck = perpCheck && this.chkList[index];
  788. if (this.keyList[index] == this.zobristKey) {
  789. recur --;
  790. if (recur == 0) {
  791. return 1 + (perpCheck ? 2 : 0) + (oppPerpCheck ? 4 : 0);
  792. }
  793. }
  794. } else {
  795. oppPerpCheck = oppPerpCheck && this.chkList[index];
  796. }
  797. selfSide = !selfSide;
  798. index --;
  799. }
  800. return 0;
  801. }
  802. mirror() {
  803. var pos = new ChessPos();
  804. pos.clearBoard();
  805. for (var sq = 0; sq < 256; sq ++) {
  806. var pc = this.squares[sq];
  807. if (pc > 0) {
  808. pos.addPiece(MIRROR_SQUARE(sq), pc,null);
  809. }
  810. }
  811. if (this.sdPlayer == 1) {
  812. pos.changeSide();
  813. }
  814. return pos;
  815. }
  816. bookMove() {
  817. return 0;
  818. // if (typeof BOOK_DAT != "object" || BOOK_DAT.length == 0) {
  819. // return 0;
  820. // }
  821. // var mirror = false;
  822. // var lock = this.zobristLock >>> 1; // Convert into Unsigned
  823. // var index = binarySearch(BOOK_DAT, lock);
  824. // if (index < 0) {
  825. // mirror = true;
  826. // lock = this.mirror().zobristLock >>> 1; // Convert into Unsigned
  827. // index = binarySearch(BOOK_DAT, lock);
  828. // }
  829. // if (index < 0) {
  830. // return 0;
  831. // }
  832. // index --;
  833. // while (index >= 0 && BOOK_DAT[index][0] == lock) {
  834. // index --;
  835. // }
  836. // var mvs = [], vls = [];
  837. // var value = 0;
  838. // index ++;
  839. // while (index < BOOK_DAT.length && BOOK_DAT[index][0] == lock) {
  840. // var mv = BOOK_DAT[index][1];
  841. // mv = (mirror ? MIRROR_MOVE(mv) : mv);
  842. // if (this.legalMove(mv)) {
  843. // mvs.push(mv);
  844. // var vl = BOOK_DAT[index][2];
  845. // vls.push(vl);
  846. // value += vl;
  847. // }
  848. // index ++;
  849. // }
  850. // if (value == 0) {
  851. // return 0;
  852. // }
  853. // value = Math.floor(Math.random() * value);
  854. // for (index = 0; index < mvs.length; index ++) {
  855. // value -= vls[index];
  856. // if (value < 0) {
  857. // break;
  858. // }
  859. // }
  860. // return mvs[index];
  861. }
  862. historyIndex(mv) {
  863. return ((this.squares[SRC(mv)] - 8) << 8) + DST(mv);
  864. }
  865. }
  866. function binarySearch(vlss, vl) {
  867. var low = 0;
  868. var high = vlss.length - 1;
  869. while (low <= high) {
  870. var mid = (low + high) >> 1;
  871. if (vlss[mid][0] < vl) {
  872. low = mid + 1;
  873. } else if (vlss[mid][0] > vl) {
  874. high = mid - 1;
  875. } else {
  876. return mid;
  877. }
  878. }
  879. return -1;
  880. }
  881. var MATE_VALUE = 10000;
  882. var BAN_VALUE = MATE_VALUE - 100;
  883. var WIN_VALUE = MATE_VALUE - 200;
  884. var NULL_SAFE_MARGIN = 400;
  885. var NULL_OKAY_MARGIN = 200;
  886. var DRAW_VALUE = 20;
  887. var ADVANCED_VALUE = 3;
  888. /** !#en This is a KING.
  889. !#zh 这是一个 將 */
  890. var PIECE_KING = 0;
  891. /** !#en This is a ADVISOR.
  892. !#zh 这是一个 士 */
  893. var PIECE_ADVISOR = 1;
  894. /** !#en This is a BISHOP.
  895. !#zh 这是一个 象 */
  896. var PIECE_BISHOP = 2;
  897. /** !#en This is a KNIGHT.
  898. !#zh 这是一个 馬 */
  899. var PIECE_KNIGHT = 3;
  900. /** !#en This is a ROOK.
  901. !#zh 这是一个 车 */
  902. var PIECE_ROOK = 4;// 车
  903. /** !#en This is a CANNON.
  904. !#zh 这是一个 炮 */
  905. var PIECE_CANNON = 5;
  906. /** !#en This is a PAWN.
  907. !#zh 这是一个 卒 */
  908. var PIECE_PAWN = 6;
  909. var RANK_TOP = 3;
  910. var RANK_BOTTOM = 12;
  911. var FILE_LEFT = 3;
  912. var FILE_RIGHT = 11;
  913. var ADD_PIECE = false;
  914. var DEL_PIECE = true;
  915. /** !#en This is a init board 16*16 size.
  916. !#zh 这是一个初始棋盤的大小 在程序中,棋局被表示为大小为256的一维数组,一半棋盘位于0到127,另一半位于128到255。
  917. 128的二进制是1000 0000,右起第八位是1。128到255这些数的二进制,右起第8位都是1;0到127这些数的二进制,右起第8位都是0。 */
  918. var IN_BOARD_ = [
  919. /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //15
  920. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //31
  921. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //47
  922. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  923. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  924. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  925. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  926. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, /* 127 */
  927. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  928. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  929. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  930. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  931. 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0,
  1016. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1017. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1018. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1019. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1020. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1021. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1022. 0, 0, 0, 0, 0, 0, 0,
  1023. ];
  1024. var KING_DELTA = [-16, -1, 1, 16];
  1025. var ADVISOR_DELTA = [-17, -15, 15, 17];
  1026. var KNIGHT_DELTA = [[-33, -31], [-18, 14], [-14, 18], [31, 33]];
  1027. var KNIGHT_CHECK_DELTA = [[-33, -18], [-31, -14], [14, 31], [18, 33]];
  1028. var MVV_VALUE = [50, 10, 10, 30, 40, 30, 20, 0];
  1029. var PIECE_VALUE = [
  1030. [
  1031. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1032. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1033. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1034. 0, 0, 0, 9, 9, 9, 11, 13, 11, 9, 9, 9, 0, 0, 0, 0,
  1035. 0, 0, 0, 19, 24, 34, 42, 44, 42, 34, 24, 19, 0, 0, 0, 0,
  1036. 0, 0, 0, 19, 24, 32, 37, 37, 37, 32, 24, 19, 0, 0, 0, 0,
  1037. 0, 0, 0, 19, 23, 27, 29, 30, 29, 27, 23, 19, 0, 0, 0, 0,
  1038. 0, 0, 0, 14, 18, 20, 27, 29, 27, 20, 18, 14, 0, 0, 0, 0,
  1039. 0,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 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1080. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1081. ], [
  1082. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1083. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1084. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1085. 0, 0, 0, 90, 90, 90, 96, 90, 96, 90, 90, 90, 0, 0, 0, 0,
  1086. 0, 0, 0, 90, 96,103, 97, 94, 97,103, 96, 90, 0, 0, 0, 0,
  1087. 0, 0, 0, 92, 98, 99,103, 99,103, 99, 98, 92, 0, 0, 0, 0,
  1088. 0, 0, 0, 93,108,100,107,100,107,100,108, 93, 0, 0, 0, 0,
  1089. 0, 0, 0, 90,100, 99,103,104,103, 99,100, 90, 0, 0, 0, 0,
  1090. 0, 0, 0, 90, 98,101,102,103,102,101, 98, 90, 0, 0, 0, 0,
  1091. 0, 0, 0, 92, 94, 98, 95, 98, 95, 98, 94, 92, 0, 0, 0, 0,
  1092. 0, 0, 0, 93, 92, 94, 95, 92, 95, 94, 92, 93, 0, 0, 0, 0,
  1093. 0, 0, 0, 85, 90, 92, 93, 78, 93, 92, 90, 85, 0, 0, 0, 0,
  1094. 0, 0, 0, 88, 85, 90, 88, 90, 88, 90, 85, 88, 0, 0, 0, 0,
  1095. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1096. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1097. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1098. ], [
  1099. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1100. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1101. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1102. 0, 0, 0,206,208,207,213,214,213,207,208,206, 0, 0, 0, 0,
  1103. 0, 0, 0,206,212,209,216,233,216,209,212,206, 0, 0, 0, 0,
  1104. 0, 0, 0,206,208,207,214,216,214,207,208,206, 0, 0, 0, 0,
  1105. 0, 0, 0,206,213,213,216,216,216,213,213,206, 0, 0, 0, 0,
  1106. 0, 0, 0,208,211,211,214,215,214,211,211,208, 0, 0, 0, 0,
  1107. 0, 0, 0,208,212,212,214,215,214,212,212,208, 0, 0, 0, 0,
  1108. 0, 0, 0,204,209,204,212,214,212,204,209,204, 0, 0, 0, 0,
  1109. 0, 0, 0,198,208,204,212,212,212,204,208,198, 0, 0, 0, 0,
  1110. 0, 0, 0,200,208,206,212,200,212,206,208,200, 0, 0, 0, 0,
  1111. 0, 0, 0,194,206,204,212,200,212,204,206,194, 0, 0, 0, 0,
  1112. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1113. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1114. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1115. ], [
  1116. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1117. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1118. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1119. 0, 0, 0,100,100, 96, 91, 90, 91, 96,100,100, 0, 0, 0, 0,
  1120. 0, 0, 0, 98, 98, 96, 92, 89, 92, 96, 98, 98, 0, 0, 0, 0,
  1121. 0, 0, 0, 97, 97, 96, 91, 92, 91, 96, 97, 97, 0, 0, 0, 0,
  1122. 0, 0, 0, 96, 99, 99, 98,100, 98, 99, 99, 96, 0, 0, 0, 0,
  1123. 0, 0, 0, 96, 96, 96, 96,100, 96, 96, 96, 96, 0, 0, 0, 0,
  1124. 0, 0, 0, 95, 96, 99, 96,100, 96, 99, 96, 95, 0, 0, 0, 0,
  1125. 0, 0, 0, 96, 96, 96, 96, 96, 96, 96, 96, 96, 0, 0, 0, 0,
  1126. 0, 0, 0, 97, 96,100, 99,101, 99,100, 96, 97, 0, 0, 0, 0,
  1127. 0, 0, 0, 96, 97, 98, 98, 98, 98, 98, 97, 96, 0, 0, 0, 0,
  1128. 0, 0, 0, 96, 96, 97, 99, 99, 99, 97, 96, 96, 0, 0, 0, 0,
  1129. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1130. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1131. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1132. ], [
  1133. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1134. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1135. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1136. 0, 0, 0, 9, 9, 9, 11, 13, 11, 9, 9, 9, 0, 0, 0, 0,
  1137. 0, 0, 0, 19, 24, 34, 42, 44, 42, 34, 24, 19, 0, 0, 0, 0,
  1138. 0, 0, 0, 19, 24, 32, 37, 37, 37, 32, 24, 19, 0, 0, 0, 0,
  1139. 0, 0, 0, 19, 23, 27, 29, 30, 29, 27, 23, 19, 0, 0, 0, 0,
  1140. 0, 0, 0, 14, 18, 20, 27, 29, 27, 20, 18, 14, 0, 0, 0, 0,
  1141. 0, 0, 0, 7, 0, 13, 0, 16, 0, 13, 0, 7, 0, 0, 0, 0,
  1142. 0, 0, 0, 7, 0, 7, 0, 15, 0, 7, 0, 7, 0, 0, 0, 0,
  1143. 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
  1144. 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0,
  1145. 0, 0, 0, 0, 0, 0, 11, 15, 11, 0, 0, 0, 0, 0, 0, 0,
  1146. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1147. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1148. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1149. ],
  1150. ];
  1151. function IN_BOARD(sq) {
  1152. return IN_BOARD_[sq] != 0;
  1153. }
  1154. function IN_FORT(sq) {
  1155. return IN_FORT_[sq] != 0;
  1156. }
  1157. function RANK_Y(sq) {
  1158. return sq >> 4;
  1159. }
  1160. function FILE_X(sq) {
  1161. return sq & 15;
  1162. }
  1163. // 将二维矩阵转换为一维矩阵
  1164. function COORD_XY(x, y) {
  1165. return x + (y << 4);
  1166. }
  1167. function SQUARE_FLIP(sq) {
  1168. return 254 - sq;
  1169. }
  1170. function FILE_FLIP(x) {
  1171. return 14 - x;
  1172. }
  1173. function RANK_FLIP(y) {
  1174. return 15 - y;
  1175. }
  1176. function MIRROR_SQUARE(sq) {
  1177. return COORD_XY(FILE_FLIP(FILE_X(sq)), RANK_Y(sq));
  1178. }
  1179. function SQUARE_FORWARD(sq, sd) {
  1180. return sq - 16 + (sd << 5);
  1181. }
  1182. function KING_SPAN(sqSrc, sqDst) {
  1183. return LEGAL_SPAN[sqDst - sqSrc + 256] == 1;
  1184. }
  1185. function ADVISOR_SPAN(sqSrc, sqDst) {
  1186. return LEGAL_SPAN[sqDst - sqSrc + 256] == 2;
  1187. }
  1188. function BISHOP_SPAN(sqSrc, sqDst) {
  1189. return LEGAL_SPAN[sqDst - sqSrc + 256] == 3;
  1190. }
  1191. function BISHOP_PIN(sqSrc, sqDst) {
  1192. return (sqSrc + sqDst) >> 1;
  1193. }
  1194. function KNIGHT_PIN(sqSrc, sqDst) {
  1195. return sqSrc + KNIGHT_PIN_[sqDst - sqSrc + 256];
  1196. }
  1197. function HOME_HALF(sq, sd) {
  1198. return (sq & 0x80) != (sd << 7);
  1199. }
  1200. function AWAY_HALF(sq, sd) {
  1201. return (sq & 0x80) == (sd << 7);
  1202. }
  1203. // 如果从起点sqSrc到终点sqDst没有过河,则返回true;否则返回false
  1204. function SAME_HALF(sqSrc, sqDst) {
  1205. return ((sqSrc ^ sqDst) & 0x80) == 0;
  1206. }
  1207. function SAME_RANK(sqSrc, sqDst) {
  1208. return ((sqSrc ^ sqDst) & 0xf0) == 0;
  1209. }
  1210. function SAME_FILE(sqSrc, sqDst) {
  1211. return ((sqSrc ^ sqDst) & 0x0f) == 0;
  1212. }
  1213. function SIDE_TAG(sd) {
  1214. return 8 + (sd << 3);
  1215. }
  1216. function OPP_SIDE_TAG(sd) {
  1217. return 16 - (sd << 3);
  1218. }
  1219. function SRC(mv) {
  1220. return mv & 255;
  1221. }
  1222. function DST(mv) {
  1223. return mv >> 8;
  1224. }
  1225. function MOVE(sqSrc, sqDst) {
  1226. return sqSrc + (sqDst << 8);
  1227. }
  1228. function MIRROR_MOVE(mv) {
  1229. return MOVE(MIRROR_SQUARE(SRC(mv)), MIRROR_SQUARE(DST(mv)));
  1230. }
  1231. function MVV_LVA(pc, lva) {
  1232. return MVV_VALUE[pc & 7] - lva;
  1233. }
  1234. function CHR(n) {
  1235. return String.fromCharCode(n);
  1236. }
  1237. function ASC(c) {
  1238. return c.charCodeAt(0);
  1239. }
  1240. var FEN_PIECE = " KABNRCP kabnrcp ";
  1241. function CHAR_TO_PIECE(c) {
  1242. switch (c) {
  1243. case "K":
  1244. return PIECE_KING; // 将
  1245. case "A":
  1246. return PIECE_ADVISOR; // 士
  1247. case "B":
  1248. case "E":
  1249. return PIECE_BISHOP; // 象
  1250. case "H":
  1251. case "N":
  1252. return PIECE_KNIGHT; // 马
  1253. case "R":
  1254. return PIECE_ROOK; // 车
  1255. case "C":
  1256. return PIECE_CANNON; // 炮
  1257. case "P":
  1258. return PIECE_PAWN; //卒
  1259. default:
  1260. return -1;
  1261. }
  1262. }
  1263. function RC4(key) {
  1264. this.x = this.y = 0;
  1265. this.state = [];
  1266. for (var i = 0; i < 256; i ++) {
  1267. this.state.push(i);
  1268. }
  1269. var j = 0;
  1270. for (var i = 0; i < 256; i ++) {
  1271. j = (j + this.state[i] + key[i % key.length]) & 0xff;
  1272. this.swap(i, j);
  1273. }
  1274. }
  1275. RC4.prototype.swap = function(i, j) {
  1276. var t = this.state[i];
  1277. this.state[i] = this.state[j];
  1278. this.state[j] = t;
  1279. }
  1280. RC4.prototype.nextByte = function() {
  1281. this.x = (this.x + 1) & 0xff;
  1282. this.y = (this.y + this.state[this.x]) & 0xff;
  1283. this.swap(this.x, this.y);
  1284. var t = (this.state[this.x] + this.state[this.y]) & 0xff;
  1285. return this.state[t];
  1286. }
  1287. RC4.prototype.nextLong = function() {
  1288. var n0 = this.nextByte();
  1289. var n1 = this.nextByte();
  1290. var n2 = this.nextByte();
  1291. var n3 = this.nextByte();
  1292. return n0 + (n1 << 8) + (n2 << 16) + ((n3 << 24) & 0xffffffff);
  1293. }
  1294. var PreGen_zobristKeyPlayer, PreGen_zobristLockPlayer;
  1295. var PreGen_zobristKeyTable = [], PreGen_zobristLockTable = [];
  1296. var rc4 = new RC4([0]);
  1297. PreGen_zobristKeyPlayer = rc4.nextLong();
  1298. rc4.nextLong();
  1299. PreGen_zobristLockPlayer = rc4.nextLong();
  1300. for (var i = 0; i < 14; i ++) {
  1301. var keys = [];
  1302. var locks = [];
  1303. for (var j = 0; j < 256; j ++) {
  1304. keys.push(rc4.nextLong());
  1305. rc4.nextLong();
  1306. locks.push(rc4.nextLong());
  1307. }
  1308. PreGen_zobristKeyTable.push(keys);
  1309. PreGen_zobristLockTable.push(locks);
  1310. }
  1311. // function Position() {
  1312. // // sdPlayer, zobristKey, zobristLock, vlWhite, vlBlack, distance;
  1313. // // squares, mvList, pcList, keyList, chkList;
  1314. // }
  1315. //module.exports = Position;