GBoardChess.ts 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. import ADSDK from '../../gcommon/AdSdk/ADSDK';
  2. import AudioMng from '../../gcommon/AudioMng';
  3. import { UIButton } from '../../gcommon/UIButton';
  4. import GameUtil from '../../gcommon/GameUtil';
  5. import { ResMng } from '../../gcommon/ResMng';
  6. import UISetting from '../../gcommon/UISetting';
  7. import UITips from '../../gcommon/UITips';
  8. import { GameMng, quit_chess_show_view } from '../../GameMng';
  9. import CanjuModel from '../ChessCanJuSel/CanjuModel';
  10. import ChessMng, { ChessType } from '../DiffSel/ChessMng';
  11. import UIDiffSel from '../DiffSel/UIDiffSel';
  12. import ChessPos from '../ChessPos';
  13. import Search from '../Search';
  14. import UIEndChess from './UIEndChess';
  15. import ChessRBSel from './ChessRBSel';
  16. import { Vec3,tween,UITransform ,_decorator ,Component,Label,Node,instantiate,Animation,Prefab,Sprite, director, Director, SpriteFrame} from "cc";
  17. import { UIjiesuan } from '../UIjiesuan';
  18. import { ChessThink } from './ChessThink';
  19. import { ClientEvent } from '../../clientEvent';
  20. import { Constant, robot_ai_level, room_type } from '../../constant';
  21. import { roomData } from '../../UserData/roomData';
  22. import { UIManager } from '../../gcommon/UIManager';
  23. import { waitFirend } from '../waitFirend';
  24. import UIDialog from '../UIDialog';
  25. import ScenceMng from '../../gcommon/ScenceMng';
  26. import { setupData } from '../../UserData/setupData';
  27. import { msgManager } from '../../socket/msgManager';
  28. import { settlementData } from '../../UserData/settlementData';
  29. import { Tools } from '../../Tools';
  30. import { timeCountData } from '../../UserData/timeCountData';
  31. import { waitView } from '../waitView';
  32. import { waitJifen } from '../waitJifen';
  33. const {ccclass, property} = _decorator;
  34. var LIMIT_DEPTH = 64;
  35. ///
  36. var MATE_VALUE = 10000;
  37. var WIN_VALUE = MATE_VALUE - 200;
  38. var PIECE_KING=0;
  39. //////
  40. var RESULT_UNKNOWN = 0;
  41. var RESULT_WIN = 1;
  42. var RESULT_DRAW = 2;
  43. var RESULT_LOSS = 3;
  44. var BOARD_WIDTH = 965;
  45. var BOARD_HEIGHT = 1080;
  46. var SQUARE_SIZE = 106;
  47. var SQUARE_LEFT = (BOARD_WIDTH - SQUARE_SIZE * 9) >> 1;
  48. var SQUARE_TOP = (BOARD_HEIGHT - SQUARE_SIZE * 10) >> 1;
  49. var THINKING_SIZE = 32;
  50. var THINKING_LEFT = (BOARD_WIDTH - THINKING_SIZE) >> 1;
  51. var THINKING_TOP = (BOARD_HEIGHT - THINKING_SIZE) >> 1;
  52. var MAX_STEP = 8;
  53. var PIECE_NAME = [
  54. "oo", null, null, null, null, null, null, null,
  55. "hj", "hs", "hx", "hm", "hc", "hp", "hz", null,
  56. "bj", "bs", "bx", "bm", "bc", "bp", "bz", null,
  57. ];
  58. var IN_CHESS_BOARD = [
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  61. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  62. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  63. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  64. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  65. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  66. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  67. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  68. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  69. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  70. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  71. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  72. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  73. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  74. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  75. ];
  76. function IS_INBOARD(sq) {
  77. return IN_CHESS_BOARD[sq] != 0;
  78. }
  79. function COORD_XY(x, y) {
  80. return x + (y << 4);
  81. }
  82. function FILE_X(sq) {
  83. return sq & 15;
  84. }
  85. function RANK_Y(sq) {
  86. return sq >> 4;
  87. }
  88. function SQ_X(sq) {
  89. return SQUARE_LEFT + (FILE_X(sq) - 3) * SQUARE_SIZE;
  90. }
  91. function SQ_Y(sq) {
  92. return -(SQUARE_TOP + (RANK_Y(sq) - 3) * SQUARE_SIZE);
  93. }
  94. function MOVE_PX(src, dst, step) {
  95. return Math.floor((src * step + dst * (MAX_STEP - step)) / MAX_STEP + .5);
  96. }
  97. function alertDelay(message) {
  98. setTimeout(function () {
  99. alert(message);
  100. }, 250);
  101. }
  102. function SRC(mv) {
  103. return mv & 255;
  104. }
  105. function DST(mv) {
  106. return mv >> 8;
  107. }
  108. function SQUARE_FLIP(sq) {
  109. return 254 - sq;
  110. }
  111. function SIDE_TAG(sd) {
  112. return 8 + (sd << 3);
  113. }
  114. function MOVE(sqSrc, sqDst) {
  115. return sqSrc + (sqDst << 8);
  116. }
  117. @ccclass
  118. export default class GBoardChess extends Component {
  119. isComputerMoveing:boolean=false;
  120. // @property(Label)
  121. // huihelabel:Label=null;
  122. // @property(Label)
  123. // timelabel:Label=null;
  124. // @property(Label)
  125. // bushulabel:Label=null;
  126. @property(Node)
  127. bottom_node:Node=null;
  128. @property(Animation)
  129. animation:Animation=null;
  130. @property(Node)
  131. Juesha:Node=null;
  132. @property(Prefab)
  133. JueshaAniPrefab:Node=null;
  134. @property(Prefab)
  135. FaceKingPrefab:Prefab=null;
  136. @property(Prefab)
  137. startGameAniPrefab:Prefab=null;
  138. @property(Prefab)
  139. chiAniPrefab:Prefab=null;
  140. // @property(Label)
  141. // titlelabel:Label=null;
  142. @property(Node)
  143. GBoardChess:Node=null;
  144. @property(Node)
  145. chessNode: Node = null;
  146. @property(Node)
  147. wait_haoyou: Node = null;
  148. @property(Node)
  149. wait_jifen: Node = null;
  150. @property(Node)
  151. chatNode: Node = null;
  152. @property(SpriteFrame)
  153. select_chess_icon: SpriteFrame = null;
  154. @property(Node)
  155. jiesuan_node:Node=null;
  156. @property(Node)
  157. headNode: Node = null;
  158. @property(Node)
  159. uiLayer: Node = null;
  160. @property(Node)
  161. chat_view: Node = null;
  162. pos:ChessPos=null;
  163. search:Search=null;
  164. imgSquares:Node[]=[];
  165. sqSelected:number=0; //当前被选中棋子的位置。
  166. mvLast:number=0;
  167. millis:number=0;
  168. computer:number=0;
  169. result:number=0;
  170. busy:boolean=false;
  171. animated:boolean=false;
  172. static instance:GBoardChess=null;
  173. moveNode:Node=null;
  174. isStartGame:boolean = false;
  175. static g_isStartFirstGame:boolean = true;
  176. isStartFirstGame:boolean = true;
  177. selfLaseSelectNode:Node=null;
  178. wait_rival_status:number=0; // 0等待,1好友已加入
  179. wait_rival_startGame_status:number = 0;//0,都没准备,1,好友未准备,2,自己未准备,3,开始下棋
  180. //temp test ai
  181. tempsqSelected:number=0;
  182. tempsqDes:number=0;
  183. gTime:number=0;
  184. isend:boolean=false;
  185. bushu:number=0;
  186. result_pos:any[] =[]
  187. last_settlementData:settlementData= null;
  188. who_round:number = 0 //1 自己回手,0对方回合
  189. round_id:number = 0 //回合者的id
  190. moveCallfun:any = null
  191. rund_number:number = 0 //当前第多少步
  192. heqi_number:number = 0
  193. huiqi_number:number = 0
  194. huiqi_setup:number = 0;
  195. moveList:any[] = []
  196. mv_rival_last:number=0;
  197. start () {
  198. GBoardChess.instance=this;
  199. this.moveNode=instantiate(this.chessNode);
  200. //this.moveNode.removeAllChildren();
  201. //this.huihelabel.string="开始游戏";
  202. this.initNeed();
  203. this.setTitlelabel();
  204. this.startGame();
  205. this.regMsg();
  206. }
  207. regMsg(){
  208. ClientEvent.on(Constant.EVENT_TYPE.MSG_ROOM_STATUS,this.recv_room_status,this)
  209. ClientEvent.on(Constant.EVENT_TYPE.MSG_DISMISS_ROOM,this.recv_room_dismiss,this)
  210. ClientEvent.on(Constant.EVENT_TYPE.MSG_OWN_PLAYCHESS,this.recv_own_playchess,this)
  211. ClientEvent.on(Constant.EVENT_TYPE.MSG_RIVAL_PLAYCHESS,this.recv_rival_playchess,this)
  212. ClientEvent.on(Constant.EVENT_TYPE.MSG_SETTLEMENT,this.recv_settlement,this)
  213. ClientEvent.on(Constant.EVENT_TYPE.MSG_QUIT_ROOM,this.recvQuit,this)
  214. ClientEvent.on(Constant.EVENT_TYPE.MSG_PVP_TIME,this.recv_time_count,this)
  215. ClientEvent.on(Constant.EVENT_TYPE.MSG_HEQI_JIESHOU,this.recv_jieshou_heqi_game,this)
  216. ClientEvent.on(Constant.EVENT_TYPE.MSG_HUIQI_JIESHOU,this.recv_jieshou_huiqi_game,this)
  217. ClientEvent.on(Constant.EVENT_TYPE.MSG_HEQI_TONGZHI,this.recv_tongzhi_heqi_game,this)
  218. ClientEvent.on(Constant.EVENT_TYPE.MSG_HUIQI_TONGZHI,this.recv_tongzhi_huiqi_game,this)
  219. ClientEvent.on(Constant.EVENT_TYPE.MSG_START_HUIQI,this.recv_start_huiqi,this)
  220. ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_HEQI,this.onBtnHeQi,this)
  221. ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_HUIQI,this.onBtnHuiQi,this)
  222. ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_SHOW_CHAT,this.onBtnShowChat,this)
  223. ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_HIDE_CHAT,this.onBtnHideChat,this)
  224. }
  225. onDestroy(){
  226. GBoardChess.instance=null;
  227. ClientEvent.off(Constant.EVENT_TYPE.MSG_ROOM_STATUS,this.recv_room_status,this)
  228. ClientEvent.off(Constant.EVENT_TYPE.MSG_DISMISS_ROOM,this.recv_room_dismiss,this)
  229. ClientEvent.off(Constant.EVENT_TYPE.MSG_OWN_PLAYCHESS,this.recv_own_playchess,this)
  230. ClientEvent.off(Constant.EVENT_TYPE.MSG_RIVAL_PLAYCHESS,this.recv_rival_playchess,this)
  231. ClientEvent.off(Constant.EVENT_TYPE.MSG_SETTLEMENT,this.recv_settlement,this)
  232. ClientEvent.off(Constant.EVENT_TYPE.MSG_QUIT_ROOM,this.recvQuit,this)
  233. ClientEvent.off(Constant.EVENT_TYPE.MSG_PVP_TIME,this.recv_time_count,this)
  234. ClientEvent.off(Constant.EVENT_TYPE.MSG_HEQI_JIESHOU,this.recv_jieshou_heqi_game,this)
  235. ClientEvent.off(Constant.EVENT_TYPE.MSG_HUIQI_JIESHOU,this.recv_jieshou_huiqi_game,this)
  236. ClientEvent.off(Constant.EVENT_TYPE.MSG_HUIQI_TONGZHI,this.recv_tongzhi_huiqi_game,this)
  237. ClientEvent.off(Constant.EVENT_TYPE.MSG_START_HUIQI,this.recv_start_huiqi,this)
  238. ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_HEQI,this.onBtnHeQi,this)
  239. ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_HUIQI,this.onBtnHuiQi,this)
  240. ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_SHOW_CHAT,this.onBtnShowChat,this)
  241. ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_HIDE_CHAT,this.onBtnHideChat,this)
  242. }
  243. onBtnShowChat(){
  244. this.chat_view.active = true;
  245. }
  246. onBtnHideChat(){
  247. this.chat_view.active = false;
  248. }
  249. onBtnHuiQi(){
  250. msgManager.qingqiu_huiqi_game()
  251. waitView.Show("等待对方是否同意悔棋。")
  252. }
  253. onBtnHeQi(){
  254. msgManager.qingqiu_heqi_game()
  255. waitView.Show("等待对方是否同意和棋。")
  256. }
  257. checkWaitStatus(){
  258. if(GameMng._userData.room_info.chess!=null){
  259. if(GameMng._userData.room_info.chess.wait_select_result!=null&&GameMng._userData.room_info.chess.wait_select_result!=undefined){
  260. switch(GameMng._userData.room_info.chess.wait_select_result){
  261. case 0:
  262. {
  263. waitView.Show("等待对方是否同意悔棋。")
  264. break;
  265. }
  266. case 1:
  267. {
  268. waitView.Show("等待对方是否同意和棋。")
  269. break;
  270. }
  271. case 2:
  272. {
  273. this.recv_jieshou_huiqi_game()
  274. break;
  275. }
  276. case 3:
  277. {
  278. this.recv_jieshou_heqi_game()
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. }
  285. recv_start_huiqi(setup:setupData){
  286. var move_data = MOVE(setup.src_pos,setup.dec_pos)
  287. // var src = COORD_XY(FILE_X(setup.src_pos),15-RANK_Y(setup.src_pos))
  288. // var des = COORD_XY(FILE_X(setup.dec_pos),15-RANK_Y(setup.dec_pos))
  289. // move_data = MOVE(des,src)
  290. this.huiqi_setup = setup.huiqi_setup+1;
  291. this.pos.pushMvList(move_data)
  292. if(setup.userid!=GameMng._userData.userid){ //说明不是自己
  293. console.log("开始对方开始悔棋",setup)
  294. this.huiqi(true)
  295. }else{
  296. this.huiqi(false)
  297. console.log("开始我方开始悔棋",setup)
  298. }
  299. console.log("move_data",move_data)
  300. this.who_round = 1 - this.who_round
  301. if(this.isStartGame===true){
  302. this.headNode.getComponent(ChessThink).startThink(true)
  303. }
  304. }
  305. recv_tongzhi_heqi_game(obj:any){
  306. if(obj.type === 0){
  307. UITips.show("对方拒绝和棋");
  308. }else if(obj.type === 1){
  309. UITips.show("协商和棋,对局结束");
  310. }
  311. UIManager.removeWaitViewLayer()
  312. this.heqi_number-=1;
  313. }
  314. recv_tongzhi_huiqi_game(obj:any){
  315. console.log("recv_tongzhi_huiqi_game",obj.type)
  316. if(obj.type === 0){
  317. UITips.show("对方拒绝悔棋");
  318. this.huiqi_number-=1;
  319. }else if(obj.type === 1){
  320. UITips.show("对方同意悔棋");
  321. this.huiqi_number-=1;
  322. }else if(obj.type === 2){
  323. UITips.show("悔棋请求超时!");
  324. }
  325. UIManager.removeWaitViewLayer()
  326. }
  327. recv_jieshou_heqi_game(){
  328. UIDialog.Show(()=>{
  329. msgManager.tongzhi_heqi_game(1)
  330. },()=>{
  331. msgManager.tongzhi_heqi_game(0)
  332. },"对方请求提和,是否同意",null,true)
  333. }
  334. recv_jieshou_huiqi_game(){
  335. // this.who_round===1&&
  336. if(!this.computerMove()){
  337. UIDialog.Show(()=>{
  338. msgManager.tongzhi_huiqi_game(1)
  339. },()=>{
  340. msgManager.tongzhi_huiqi_game(0)
  341. },"对方请求悔棋,是否同意",null,true)
  342. }else{
  343. msgManager.tongzhi_huiqi_game(2)
  344. }
  345. }
  346. recv_time_count(time_count_data:timeCountData){
  347. GameMng._userData.room.time_count_data = time_count_data;
  348. if(this.round_id!=time_count_data.round_id){
  349. this.round_id = time_count_data.round_id
  350. }else{
  351. }
  352. this.headNode.getComponent(ChessThink).startThink(false)
  353. if(time_count_data.round_id===GameMng._userData.userid){
  354. console.log("我的回合",time_count_data)
  355. this.who_round = 1;
  356. }else{
  357. this.who_round = 0;
  358. console.log("对方回合",time_count_data)
  359. }
  360. this.updateRound()
  361. }
  362. recvQuit(obj:any={type:0}){
  363. if(obj.type===0){
  364. }else{
  365. GameMng.Instance.quit_view = quit_chess_show_view.beitichu;
  366. }
  367. ChessMng.Instance.chesstype = ChessType._null;
  368. GameMng._userData.room = null;
  369. AudioMng.Instance.PlayBGM(GameMng.Instance.mainbgm);
  370. ScenceMng.Instance.load('GameLaunch');
  371. }
  372. recv_own_playchess(setup:setupData){
  373. console.log("自己完成下棋",setup.userid)
  374. // this.rund_number = setup.rund_number
  375. this.who_round = 0;
  376. this.updateRound();
  377. this.rund_number = setup.rund_number
  378. if(this.isStartGame===true){
  379. if(setup.pvp_time==null||setup.pvp_time==undefined){
  380. }else{
  381. GameMng._userData.room.time_count_data = setup.pvp_time;
  382. }
  383. this.headNode.getComponent(ChessThink).startThink(true,true)
  384. }
  385. }
  386. recv_settlement(settlement:settlementData){
  387. if(GameMng._userData.room==null){
  388. }else{
  389. UIManager.removeDialogViewLayer()
  390. UIManager.removeWaitViewLayer()
  391. this.headNode.getComponent(ChessThink).stopThink()
  392. console.log("结算",settlement)
  393. this.last_settlementData = settlement
  394. if(this.moveList.length>0){
  395. this.moveCallfun = ()=>{
  396. console.log("移动完,开始结算!")
  397. this.settlement(settlement)
  398. }
  399. }else{
  400. this.settlement(settlement)
  401. }
  402. this.isStartGame = false;
  403. if(GameMng._userData.room.rival==null){
  404. }
  405. let self = this;
  406. this.scheduleOnce(()=>{
  407. if(ChessMng.Instance.isHaoyouRoom()){
  408. this.wait_haoyou.active = true;
  409. self.wait_haoyou.getComponent(waitFirend).recvAgainRound(GameMng._userData.room)
  410. }else if(ChessMng.Instance.isJiFenRoom()){
  411. this.showWaitJiFen()
  412. self.wait_jifen.getComponent(waitJifen).recvAgainRound(GameMng._userData.room)
  413. }
  414. },3)
  415. GameMng._userData.room.time_count_data = null
  416. }
  417. }
  418. recv_rival_playchess(setup:setupData){
  419. this.who_round = 1;
  420. this.updateRound()
  421. // console.log("self.tempsqDes",self.tempsqDes)
  422. // console.log("self.tempsqSelected",self.tempsqSelected)
  423. if(this.isStartGame ==true){
  424. var self = this;
  425. this.moveList.push("0")
  426. // var src = COORD_XY(FILE_X(setup.src_pos),15-RANK_Y(setup.src_pos))
  427. // var des = COORD_XY(FILE_X(setup.dec_pos),15-RANK_Y(setup.dec_pos))
  428. // console.log("src ",src);
  429. // console.log("des ",des);
  430. // console.log("des row ",RANK_Y(des));
  431. this.mv_rival_last = MOVE(setup.src_pos, setup.dec_pos)
  432. director.once(Director.EVENT_AFTER_DRAW,function(){
  433. self.addMove(MOVE(setup.src_pos, setup.dec_pos),true);
  434. })
  435. console.log("对手下棋了",setup)
  436. this.rund_number = setup.rund_number
  437. if(this.isStartGame===true){
  438. if(setup.pvp_time==null||setup.pvp_time==undefined){
  439. }else{
  440. GameMng._userData.room.time_count_data = setup.pvp_time;
  441. }
  442. this.headNode.getComponent(ChessThink).startThink(true,true)
  443. }
  444. ClientEvent.dispatchEvent(Constant.UI_EVENT.UI_MSG_UPDATE_MENU_STATUS)
  445. }
  446. }
  447. recv_room_dismiss(room:roomData){
  448. GameMng._userData.room = null;
  449. UIDialog.Show(()=>{
  450. AudioMng.Instance.PlayBGM(GameMng.Instance.mainbgm);
  451. ScenceMng.Instance.load('GameLaunch');
  452. },()=>{
  453. },"房间已解散",null,false)
  454. }
  455. recv_room_status(room:roomData){
  456. UIManager.removeLoadingLayer()
  457. GameMng.updateRoomData(room)
  458. this.headNode.getComponent(ChessThink).StatusShow()
  459. this.onRecvStartGame(room)
  460. }
  461. onRecvStartGame(room:roomData){
  462. if(ChessMng.Instance.isHaoyouRoom()){
  463. this.wait_haoyou.getComponent(waitFirend).recvReady(room)
  464. }else if(ChessMng.Instance.isJiFenRoom()){
  465. this.wait_jifen.getComponent(waitJifen).recvReady(room)
  466. }
  467. console.log("this.isStartGame== ",this.isStartGame,room.wait_rival_startGame_status,room.rival)
  468. if(room.wait_rival_startGame_status === 3 &&!this.isStartGame){
  469. this.isStartGame = true;
  470. this.userReadyStartGame()
  471. this.headNode.getComponent(ChessThink).startThink(true)
  472. if(room.isfirst_start===1){
  473. this.onstartGameAni()
  474. }
  475. }
  476. if(room.rival===undefined||room.rival===null){
  477. //this.hideAllChess()
  478. }
  479. }
  480. private startGame(){
  481. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  482. this.ShowRoleSel();
  483. this.isStartGame = true;
  484. }else{
  485. this.initChessBoard();
  486. }
  487. }
  488. countTime(){
  489. if(this.isend)return;
  490. this.gTime++;
  491. //this.timelabel.string="耗时:"+GameUtil.FormatTime_HHMMSS(this.gTime*1000)
  492. }
  493. ShowRoleSel(){
  494. //this.uirbselect.active=true;
  495. GBoardChess.instance.initChessBoard()
  496. }
  497. updateWaitStatus(){
  498. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  499. this.isStartGame = true;
  500. this.onstartGameAni()
  501. }else{
  502. this.wait_rival_status = GameMng._userData.room.wait_rival_status
  503. this.wait_rival_startGame_status = GameMng._userData.room.wait_rival_startGame_status
  504. this.onRecvStartGame(GameMng._userData.room);
  505. }
  506. this.headNode.getComponent(ChessThink).StatusShow()
  507. // if(this.wait_rival_status==0){ //房间只有自己还在等待
  508. // }else{//对手已加入房间
  509. // if(this.wait_rival_startGame_status===0){ //0,都没准备
  510. // }else if(this.wait_rival_startGame_status===1){ //1,好友未准备
  511. // }else if(this.wait_rival_startGame_status===2){ //2,自己未准备
  512. // }else if(this.wait_rival_startGame_status===3){ //3 都准备好开始下棋
  513. // }
  514. // }
  515. }
  516. setTitlelabel(){
  517. // if(ChessMng.Instance.chesstype==ChessType.renjiboyi)
  518. // this.titlelabel.string="难度:"+UIDiffSel.lvArr[UIDiffSel.selectIndex];
  519. // else if(ChessMng.Instance.chesstype==ChessType.canju)
  520. // this.titlelabel.string=CanjuModel.Instance.canjuData.name;
  521. }
  522. initChessInfo(){
  523. this.isComputerMoveing=false;
  524. this.busy=false;
  525. this.isend=false;
  526. this.huiqi_setup = 0;
  527. this.gTime=0;
  528. this.millis = this.getDiff();
  529. this.bushu=0;
  530. this.setTitlelabel();
  531. this.hideAllTrack();
  532. this.hideAllPath();
  533. this.result = RESULT_UNKNOWN;
  534. this.round_id = -1;
  535. this.rund_number = 0;
  536. this.heqi_number = 0;
  537. this.huiqi_number = 0;
  538. console.log("initChessInfo")
  539. }
  540. hideAllChess(){
  541. if(this.imgSquares.length>0){
  542. for (let index = 0; index < this.imgSquares.length; index++) {
  543. const element = this.imgSquares[index];
  544. if(element!=null){
  545. element.active =false;
  546. }
  547. }
  548. }
  549. }
  550. showAllChess(){
  551. if(this.imgSquares.length>0){
  552. for (let index = 0; index < this.imgSquares.length; index++) {
  553. const element = this.imgSquares[index];
  554. if(element!=null){
  555. element.active =true;
  556. }
  557. }
  558. }
  559. }
  560. //初始化棋盘
  561. initChessBoard(){
  562. this.initChessInfo()
  563. // this.isComputerMoveing=false;
  564. // this.busy=false;
  565. // this.isend=false;
  566. // this.gTime=0;
  567. // this.millis = this.getDiff();
  568. // this.bushu=0;
  569. // this.unschedule(this.countTime);
  570. // this.schedule(this.countTime, 1);
  571. // this.setTitlelabel();
  572. AudioMng.Instance.PlayBGM(GameMng.Instance.gamebgm)
  573. console.log(this.millis)
  574. if(ChessMng.Instance.chesstype==ChessType.canju){
  575. ChessRBSel.selIndex=0;
  576. }
  577. this.showHuiheLabel(ChessRBSel.selIndex == 0);
  578. // AudioMng.Instance.PlaySoundByName(GameMng.Instance.startclip)
  579. this.refreshBushulabel();
  580. //this.timelabel.string='耗时:00:00';
  581. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  582. this.pos.fromFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1");
  583. this.computer = 1 - ChessRBSel.selIndex;
  584. this.flushBoard();
  585. }else if(ChessMng.Instance.chesstype==ChessType.canju){
  586. this.pos.fromFen(CanjuModel.Instance.canjuData.fen);
  587. }else{
  588. if(ChessMng.Instance.isHaoyouRoom()){
  589. this.showWaitHaoYou();
  590. }else if(ChessMng.Instance.isJiFenRoom()){
  591. this.showWaitJiFen();
  592. }
  593. }
  594. this.updateWaitStatus();
  595. setTimeout(() => {
  596. this.response();
  597. }, 500);
  598. }
  599. userReadyStartGame(){
  600. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  601. }else{
  602. if(ChessMng.Instance.isHaoyouRoom()){
  603. this.showAllChess();
  604. this.wait_haoyou.active = false;
  605. } else if(ChessMng.Instance.isJiFenRoom()){
  606. this.showAllChess();
  607. this.wait_jifen.active = false;
  608. }
  609. }
  610. //ChessRBSel.selIndex = 1-GameMng._userData.room.firstid
  611. //console.log("this.compute",this.computer)
  612. if(this.isStartFirstGame){
  613. }else{
  614. console.log("第二次初始化棋盘")
  615. this.initChessInfo()
  616. }
  617. this.computer = GameMng._userData.room.firstid;
  618. this.busy = GameMng._userData.room.firstid ==0;
  619. this.who_round = GameMng._userData.room.firstid;
  620. this.updateRound();
  621. this.heqi_number = GameMng._userData.room.heqi_number;
  622. this.huiqi_number = GameMng._userData.room.huiqi_number;
  623. this.huiqi_setup = GameMng._userData.room.huiqi_setup;
  624. if(GameMng._userData.room.chess!=null&&GBoardChess.g_isStartFirstGame){
  625. if(GameMng._userData.room_info.chess.chess_whole===""||GameMng._userData.room_info.chess.chess_whole===undefined||GameMng._userData.room_info.chess.chess_whole===null){
  626. this.pos.fromFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1");
  627. }else{
  628. this.pos.fromFen(GameMng._userData.room_info.chess.chess_whole);
  629. this.pos.pushMvList(MOVE(GameMng._userData.room_info.chess.src_pos, GameMng._userData.room_info.chess.dec_pos))
  630. this.drawOldAndNewSelect(GameMng._userData.room_info.chess.src_pos, GameMng._userData.room_info.chess.dec_pos,true)
  631. }
  632. this.rund_number = GameMng._userData.room_info.chess.rund_number;
  633. this.heqi_number = GameMng._userData.room_info.chess.heqi_number;
  634. this.huiqi_number = GameMng._userData.room_info.chess.huiqi_number;
  635. this.checkWaitStatus()
  636. }else{
  637. this.pos.fromFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1");
  638. }
  639. this.flushBoard();
  640. this.isStartFirstGame = false;
  641. GBoardChess.g_isStartFirstGame = false;
  642. }
  643. showWaitHaoYou(){
  644. if(this.isStartFirstGame){
  645. this.bottom_node.active = false;
  646. this.wait_haoyou.active = true;
  647. this.chatNode.active = true;
  648. }
  649. }
  650. showWaitJiFen(){
  651. this.bottom_node.active = false;
  652. this.wait_haoyou.active = false;
  653. this.chatNode.active = true;
  654. this.wait_jifen.active = true;
  655. }
  656. //重新开始
  657. restartGame(){
  658. AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
  659. this.startGame();
  660. }
  661. //悔棋
  662. retract(){
  663. AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
  664. if (this.busy) {
  665. return;
  666. }
  667. if(!this.isStartGame){
  668. return;
  669. }
  670. // ADSDK.Share(null, () => {
  671. // }, null)
  672. this.result = RESULT_UNKNOWN;
  673. if (this.pos.mvList.length > 1) {
  674. this.pos.undoMakeMove();
  675. }
  676. if (this.pos.mvList.length > 1 && this.computerMove()) {
  677. this.pos.undoMakeMove();
  678. }
  679. if(this.mvLast>0){
  680. this.drawSquare(SRC(this.mvLast), false,false);
  681. this.drawSquare(DST(this.mvLast), false,false);
  682. this.drawOldAndNewSelect(SRC(this.mvLast),DST(this.mvLast),false)
  683. }
  684. this.flushBoard();
  685. this.response();
  686. this.hideAllPath();
  687. console.log("this.mvLast == ",this.mvLast)
  688. }
  689. //提示
  690. tishi(){
  691. AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
  692. if(this.busy||this.isComputerMoveing)return;
  693. ADSDK.Share(null,()=>{
  694. this.isComputerMoveing=true;
  695. console.log("tishi")
  696. this.search.searchMain(4,400,(a)=>{
  697. if(this.addMove(a,false)){
  698. this.hideAllPath()
  699. }
  700. });
  701. },null)
  702. }
  703. private getDiff() {
  704. return Math.pow(10, UIDiffSel.selectIndex + 1);
  705. }
  706. private initNeed() {
  707. this.animated = true;
  708. this.search = null;
  709. this.imgSquares = [];
  710. this.sqSelected = 0;
  711. this.mvLast = 0;
  712. this.millis = 0;
  713. this.computer = -1;
  714. this.result = RESULT_UNKNOWN;
  715. this.busy = false;
  716. this.pos = new ChessPos();
  717. this.setSearch(16);
  718. for (let sq = 0; sq < 256; sq++) {
  719. if (!IS_INBOARD(sq)) {
  720. this.imgSquares.push(null);
  721. continue;
  722. }
  723. let gnode=instantiate(this.chessNode);
  724. gnode.position = new Vec3( SQ_X(sq),SQ_Y(sq));
  725. gnode.getComponent(UITransform).setContentSize(SQUARE_SIZE,SQUARE_SIZE);
  726. gnode.parent=this.GBoardChess;
  727. var sp = gnode.getChildByName("chess_bg");
  728. (sp.getComponent(Sprite) as Sprite).enabled=false;
  729. gnode.getChildByName('track1').active=false;
  730. gnode.getChildByName('track2').active=false;
  731. gnode.on('click', () => {
  732. this.clickSquare(sq);
  733. }, this);
  734. this.imgSquares.push(gnode);
  735. }
  736. //this.hideAllTrack();
  737. }
  738. ShowSeting(){
  739. AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
  740. UISetting.Show();
  741. }
  742. // 点击棋盘的响应函数。点击棋盘(棋子或者空位置),就会调用该函数。sq_是点击的位置。
  743. clickSquare(sq_) {
  744. if(GameMng._userData.room){
  745. console.log(this.isStartGame,sq_,this.busy,this.result,this.who_round,this.sqSelected);
  746. }
  747. if(!this.isStartGame){
  748. return;
  749. }
  750. if (this.busy || this.result != RESULT_UNKNOWN) {
  751. return;
  752. }
  753. var sq = this.flipped(sq_);
  754. var pc = this.pos.squares[sq];
  755. // var col = FILE_X(sq_)-3
  756. // var row = RANK_Y(sq_)-3
  757. // console.log("col ",col);
  758. // console.log("row ",row);
  759. // console.log("pc ",pc,sq);
  760. if(this.sqSelected==sq){
  761. return;
  762. }
  763. if ((pc & SIDE_TAG(this.pos.sdPlayer)) != 0 ) {
  764. console.log("111");
  765. this.hideAllPath()
  766. AudioMng.Instance.PlaySoundByName(GameMng.Instance.pickclip);
  767. // if (this.mvLast != 0) {
  768. // this.drawSquare(SRC(this.mvLast), false,true);
  769. // this.drawSquare(DST(this.mvLast), false,true);
  770. // }
  771. if (this.sqSelected) {
  772. this.drawSquare(this.sqSelected, false,true);
  773. }
  774. this.drawSquare(sq, true,true);
  775. var temp_array = this.findAllCanMove(sq)
  776. this.drawCanMovePath(temp_array)
  777. this.sqSelected = sq;
  778. } else if (this.sqSelected > 0) {
  779. console.log("222");
  780. this.tempsqSelected = this.sqSelected;
  781. this.tempsqDes = sq;
  782. if(this.addMove(MOVE(this.sqSelected, sq), false)){
  783. this.hideAllPath();
  784. }
  785. }
  786. }
  787. private FaceKingWarning() //将军
  788. {
  789. let node = instantiate(this.FaceKingPrefab);
  790. node.parent = this.node;
  791. tween()
  792. .target(node)
  793. .to(0.5,{ scale: new Vec3(1, 1, 1) })
  794. .start();
  795. AudioMng.Instance.PlaySoundByName(GameMng.Instance.kingclip)
  796. }
  797. private onstartGameAni(){
  798. let node = instantiate(this.startGameAniPrefab);
  799. node.parent = this.node;
  800. tween()
  801. .target(node)
  802. .to(0.5,{ scale: new Vec3(1, 1, 1) })
  803. .start();
  804. console.log("onstartGameAni")
  805. }
  806. private onchiGameAni(){
  807. let node = instantiate(this.chiAniPrefab);
  808. node.parent = this.node;
  809. tween()
  810. .target(node)
  811. .to(0.5,{ scale: new Vec3(1, 1, 1) })
  812. .start();
  813. console.log("onchiGameAni")
  814. AudioMng.Instance.PlaySoundByName(GameMng.Instance.eatclip)
  815. }
  816. private onJueShaAni(){
  817. let node = instantiate(this.JueshaAniPrefab);
  818. node.parent = this.node;
  819. tween()
  820. .target(node)
  821. .to(0.5,{ scale: new Vec3(1, 1, 1) })
  822. .start();
  823. console.log("onJueShaAni")
  824. AudioMng.Instance.PlaySoundByName(GameMng.Instance.kill)
  825. }
  826. settlement(data:settlementData){
  827. AudioMng.Instance.PauseBGM();
  828. this.result_pos = Tools.getChessPos()
  829. this.isend=true;
  830. if(ChessMng.Instance.chesstype===ChessType.renjiboyi){
  831. if(!data.isheqi){
  832. //this.animation.play('win');
  833. AudioMng.Instance.PlaySoundByName(GameMng.Instance.kill)
  834. this.onJueShaAni()
  835. setTimeout(() => {
  836. this.jiesuan_node.active = true;
  837. this.jiesuan_node.getComponent(UIjiesuan).setData(data.result,GameUtil.FormatTime_HHMMSS(this.gTime*1000),this.bushu)
  838. //this.endview.active=true;
  839. //let nodets= this.endview.getComponent(UIEndChess);
  840. //nodets.setData(result,GameUtil.FormatTime_HHMMSS(this.gTime*1000),this.bushu);
  841. }, 2000);
  842. }
  843. }else {
  844. if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
  845. switch(data.result_type){
  846. case 0:{ //正常绝杀
  847. this.onJueShaAni()
  848. break;
  849. }
  850. case 1:{ //到时间
  851. if(data.result==1){
  852. UITips.show("对方到时间,您获得胜利!");
  853. }else{
  854. UITips.show("你的到时间,对方获得胜利!");
  855. }
  856. break;
  857. }
  858. case 2:{ //逃跑
  859. if(data.result==1){
  860. UITips.show("对方逃跑,获得胜利");
  861. }else{
  862. UITips.show("你逃跑输掉比赛");
  863. }
  864. break;
  865. }
  866. case 3:{ //认输
  867. if(data.result==1){
  868. UITips.show("对方认输,对局结束");
  869. }else{
  870. UITips.show("我方认输,对局结束");
  871. }
  872. break;
  873. }
  874. case 4:{ //和棋
  875. if(data.result==1){
  876. }else{
  877. }
  878. break;
  879. }
  880. }
  881. this.jiesuan_node.active = true;
  882. this.jiesuan_node.getComponent(UIjiesuan).setData(data.result,GameUtil.FormatTime_HHMMSS(this.gTime*1000),this.bushu)
  883. }
  884. }
  885. }
  886. private gameEnd(result,isheqi:boolean=false)
  887. {
  888. this.isStartGame =false;
  889. if(ChessMng.Instance.chesstype == ChessType.renjiboyi){
  890. let settlement_data = new settlementData()
  891. settlement_data.result = result
  892. settlement_data.isheqi = isheqi
  893. this.settlement(settlement_data)
  894. }else{
  895. console.log("result == ",result)
  896. if(result === 1){
  897. //msgManager.final_game()
  898. }else{
  899. console.log("result",result)
  900. console.log("isheqi",isheqi)
  901. }
  902. }
  903. }
  904. findAllCanMove(sq){
  905. return this.pos.findAllCanMove(sq)
  906. }
  907. moveAni(mv,computerMove,isRight:boolean=true){
  908. AudioMng.Instance.PlaySoundByName(GameMng.Instance.putclip)
  909. this.busy = true;
  910. if (!this.animated) {
  911. if(computerMove)this.isComputerMoveing=false;
  912. this.postAddMove(mv, computerMove);
  913. return false;
  914. }
  915. console.log("sqSrc",mv)
  916. var sqSrc = this.flipped(SRC(mv));
  917. var xSrc = SQ_X(sqSrc);
  918. var ySrc = SQ_Y(sqSrc);
  919. //console.log("sqSrc",sqSrc,SRC(mv),this.computer);
  920. var sqDst = this.flipped(DST(mv));
  921. var xDst = SQ_X(sqDst);
  922. var yDst = SQ_Y(sqDst);
  923. var getNode = this.imgSquares[sqSrc];
  924. var bg = getNode.getChildByName("chess_bg");
  925. var qz = bg.getChildByName("chess_qz");
  926. var _bg = this.moveNode.getChildByName("chess_bg");
  927. var _qz = _bg.getChildByName("chess_qz");
  928. _qz.active = true;
  929. this.moveNode.active=true;
  930. getNode.active =false;
  931. this.moveNode.removeFromParent()
  932. _bg.getComponent(Sprite).spriteFrame=bg.getComponent(Sprite).spriteFrame;
  933. _qz.getComponent(Sprite).spriteFrame=qz.getComponent(Sprite).spriteFrame;
  934. //this.moveNode.getComponent(Sprite).spriteFrame=getNode.getComponent(Sprite).spriteFrame;
  935. this.moveNode.parent=getNode.parent;
  936. var temp_pos = new Vec3(getNode.position.x,getNode.position.y,0)
  937. this.moveNode.position = temp_pos
  938. var step = MAX_STEP - 1;
  939. var this_ = this;
  940. tween(this.moveNode)
  941. .to(0.2, { position:new Vec3(xDst,yDst,0)})
  942. .call(async ()=>{
  943. if(isRight){
  944. if(computerMove)this_.isComputerMoveing=false;
  945. var _status = this_.postAddMove(mv, computerMove)
  946. if(!computerMove){
  947. if(ChessMng.Instance.isJiFenRoom()||ChessMng.Instance.isHaoyouRoom()){
  948. let setup = new setupData
  949. setup.src_pos = SRC(mv)
  950. setup.dec_pos = DST(mv)
  951. setup.chess_whole = this.pos.toFen()
  952. setup.is_mate = _status?0:1
  953. if(GameMng._userData.room.firstid==1){
  954. setup.des_who = "我是红方"
  955. }else{
  956. setup.des_who = "我是黑方"
  957. }
  958. if(GameMng._userData.room!=null){
  959. if(GameMng._userData.room.rival!=null){
  960. if(GameMng._userData.room.rival.is_robot===1){
  961. setup.robot_data = await this.robot_search(setup.chess_whole,setup.is_mate)
  962. console.log("给人机插入数据","给人机插入数据")
  963. }else{
  964. console.log("人机数据插入空值","人机数据插入空值")
  965. }
  966. }
  967. }
  968. this.send_msg_self_move(setup)
  969. }
  970. }
  971. this_.moveList.splice(0,1)
  972. if(this_.moveCallfun!=null){
  973. this_.moveCallfun()
  974. this_.moveCallfun = null
  975. }
  976. this_.moveNode.active=false;
  977. getNode.active =true;
  978. }else{
  979. this.busy = false;
  980. this_.moveNode.active=false;
  981. getNode.active =true;
  982. tween(this.moveNode)
  983. .to(0.1, { position:temp_pos}).start()
  984. }
  985. })
  986. .start();
  987. }
  988. robot_search(chess_whole,isMate) :Promise<setupData>{
  989. if(isMate===1){
  990. let robot_data = new setupData
  991. robot_data.is_mate = 0
  992. robot_data.chess_whole = chess_whole
  993. return
  994. }
  995. return new Promise((resolve, reject) => {
  996. this.scheduleOnce(() => {
  997. var search = new Search()
  998. var pos = new ChessPos();
  999. pos.fromFen(chess_whole);
  1000. search.setSearch(pos,1)
  1001. switch(GameMng._userData.room.rival.robot_ai_lv){
  1002. case robot_ai_level.xiaobai:{
  1003. search.xiaoBaiSearchMain((a)=>{
  1004. director.once(Director.EVENT_AFTER_DRAW,function(){
  1005. let robot_data = new setupData
  1006. robot_data.src_pos = SRC(a)
  1007. robot_data.dec_pos = DST(a)
  1008. if(!pos.makeMove(a)){
  1009. robot_data.src_pos = 0
  1010. robot_data.dec_pos = 0
  1011. }
  1012. robot_data.is_mate = pos.isMate()?1:0
  1013. robot_data.chess_whole = pos.toFen()
  1014. resolve(robot_data)
  1015. })
  1016. });
  1017. return;
  1018. }
  1019. }
  1020. search.searchMain(GameMng._userData.room.rival.robot_ai_lv,400,(a)=>{
  1021. var robot_data = new setupData
  1022. robot_data.src_pos = SRC(a)
  1023. robot_data.dec_pos = DST(a)
  1024. if(!pos.makeMove(a)){
  1025. robot_data.src_pos = 0
  1026. robot_data.dec_pos = 0
  1027. }
  1028. robot_data.is_mate = pos.isMate()?1:0
  1029. robot_data.chess_whole = pos.toFen()
  1030. resolve(robot_data)
  1031. });
  1032. }, 0)
  1033. })
  1034. }
  1035. send_msg_self_move(setup:setupData){
  1036. if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
  1037. msgManager.sendPlayChess(setup)
  1038. }
  1039. }
  1040. /** 判断一步棋是否合法,如果合法,就执行这步棋。 */
  1041. addMove(mv, computerMove) {
  1042. console.log("addMove",mv)
  1043. var is_legalMove = !this.pos.legalMove(mv)
  1044. if (is_legalMove) {
  1045. console.log("不合法",mv)
  1046. this.moveAni(mv, computerMove,false)
  1047. return false;
  1048. }
  1049. var is_makeMove = !this.pos.makeMove(mv)
  1050. if (is_makeMove) {
  1051. UITips.show("不能送将!");
  1052. this.moveAni(mv, computerMove,false)
  1053. return false;
  1054. }
  1055. this.moveAni(mv, computerMove)
  1056. // var timer = setInterval(function() {
  1057. // if (step == 0) {
  1058. // clearInterval(timer);
  1059. // getNode.x = xSrc;
  1060. // getNode.y = ySrc;
  1061. // if(computerMove)this_.isComputerMoveing=false;
  1062. // this_.postAddMove(mv, computerMove);
  1063. // } else {
  1064. // getNode.x = MOVE_PX(xSrc, xDst, step);
  1065. // getNode.y = MOVE_PX(ySrc, yDst, step);
  1066. // step --;
  1067. // }
  1068. // }, 16);
  1069. return true;
  1070. }
  1071. showHuiheLabel(computerMove){
  1072. // if(this.computer==0)
  1073. // {
  1074. // if(computerMove)
  1075. // this.huihelabel.string="黑方回合";
  1076. // else this.huihelabel.string="红方回合";
  1077. // }
  1078. // else{
  1079. // if(computerMove)
  1080. // this.huihelabel.string="红方回合";
  1081. // else this.huihelabel.string="黑方回合";
  1082. // }
  1083. }
  1084. drawOldAndNewSelect(old_sq,new_sq,isShow){
  1085. let old_node = this.imgSquares[this.flipped(old_sq)];
  1086. let new_node = this.imgSquares[this.flipped(new_sq)];
  1087. if(isShow){
  1088. old_node.getChildByName("track1").active = true;
  1089. old_node.getChildByName("track2").active = false;
  1090. new_node.getChildByName("track1").active = false;
  1091. new_node.getChildByName("track2").active = true;
  1092. }else{
  1093. old_node.getChildByName("track1").active = false;
  1094. old_node.getChildByName("track2").active = false;
  1095. new_node.getChildByName("track1").active = false;
  1096. new_node.getChildByName("track2").active = false;
  1097. }
  1098. }
  1099. postAddMove(mv, computerMove):boolean {
  1100. this.showHuiheLabel(computerMove);
  1101. if (this.mvLast > 0) {
  1102. this.drawSquare(SRC(this.mvLast), false,false);
  1103. this.drawSquare(DST(this.mvLast), false,false);
  1104. this.drawOldAndNewSelect(SRC(this.mvLast),DST(this.mvLast),false)
  1105. }
  1106. this.drawSquare(SRC(mv), true,false);
  1107. this.drawSquare(DST(mv), true,false);
  1108. this.drawOldAndNewSelect(SRC(mv),DST(mv),true)
  1109. this.sqSelected = 0;
  1110. this.mvLast = mv;
  1111. if (this.pos.isMate()) { //被将死
  1112. this.result = computerMove ? RESULT_LOSS : RESULT_WIN;
  1113. var pc = SIDE_TAG(this.pos.sdPlayer) + PIECE_KING;
  1114. var sqMate = 0;
  1115. for (var sq = 0; sq < 256; sq ++) {
  1116. if (this.pos.squares[sq] == pc) {
  1117. sqMate = sq;
  1118. break;
  1119. }
  1120. }
  1121. if (!this.animated || sqMate == 0) {
  1122. this.postMate(computerMove);
  1123. return false;
  1124. }
  1125. this.postMate(computerMove);
  1126. return false;
  1127. // sqMate = this.flipped(sqMate);
  1128. // var style = this.imgSquares[sqMate].style;
  1129. // style.zIndex = 256;
  1130. // var xMate = SQ_X(sqMate);
  1131. // var step = MAX_STEP;
  1132. // var this_ = this;
  1133. // var timer = setInterval(function() {
  1134. // if (step == 0) {
  1135. // clearInterval(timer);
  1136. // style.left = xMate + "px";
  1137. // style.zIndex = 0;
  1138. // this_.imgSquares[sqMate].src = this_.images +
  1139. // (this_.pos.sdPlayer == 0 ? "r" : "b") + "km.gif";
  1140. // this_.postMate(computerMove);
  1141. // } else {
  1142. // style.left = (xMate + ((step & 1) == 0 ? step : -step) * 2) + "px";
  1143. // step --;
  1144. // }
  1145. // }, 50);
  1146. // return;
  1147. }
  1148. // var vlRep = this.pos.repStatus(3);
  1149. // if (vlRep > 0) {
  1150. // vlRep = this.pos.repValue(vlRep);
  1151. // if (vlRep > -WIN_VALUE && vlRep < WIN_VALUE) {
  1152. // UITips.show("双方不变作和");
  1153. // //this.heqi();
  1154. // } else if (computerMove == (vlRep < 0)) {
  1155. // this.result = RESULT_LOSS;
  1156. // UITips.show("长打作负");
  1157. // //this.postMate(true);
  1158. // } else {
  1159. // this.result = RESULT_WIN;
  1160. // UITips.show("长打作负");
  1161. // //this.postMate(false);
  1162. // }
  1163. // return;
  1164. // }
  1165. if (this.pos.captured()) {
  1166. var hasMaterial = false;
  1167. for (var sq = 0; sq < 256; sq ++) {
  1168. if (IS_INBOARD(sq) && (this.pos.squares[sq] & 7) > 2) {
  1169. hasMaterial = true;
  1170. break;
  1171. }
  1172. }
  1173. if (!hasMaterial) {
  1174. UITips.show("双方都没有进攻棋子了,作和!");
  1175. this.heqi();
  1176. return false;
  1177. }
  1178. } else if (this.pos.pcList.length > 100) {
  1179. var captured = false;
  1180. for (var i = 2; i <= 100; i ++) {
  1181. if (this.pos.pcList[this.pos.pcList.length - i] > 0) {
  1182. captured = true;
  1183. break;
  1184. }
  1185. }
  1186. if (!captured) {
  1187. UITips.show("超过自然限着,作和");
  1188. this.heqi();
  1189. return false;
  1190. }
  1191. }
  1192. if (this.pos.inCheck()) {
  1193. this.FaceKingWarning();
  1194. // this.playSound(computerMove ? "check2" : "check");
  1195. } else if (this.pos.captured()) {
  1196. this.onchiGameAni()
  1197. } else {
  1198. // AudioMng.Instance.PlaySoundByName(AudioType.put)
  1199. }
  1200. this.bushu++;
  1201. this.refreshBushulabel();
  1202. this.response();
  1203. return true;
  1204. }
  1205. refreshBushulabel(){
  1206. // this.bushulabel.string='步数:'+this.bushu;
  1207. }
  1208. //和棋做赢
  1209. heqi(){
  1210. this.result = RESULT_DRAW;
  1211. this.busy = false;
  1212. if(ChessType.renjiboyi === ChessMng.Instance.chesstype){
  1213. this.gameEnd(0,true);
  1214. }else{
  1215. if(GameMng._userData.room.firstid==0){
  1216. msgManager.heqi_game()
  1217. }
  1218. }
  1219. }
  1220. //结束
  1221. postMate(computerMove) {
  1222. console.log("移动的是不是电脑",computerMove,this.result)
  1223. if(ChessType.renjiboyi === ChessMng.Instance.chesstype){
  1224. let result=computerMove?1:0;
  1225. this.gameEnd(result);
  1226. }else{
  1227. if(!computerMove){ //说明电脑被将死
  1228. this.postMateNew();
  1229. }
  1230. }
  1231. this.busy = false;
  1232. }
  1233. postMateNew(){
  1234. console.log("result",this.result)
  1235. //this.gameEnd(this.result);
  1236. this.busy = false;
  1237. }
  1238. setSearch (hashLevel) {
  1239. this.search = hashLevel == 0 ? null : new Search();
  1240. this.search.setSearch(this.pos, hashLevel)
  1241. }
  1242. updateRound(){
  1243. if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
  1244. if(this.who_round===1){
  1245. this.busy = false;
  1246. }else{
  1247. this.busy = true;
  1248. }
  1249. }
  1250. }
  1251. response() {
  1252. if(GameMng._userData.room!=null){
  1253. console.log("谁的回合",GameMng._userData.room.round)
  1254. }
  1255. if (this.search == null || !this.computerMove()) {
  1256. this.busy = false;
  1257. return;
  1258. }
  1259. // this.thinking.style.visibility = "visible";
  1260. var self = this;
  1261. this.busy = true;
  1262. setTimeout(() => {
  1263. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  1264. if(UIDiffSel.selectIndex==0){
  1265. self.search.xiaoBaiSearchMain((a)=>{
  1266. director.once(Director.EVENT_AFTER_DRAW,function(){
  1267. self.addMove(a,true);
  1268. })
  1269. });
  1270. }else{
  1271. self.search.searchMain(UIDiffSel.selectIndex+1,self.millis,(a)=>{
  1272. director.once(Director.EVENT_AFTER_DRAW,function(){
  1273. self.addMove(a,true);
  1274. });
  1275. });
  1276. }
  1277. }else{
  1278. if(ChessMng.Instance.isHaoyouRoom()){
  1279. console.log("轮到好友走棋子")
  1280. }
  1281. }
  1282. }, 200);
  1283. }
  1284. // ab():Promise<any>{
  1285. // return new Promise<any>(async (resolve, reject) => {
  1286. // let a=this.search.searchMain(LIMIT_DEPTH, this.millis);
  1287. // resolve(a);
  1288. // });
  1289. // }
  1290. computerMove() {
  1291. if(GameMng._userData.room===null||GameMng._userData.room===undefined){
  1292. return this.pos.sdPlayer == this.computer;
  1293. }
  1294. if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
  1295. }else{
  1296. if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
  1297. return this.pos.sdPlayer == GameMng._userData.room.firstid;
  1298. }
  1299. }
  1300. return this.pos.sdPlayer == this.computer;
  1301. }
  1302. //刷新棋盘,也就是重新显示棋盘上的棋子。
  1303. flushBoard() {
  1304. this.mvLast = this.pos.mvList[this.pos.mvList.length - 1];
  1305. for (var sq = 0; sq < 256; sq ++) {
  1306. if (IS_INBOARD(sq)) {
  1307. this.drawSquare(sq, sq == SRC(this.mvLast) || sq == DST(this.mvLast),false);
  1308. }
  1309. }
  1310. }
  1311. drawCanMovePath(sqList){
  1312. for(var i=0;i<sqList.length;i++){
  1313. var sq = sqList[i];
  1314. var gnodeSp = this.imgSquares[this.flipped(sq)]
  1315. if(gnodeSp){
  1316. gnodeSp.getChildByName('path').active=true;
  1317. }
  1318. }
  1319. }
  1320. hideAllPath(){
  1321. for (let index = 0; index < this.imgSquares.length; index++) {
  1322. const gnodeSp = this.imgSquares[index];
  1323. if(gnodeSp){
  1324. gnodeSp.getChildByName('path').active=false;
  1325. }
  1326. }
  1327. }
  1328. hideAllTrack(){
  1329. for (let index = 0; index < this.imgSquares.length; index++) {
  1330. const gnodeSp = this.imgSquares[index];
  1331. if(gnodeSp){
  1332. gnodeSp.getChildByName('track1').active=false;
  1333. gnodeSp.getChildByName('track2').active=false;
  1334. }
  1335. }
  1336. }
  1337. //显示sq位置的棋子图片。如果该位置没棋子,则显示一张透明的图片。
  1338. drawSquare(sq, selected,isSelfOpe) {
  1339. var node__ = this.imgSquares[this.flipped(sq)];
  1340. var gnodeSp =node__.getChildByName("chess_bg").getComponent(Sprite) as Sprite;
  1341. var sp =gnodeSp.node.getChildByName("chess_qz").getComponent(Sprite) as Sprite;
  1342. let spName=PIECE_NAME[this.pos.squares[sq]]
  1343. var shadow = node__.getChildByName("chess_put_down_shadow");
  1344. var self = this;
  1345. if(spName=="oo"){
  1346. gnodeSp.enabled=false;
  1347. sp.node.active = false;
  1348. shadow.active = false;
  1349. }
  1350. else{
  1351. ResMng.LoadImg(spName,sp,()=>{
  1352. this.moveNode.active=false;
  1353. gnodeSp.enabled=true;
  1354. shadow.active = true;
  1355. sp.node.active = true;
  1356. },this);
  1357. }
  1358. if(selected){
  1359. if(isSelfOpe){
  1360. // tween()
  1361. // .target(gnodeSp.node)
  1362. // .to(0.1,{ scale: new Vec3(1.1, 1.1, 1.1) })
  1363. // .start();
  1364. if(spName=="oo"){
  1365. }
  1366. else{
  1367. ResMng.LoadImg("s_"+spName,sp,()=>{
  1368. sp.node.position = new Vec3(sp.node.position.x,54,sp.node.position.z);
  1369. },this);
  1370. }
  1371. gnodeSp.node.getComponent(UITransform).width = 112;
  1372. gnodeSp.node.getComponent(UITransform).height = 117;
  1373. gnodeSp.spriteFrame = self.select_chess_icon;
  1374. }
  1375. //gnodeSp.node.scale = new Vec3(1.1,1.1,1.1);
  1376. }else{
  1377. //gnodeSp.node.scale = new Vec3(1,1,1)
  1378. sp.node.position = new Vec3(sp.node.position.x,48,sp.node.position.z);
  1379. gnodeSp.node.getComponent(UITransform).width = self.moveNode.getChildByName("chess_bg").getComponent(UITransform).width;
  1380. gnodeSp.node.getComponent(UITransform).height = self.moveNode.getChildByName("chess_bg").getComponent(UITransform).height;
  1381. gnodeSp.spriteFrame = self.moveNode.getChildByName("chess_bg").getComponent(Sprite).spriteFrame;
  1382. }
  1383. // node__.getChildByName('selected').active=selected;
  1384. // img.src = this.images + PIECE_NAME[this.pos.squares[sq]] + ".gif";
  1385. // img.style.backgroundImage = selected ? "url(" + this.images + "oos.gif)" : "";
  1386. }
  1387. //此处判定画什么颜色的象棋
  1388. flipped(sq) {
  1389. return this.computer == 0 ? SQUARE_FLIP(sq) : sq;
  1390. }
  1391. huiqi(isReSelf:boolean){
  1392. this.result = RESULT_UNKNOWN;
  1393. if (this.pos.mvList.length > 1) {
  1394. this.pos.undoMakeMove();
  1395. }
  1396. // if(this.computerMove()){
  1397. // console.log("我方悔棋",this.pos.sdPlayer)
  1398. // }else{
  1399. // console.log("对方悔棋",this.pos.sdPlayer)
  1400. // }
  1401. // if (this.pos.mvList.length > 1 && this.computerMove()) {
  1402. // this.pos.undoMakeMove();
  1403. // }
  1404. this.flushBoard();
  1405. this.hideAllPath();
  1406. console.log("isReSelf",isReSelf)
  1407. if (this.mvLast > 0) {
  1408. this.drawSquare(SRC(this.mvLast), false,false);
  1409. this.drawSquare(DST(this.mvLast), false,false);
  1410. this.drawOldAndNewSelect(SRC(this.mvLast),DST(this.mvLast),false)
  1411. }
  1412. if(!isReSelf){
  1413. if(this.mv_rival_last>0){
  1414. this.mvLast = this.mv_rival_last;
  1415. this.drawOldAndNewSelect(SRC(this.mv_rival_last),DST(this.mv_rival_last),true)
  1416. }
  1417. }
  1418. this.response();
  1419. console.log("this.mvLast == ",this.mvLast)
  1420. }
  1421. }