GBoardChess.ts 52 KB

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