123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578 |
- import ADSDK from '../../gcommon/AdSdk/ADSDK';
- import AudioMng from '../../gcommon/AudioMng';
- import { UIButton } from '../../gcommon/UIButton';
- import GameUtil from '../../gcommon/GameUtil';
- import { ResMng } from '../../gcommon/ResMng';
- import UISetting from '../../gcommon/UISetting';
- import UITips from '../../gcommon/UITips';
- import { GameMng, quit_chess_show_view } from '../../GameMng';
- import CanjuModel from '../ChessCanJuSel/CanjuModel';
- import ChessMng, { ChessType } from '../DiffSel/ChessMng';
- import UIDiffSel from '../DiffSel/UIDiffSel';
- import ChessPos from '../ChessPos';
- import Search from '../Search';
- import UIEndChess from './UIEndChess';
- import ChessRBSel from './ChessRBSel';
- import { Vec3,tween,UITransform ,_decorator ,Component,Label,Node,instantiate,Animation,Prefab,Sprite, director, Director, SpriteFrame} from "cc";
- import { UIjiesuan } from '../UIjiesuan';
- import { ChessThink } from './ChessThink';
- import { ClientEvent } from '../../clientEvent';
- import { Constant, robot_ai_level, room_type } from '../../constant';
- import { roomData } from '../../UserData/roomData';
- import { UIManager } from '../../gcommon/UIManager';
- import { waitFirend } from '../waitFirend';
- import UIDialog from '../UIDialog';
- import ScenceMng from '../../gcommon/ScenceMng';
- import { setupData } from '../../UserData/setupData';
- import { msgManager } from '../../socket/msgManager';
- import { settlementData } from '../../UserData/settlementData';
- import { Tools } from '../../Tools';
- import { timeCountData } from '../../UserData/timeCountData';
- import { waitView } from '../waitView';
- import { waitJifen } from '../waitJifen';
- const {ccclass, property} = _decorator;
- var LIMIT_DEPTH = 64;
- ///
- var MATE_VALUE = 10000;
- var WIN_VALUE = MATE_VALUE - 200;
- var PIECE_KING=0;
- //////
- var RESULT_UNKNOWN = 0;
- var RESULT_WIN = 1;
- var RESULT_DRAW = 2;
- var RESULT_LOSS = 3;
- var BOARD_WIDTH = 965;
- var BOARD_HEIGHT = 1080;
- var SQUARE_SIZE = 106;
- var SQUARE_LEFT = (BOARD_WIDTH - SQUARE_SIZE * 9) >> 1;
- var SQUARE_TOP = (BOARD_HEIGHT - SQUARE_SIZE * 10) >> 1;
- var THINKING_SIZE = 32;
- var THINKING_LEFT = (BOARD_WIDTH - THINKING_SIZE) >> 1;
- var THINKING_TOP = (BOARD_HEIGHT - THINKING_SIZE) >> 1;
- var MAX_STEP = 8;
- var PIECE_NAME = [
- "oo", null, null, null, null, null, null, null,
- "hj", "hs", "hx", "hm", "hc", "hp", "hz", null,
- "bj", "bs", "bx", "bm", "bc", "bp", "bz", null,
- ];
- var IN_CHESS_BOARD = [
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- ];
- function IS_INBOARD(sq) {
- return IN_CHESS_BOARD[sq] != 0;
- }
- function COORD_XY(x, y) {
- return x + (y << 4);
- }
- function FILE_X(sq) {
- return sq & 15;
- }
- function RANK_Y(sq) {
- return sq >> 4;
- }
- function SQ_X(sq) {
- return SQUARE_LEFT + (FILE_X(sq) - 3) * SQUARE_SIZE;
- }
- function SQ_Y(sq) {
- return -(SQUARE_TOP + (RANK_Y(sq) - 3) * SQUARE_SIZE);
- }
- function MOVE_PX(src, dst, step) {
- return Math.floor((src * step + dst * (MAX_STEP - step)) / MAX_STEP + .5);
- }
- function alertDelay(message) {
- setTimeout(function () {
- alert(message);
- }, 250);
- }
- function SRC(mv) {
- return mv & 255;
- }
-
- function DST(mv) {
- return mv >> 8;
- }
- function SQUARE_FLIP(sq) {
- return 254 - sq;
- }
- function SIDE_TAG(sd) {
- return 8 + (sd << 3);
- }
- function MOVE(sqSrc, sqDst) {
- return sqSrc + (sqDst << 8);
- }
- @ccclass
- export default class GBoardChess extends Component {
- isComputerMoveing:boolean=false;
- // @property(Label)
- // huihelabel:Label=null;
- // @property(Label)
- // timelabel:Label=null;
- // @property(Label)
- // bushulabel:Label=null;
- @property(Node)
- bottom_node:Node=null;
- @property(Animation)
- animation:Animation=null;
- @property(Node)
- Juesha:Node=null;
- @property(Prefab)
- JueshaAniPrefab:Node=null;
- @property(Prefab)
- FaceKingPrefab:Prefab=null;
- @property(Prefab)
- startGameAniPrefab:Prefab=null;
- @property(Prefab)
- chiAniPrefab:Prefab=null;
- // @property(Label)
- // titlelabel:Label=null;
- @property(Node)
- GBoardChess:Node=null;
- @property(Node)
- chessNode: Node = null;
- @property(Node)
- wait_haoyou: Node = null;
- @property(Node)
- wait_jifen: Node = null;
- @property(Node)
- wait_renji: Node = null;
- @property(Node)
- chatNode: Node = null;
- @property(SpriteFrame)
- select_chess_icon: SpriteFrame = null;
- @property(Node)
- jiesuan_node:Node=null;
- @property(Node)
- headNode: Node = null;
- @property(Node)
- uiLayer: Node = null;
- @property(Node)
- chat_view: Node = null;
- pos:ChessPos=null;
- search:Search=null;
- imgSquares:Node[]=[];
- sqSelected:number=0; //当前被选中棋子的位置。
- mvLast:number=0;
- millis:number=0;
- computer:number=0;
- result:number=0;
- busy:boolean=false;
- animated:boolean=false;
- static instance:GBoardChess=null;
- moveNode:Node=null;
- isStartGame:boolean = false;
- static g_isStartFirstGame:boolean = true;
- isStartFirstGame:boolean = true;
- selfLaseSelectNode:Node=null;
- wait_rival_status:number=0; // 0等待,1好友已加入
- wait_rival_startGame_status:number = 0;//0,都没准备,1,好友未准备,2,自己未准备,3,开始下棋
- //temp test ai
- tempsqSelected:number=0;
- tempsqDes:number=0;
- gTime:number=0;
- isend:boolean=false;
- bushu:number=0;
- result_pos:any[] =[]
- last_settlementData:settlementData= null;
- who_round:number = 0 //1 自己回手,0对方回合
- round_id:number = 0 //回合者的id
- moveCallfun:any = null
- rund_number:number = 0 //当前第多少步
- heqi_number:number = 0
- huiqi_number:number = 0
- huiqi_setup:number = 0;
- moveList:any[] = []
- mv_rival_last:number=0;
-
- start () {
-
- GBoardChess.instance=this;
- this.moveNode=instantiate(this.chessNode);
- //this.moveNode.removeAllChildren();
- //this.huihelabel.string="开始游戏";
- this.initNeed();
- this.setTitlelabel();
- this.startGame();
- this.regMsg();
-
- }
- regMsg(){
- ClientEvent.on(Constant.EVENT_TYPE.MSG_ROOM_STATUS,this.recv_room_status,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_DISMISS_ROOM,this.recv_room_dismiss,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_OWN_PLAYCHESS,this.recv_own_playchess,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_RIVAL_PLAYCHESS,this.recv_rival_playchess,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_SETTLEMENT,this.recv_settlement,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_QUIT_ROOM,this.recvQuit,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_PVP_TIME,this.recv_time_count,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_HEQI_JIESHOU,this.recv_jieshou_heqi_game,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_HUIQI_JIESHOU,this.recv_jieshou_huiqi_game,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_HEQI_TONGZHI,this.recv_tongzhi_heqi_game,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_HUIQI_TONGZHI,this.recv_tongzhi_huiqi_game,this)
- ClientEvent.on(Constant.EVENT_TYPE.MSG_START_HUIQI,this.recv_start_huiqi,this)
- ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_HEQI,this.onBtnHeQi,this)
- ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_HUIQI,this.onBtnHuiQi,this)
- ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_SHOW_CHAT,this.onBtnShowChat,this)
- ClientEvent.on(Constant.UI_EVENT.UI_MSG_BTN_HIDE_CHAT,this.onBtnHideChat,this)
- }
-
- onDestroy(){
- GBoardChess.instance=null;
- ClientEvent.off(Constant.EVENT_TYPE.MSG_ROOM_STATUS,this.recv_room_status,this)
- ClientEvent.off(Constant.EVENT_TYPE.MSG_DISMISS_ROOM,this.recv_room_dismiss,this)
- ClientEvent.off(Constant.EVENT_TYPE.MSG_OWN_PLAYCHESS,this.recv_own_playchess,this)
- ClientEvent.off(Constant.EVENT_TYPE.MSG_RIVAL_PLAYCHESS,this.recv_rival_playchess,this)
- ClientEvent.off(Constant.EVENT_TYPE.MSG_SETTLEMENT,this.recv_settlement,this)
- ClientEvent.off(Constant.EVENT_TYPE.MSG_QUIT_ROOM,this.recvQuit,this)
- ClientEvent.off(Constant.EVENT_TYPE.MSG_PVP_TIME,this.recv_time_count,this)
- ClientEvent.off(Constant.EVENT_TYPE.MSG_HEQI_JIESHOU,this.recv_jieshou_heqi_game,this)
- ClientEvent.off(Constant.EVENT_TYPE.MSG_HUIQI_JIESHOU,this.recv_jieshou_huiqi_game,this)
- ClientEvent.off(Constant.EVENT_TYPE.MSG_HUIQI_TONGZHI,this.recv_tongzhi_huiqi_game,this)
- ClientEvent.off(Constant.EVENT_TYPE.MSG_START_HUIQI,this.recv_start_huiqi,this)
- ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_HEQI,this.onBtnHeQi,this)
- ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_HUIQI,this.onBtnHuiQi,this)
- ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_SHOW_CHAT,this.onBtnShowChat,this)
- ClientEvent.off(Constant.UI_EVENT.UI_MSG_BTN_HIDE_CHAT,this.onBtnHideChat,this)
- }
- onBtnShowChat(){
- this.chat_view.active = true;
- }
- onBtnHideChat(){
- this.chat_view.active = false;
- }
- onBtnHuiQi(){
- msgManager.qingqiu_huiqi_game()
- waitView.Show("等待对方是否同意悔棋。")
- }
- onBtnHeQi(){
- msgManager.qingqiu_heqi_game()
- waitView.Show("等待对方是否同意和棋。")
- }
- checkWaitStatus(){
- if(GameMng._userData.room_info.chess!=null){
- if(GameMng._userData.room_info.chess.wait_select_result!=null&&GameMng._userData.room_info.chess.wait_select_result!=undefined){
- switch(GameMng._userData.room_info.chess.wait_select_result){
- case 0:
- {
- waitView.Show("等待对方是否同意悔棋。")
- break;
- }
- case 1:
- {
- waitView.Show("等待对方是否同意和棋。")
- break;
- }
- case 2:
- {
- this.recv_jieshou_huiqi_game()
- break;
- }
- case 3:
- {
- this.recv_jieshou_heqi_game()
- break;
- }
- }
- }
- }
- }
- recv_start_huiqi(setup:setupData){
- var move_data = MOVE(setup.src_pos,setup.dec_pos)
- // var src = COORD_XY(FILE_X(setup.src_pos),15-RANK_Y(setup.src_pos))
- // var des = COORD_XY(FILE_X(setup.dec_pos),15-RANK_Y(setup.dec_pos))
- // move_data = MOVE(des,src)
- this.huiqi_setup = setup.huiqi_setup+1;
- this.pos.pushMvList(move_data)
- if(setup.userid!=GameMng._userData.userid){ //说明不是自己
- console.log("开始对方开始悔棋",setup)
- this.huiqi(true)
- }else{
- this.huiqi(false)
- console.log("开始我方开始悔棋",setup)
- }
- console.log("move_data",move_data)
- this.who_round = 1 - this.who_round
- if(this.isStartGame===true){
- this.headNode.getComponent(ChessThink).startThink(true)
- }
- }
- recv_tongzhi_heqi_game(obj:any){
- if(obj.type === 0){
- UITips.show("对方拒绝和棋");
- }else if(obj.type === 1){
- UITips.show("协商和棋,对局结束");
- }
- UIManager.removeWaitViewLayer()
- this.heqi_number-=1;
- }
- recv_tongzhi_huiqi_game(obj:any){
- console.log("recv_tongzhi_huiqi_game",obj.type)
- if(obj.type === 0){
- UITips.show("对方拒绝悔棋");
- this.huiqi_number-=1;
- }else if(obj.type === 1){
- UITips.show("对方同意悔棋");
- this.huiqi_number-=1;
- }else if(obj.type === 2){
- UITips.show("悔棋请求超时!");
- }
- UIManager.removeWaitViewLayer()
-
- }
- recv_jieshou_heqi_game(){
- UIDialog.Show(()=>{
- msgManager.tongzhi_heqi_game(1)
- },()=>{
- msgManager.tongzhi_heqi_game(0)
- },"对方请求提和,是否同意",null,true)
- }
- recv_jieshou_huiqi_game(){
- // this.who_round===1&&
- if(!this.computerMove()){
- UIDialog.Show(()=>{
- msgManager.tongzhi_huiqi_game(1)
- },()=>{
- msgManager.tongzhi_huiqi_game(0)
- },"对方请求悔棋,是否同意",null,true)
- }else{
- msgManager.tongzhi_huiqi_game(2)
- }
- }
- recv_time_count(time_count_data:timeCountData){
- GameMng._userData.room.time_count_data = time_count_data;
- if(this.round_id!=time_count_data.round_id){
- this.round_id = time_count_data.round_id
- }else{
- }
- this.headNode.getComponent(ChessThink).startThink(false)
- if(time_count_data.round_id===GameMng._userData.userid){
- console.log("我的回合",time_count_data)
- this.who_round = 1;
- }else{
- this.who_round = 0;
- console.log("对方回合",time_count_data)
- }
- this.updateRound()
- }
- recvQuit(obj:any={type:0}){
- if(obj.type===0){
- }else{
- GameMng.Instance.quit_view = quit_chess_show_view.beitichu;
- }
- ChessMng.Instance.chesstype = ChessType._null;
- GameMng._userData.room = null;
- AudioMng.Instance.PlayBGM(GameMng.Instance.mainbgm);
- ScenceMng.Instance.load('GameLaunch');
- }
- recv_own_playchess(setup:setupData){
- console.log("自己完成下棋",setup.userid)
- // this.rund_number = setup.rund_number
- this.who_round = 0;
- this.updateRound();
- this.rund_number = setup.rund_number
- if(this.isStartGame===true){
- if(setup.pvp_time==null||setup.pvp_time==undefined){
- }else{
- GameMng._userData.room.time_count_data = setup.pvp_time;
- }
- this.headNode.getComponent(ChessThink).startThink(true,true)
- }
-
- }
- recv_settlement(settlement:settlementData){
- if(GameMng._userData.room==null){
- }else{
- UIManager.removeDialogViewLayer()
- UIManager.removeWaitViewLayer()
- this.headNode.getComponent(ChessThink).stopThink()
- console.log("结算",settlement)
- this.last_settlementData = settlement
- if(this.moveList.length>0){
- this.moveCallfun = ()=>{
- console.log("移动完,开始结算!")
- this.settlement(settlement)
- }
- }else{
- this.settlement(settlement)
- }
- this.isStartGame = false;
- if(GameMng._userData.room.rival==null){
-
- }
- let self = this;
- this.scheduleOnce(()=>{
- if(ChessMng.Instance.isHaoyouRoom()){
- this.wait_haoyou.active = true;
- self.wait_haoyou.getComponent(waitFirend).recvAgainRound(GameMng._userData.room)
- }else if(ChessMng.Instance.isJiFenRoom()){
- this.showWaitJiFen()
- self.wait_jifen.getComponent(waitJifen).recvAgainRound(GameMng._userData.room)
- }
- },3)
- GameMng._userData.room.time_count_data = null
-
- }
- }
- recv_rival_playchess(setup:setupData){
- this.who_round = 1;
- this.updateRound()
- // console.log("self.tempsqDes",self.tempsqDes)
- // console.log("self.tempsqSelected",self.tempsqSelected)
- if(this.isStartGame ==true){
- var self = this;
- this.moveList.push("0")
- // var src = COORD_XY(FILE_X(setup.src_pos),15-RANK_Y(setup.src_pos))
- // var des = COORD_XY(FILE_X(setup.dec_pos),15-RANK_Y(setup.dec_pos))
- // console.log("src ",src);
- // console.log("des ",des);
- // console.log("des row ",RANK_Y(des));
- this.mv_rival_last = MOVE(setup.src_pos, setup.dec_pos)
- director.once(Director.EVENT_AFTER_DRAW,function(){
- self.addMove(MOVE(setup.src_pos, setup.dec_pos),true);
- })
- console.log("对手下棋了",setup)
- this.rund_number = setup.rund_number
- if(this.isStartGame===true){
- if(setup.pvp_time==null||setup.pvp_time==undefined){
- }else{
- GameMng._userData.room.time_count_data = setup.pvp_time;
- }
- this.headNode.getComponent(ChessThink).startThink(true,true)
- }
- ClientEvent.dispatchEvent(Constant.UI_EVENT.UI_MSG_UPDATE_MENU_STATUS)
- }
- }
- recv_room_dismiss(room:roomData){
- GameMng._userData.room = null;
- UIDialog.Show(()=>{
- AudioMng.Instance.PlayBGM(GameMng.Instance.mainbgm);
- ScenceMng.Instance.load('GameLaunch');
- },()=>{
- },"房间已解散",null,false)
- }
- recv_room_status(room:roomData){
- UIManager.removeLoadingLayer()
- GameMng.updateRoomData(room)
- this.headNode.getComponent(ChessThink).StatusShow()
- this.onRecvStartGame(room)
-
- }
- onRecvStartGame(room:roomData){
- if(ChessMng.Instance.isHaoyouRoom()){
- this.wait_haoyou.getComponent(waitFirend).recvReady(room)
- }else if(ChessMng.Instance.isJiFenRoom()){
- this.wait_jifen.getComponent(waitJifen).recvReady(room)
- }
- console.log("this.isStartGame== ",this.isStartGame,room.wait_rival_startGame_status,room.rival)
- if(room.wait_rival_startGame_status === 3 &&!this.isStartGame){
- this.isStartGame = true;
- this.userReadyStartGame()
- this.headNode.getComponent(ChessThink).startThink(true)
- if(room.isfirst_start===1){
- this.onstartGameAni()
- }
- }
- if(room.rival===undefined||room.rival===null){
- //this.hideAllChess()
- }
- }
- private startGame(){
- if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
- this.ShowRoleSel();
- this.isStartGame = true;
- }else{
- this.initChessBoard();
- }
- }
- countTime(){
- if(this.isend)return;
- this.gTime++;
- //this.timelabel.string="耗时:"+GameUtil.FormatTime_HHMMSS(this.gTime*1000)
- }
- ShowRoleSel(){
- //this.uirbselect.active=true;
- GBoardChess.instance.initChessBoard()
- }
-
- updateWaitStatus(){
- if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
- this.isStartGame = true;
- this.onstartGameAni()
- }else{
- this.wait_rival_status = GameMng._userData.room.wait_rival_status
- this.wait_rival_startGame_status = GameMng._userData.room.wait_rival_startGame_status
- this.onRecvStartGame(GameMng._userData.room);
- }
- this.headNode.getComponent(ChessThink).StatusShow()
- // if(this.wait_rival_status==0){ //房间只有自己还在等待
-
- // }else{//对手已加入房间
- // if(this.wait_rival_startGame_status===0){ //0,都没准备
- // }else if(this.wait_rival_startGame_status===1){ //1,好友未准备
- // }else if(this.wait_rival_startGame_status===2){ //2,自己未准备
- // }else if(this.wait_rival_startGame_status===3){ //3 都准备好开始下棋
- // }
- // }
- }
- setTitlelabel(){
- // if(ChessMng.Instance.chesstype==ChessType.renjiboyi)
- // this.titlelabel.string="难度:"+UIDiffSel.lvArr[UIDiffSel.selectIndex];
- // else if(ChessMng.Instance.chesstype==ChessType.canju)
- // this.titlelabel.string=CanjuModel.Instance.canjuData.name;
- }
- initChessInfo(){
- this.isComputerMoveing=false;
- this.busy=false;
- this.isend=false;
- this.huiqi_setup = 0;
- this.gTime=0;
- this.millis = this.getDiff();
- this.bushu=0;
- this.setTitlelabel();
- this.hideAllTrack();
- this.hideAllPath();
- this.result = RESULT_UNKNOWN;
- this.round_id = -1;
- this.rund_number = 0;
- this.heqi_number = 0;
- this.huiqi_number = 0;
- console.log("initChessInfo")
- }
- hideAllChess(){
- if(this.imgSquares.length>0){
- for (let index = 0; index < this.imgSquares.length; index++) {
- const element = this.imgSquares[index];
- if(element!=null){
- element.active =false;
- }
-
- }
- }
-
- }
- showAllChess(){
- if(this.imgSquares.length>0){
- for (let index = 0; index < this.imgSquares.length; index++) {
- const element = this.imgSquares[index];
- if(element!=null){
- element.active =true;
- }
-
- }
- }
- }
- //初始化棋盘
- initChessBoard(){
- this.initChessInfo()
- // this.isComputerMoveing=false;
- // this.busy=false;
- // this.isend=false;
- // this.gTime=0;
- // this.millis = this.getDiff();
- // this.bushu=0;
- // this.unschedule(this.countTime);
- // this.schedule(this.countTime, 1);
- // this.setTitlelabel();
- AudioMng.Instance.PlayBGM(GameMng.Instance.gamebgm)
- console.log(this.millis)
- if(ChessMng.Instance.chesstype==ChessType.canju){
- ChessRBSel.selIndex=0;
- }
- this.showHuiheLabel(ChessRBSel.selIndex == 0);
- // AudioMng.Instance.PlaySoundByName(GameMng.Instance.startclip)
- this.refreshBushulabel();
- //this.timelabel.string='耗时:00:00';
- if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
- this.pos.fromFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1");
- this.computer = 1 - ChessRBSel.selIndex;
- this.flushBoard();
- }else if(ChessMng.Instance.chesstype==ChessType.canju){
- this.pos.fromFen(CanjuModel.Instance.canjuData.fen);
- }else{
- if(ChessMng.Instance.isHaoyouRoom()){
- this.showWaitHaoYou();
- }else if(ChessMng.Instance.isJiFenRoom()){
- this.showWaitJiFen();
- }
- }
- this.updateWaitStatus();
- setTimeout(() => {
- this.response();
- }, 500);
-
- }
- userReadyStartGame(){
- if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
- }else{
- if(ChessMng.Instance.isHaoyouRoom()){
- this.showAllChess();
- this.wait_haoyou.active = false;
- } else if(ChessMng.Instance.isJiFenRoom()){
- this.showAllChess();
- this.wait_jifen.active = false;
- }
- }
-
- //ChessRBSel.selIndex = 1-GameMng._userData.room.firstid
- //console.log("this.compute",this.computer)
- if(this.isStartFirstGame){
-
- }else{
- console.log("第二次初始化棋盘")
- this.initChessInfo()
- }
- this.computer = GameMng._userData.room.firstid;
- this.busy = GameMng._userData.room.firstid ==0;
- this.who_round = GameMng._userData.room.firstid;
- this.updateRound();
- this.heqi_number = GameMng._userData.room.heqi_number;
- this.huiqi_number = GameMng._userData.room.huiqi_number;
- this.huiqi_setup = GameMng._userData.room.huiqi_setup;
- if(GameMng._userData.room.chess!=null&&GBoardChess.g_isStartFirstGame){
- if(GameMng._userData.room_info.chess.chess_whole===""||GameMng._userData.room_info.chess.chess_whole===undefined||GameMng._userData.room_info.chess.chess_whole===null){
- this.pos.fromFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1");
- }else{
- this.pos.fromFen(GameMng._userData.room_info.chess.chess_whole);
- this.pos.pushMvList(MOVE(GameMng._userData.room_info.chess.src_pos, GameMng._userData.room_info.chess.dec_pos))
- this.drawOldAndNewSelect(GameMng._userData.room_info.chess.src_pos, GameMng._userData.room_info.chess.dec_pos,true)
- }
- this.rund_number = GameMng._userData.room_info.chess.rund_number;
- this.heqi_number = GameMng._userData.room_info.chess.heqi_number;
- this.huiqi_number = GameMng._userData.room_info.chess.huiqi_number;
- this.checkWaitStatus()
- }else{
- this.pos.fromFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1");
- }
- this.flushBoard();
- this.isStartFirstGame = false;
- GBoardChess.g_isStartFirstGame = false;
-
- }
- showWaitHaoYou(){
- if(this.isStartFirstGame){
- this.bottom_node.active = false;
- this.wait_haoyou.active = true;
- this.chatNode.active = true;
- }
- }
- showWaitJiFen(){
- this.bottom_node.active = false;
- this.wait_haoyou.active = false;
- this.chatNode.active = true;
- this.wait_jifen.active = true;
- }
- //重新开始
- restartGame(){
- AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
- if(ChessMng.Instance.chesstype===ChessType.renjiboyi){
- this.wait_renji.active = false;
- }
- this.startGame();
- }
- //悔棋
- retract(){
- AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
- if (this.busy) {
- return;
- }
- if(!this.isStartGame){
- return;
- }
- // ADSDK.Share(null, () => {
-
- // }, null)
- this.result = RESULT_UNKNOWN;
- if (this.pos.mvList.length > 1) {
- this.pos.undoMakeMove();
- }
- if (this.pos.mvList.length > 1 && this.computerMove()) {
- this.pos.undoMakeMove();
- }
- if(this.mvLast>0){
- this.drawSquare(SRC(this.mvLast), false,false);
- this.drawSquare(DST(this.mvLast), false,false);
- this.drawOldAndNewSelect(SRC(this.mvLast),DST(this.mvLast),false)
- }
- this.flushBoard();
- this.response();
- this.hideAllPath();
- console.log("this.mvLast == ",this.mvLast)
- }
- //提示
- tishi(){
-
- AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
- if(this.busy||this.isComputerMoveing)return;
- ADSDK.Share(null,()=>{
- this.isComputerMoveing=true;
- console.log("tishi")
- this.search.searchMain(4,400,(a)=>{
- if(this.addMove(a,false)){
- this.hideAllPath()
- }
- });
- },null)
-
-
- }
- private getDiff() {
- return Math.pow(10, UIDiffSel.selectIndex + 1);
- }
- private initNeed() {
- this.animated = true;
- this.search = null;
- this.imgSquares = [];
- this.sqSelected = 0;
- this.mvLast = 0;
- this.millis = 0;
- this.computer = -1;
- this.result = RESULT_UNKNOWN;
- this.busy = false;
- this.pos = new ChessPos();
- this.setSearch(16);
- for (let sq = 0; sq < 256; sq++) {
- if (!IS_INBOARD(sq)) {
- this.imgSquares.push(null);
- continue;
- }
- let gnode=instantiate(this.chessNode);
-
- gnode.position = new Vec3( SQ_X(sq),SQ_Y(sq));
- gnode.getComponent(UITransform).setContentSize(SQUARE_SIZE,SQUARE_SIZE);
- gnode.parent=this.GBoardChess;
- var sp = gnode.getChildByName("chess_bg");
- (sp.getComponent(Sprite) as Sprite).enabled=false;
- gnode.getChildByName('track1').active=false;
- gnode.getChildByName('track2').active=false;
- gnode.on('click', () => {
- this.clickSquare(sq);
- }, this);
-
- this.imgSquares.push(gnode);
- }
- //this.hideAllTrack();
-
- }
- ShowSeting(){
- AudioMng.Instance.PlaySoundByName(GameMng.Instance.buttonclick);
- UISetting.Show();
- }
- // 点击棋盘的响应函数。点击棋盘(棋子或者空位置),就会调用该函数。sq_是点击的位置。
- clickSquare(sq_) {
- if(GameMng._userData.room){
- console.log(this.isStartGame,sq_,this.busy,this.result,this.who_round,this.sqSelected);
- }
-
- if(!this.isStartGame){
- return;
- }
- if (this.busy || this.result != RESULT_UNKNOWN) {
- return;
- }
- var sq = this.flipped(sq_);
- var pc = this.pos.squares[sq];
- // var col = FILE_X(sq_)-3
- // var row = RANK_Y(sq_)-3
- // console.log("col ",col);
- // console.log("row ",row);
- // console.log("pc ",pc,sq);
- if(this.sqSelected==sq){
- return;
- }
- if ((pc & SIDE_TAG(this.pos.sdPlayer)) != 0 ) {
- console.log("111");
- this.hideAllPath()
- AudioMng.Instance.PlaySoundByName(GameMng.Instance.pickclip);
- // if (this.mvLast != 0) {
- // this.drawSquare(SRC(this.mvLast), false,true);
- // this.drawSquare(DST(this.mvLast), false,true);
- // }
- if (this.sqSelected) {
- this.drawSquare(this.sqSelected, false,true);
- }
- this.drawSquare(sq, true,true);
- var temp_array = this.findAllCanMove(sq)
- this.drawCanMovePath(temp_array)
- this.sqSelected = sq;
- } else if (this.sqSelected > 0) {
- console.log("222");
- this.tempsqSelected = this.sqSelected;
- this.tempsqDes = sq;
- if(this.addMove(MOVE(this.sqSelected, sq), false)){
- this.hideAllPath();
- }
- }
- }
- private FaceKingWarning() //将军
- {
- let node = instantiate(this.FaceKingPrefab);
- node.parent = this.node;
- tween()
- .target(node)
- .to(0.5,{ scale: new Vec3(1, 1, 1) })
- .start();
- AudioMng.Instance.PlaySoundByName(GameMng.Instance.kingclip)
- }
- private onstartGameAni(){
- let node = instantiate(this.startGameAniPrefab);
- node.parent = this.node;
- tween()
- .target(node)
- .to(0.5,{ scale: new Vec3(1, 1, 1) })
- .start();
- console.log("onstartGameAni")
- }
- private onchiGameAni(){
- let node = instantiate(this.chiAniPrefab);
- node.parent = this.node;
- tween()
- .target(node)
- .to(0.5,{ scale: new Vec3(1, 1, 1) })
- .start();
- console.log("onchiGameAni")
- AudioMng.Instance.PlaySoundByName(GameMng.Instance.eatclip)
- }
- private onJueShaAni(){
- let node = instantiate(this.JueshaAniPrefab);
- node.parent = this.node;
- tween()
- .target(node)
- .to(0.5,{ scale: new Vec3(1, 1, 1) })
- .start();
- console.log("onJueShaAni")
- AudioMng.Instance.PlaySoundByName(GameMng.Instance.kill)
- }
- settlement(data:settlementData){
- AudioMng.Instance.PauseBGM();
- this.result_pos = Tools.getChessPos()
- this.isend=true;
- if(ChessMng.Instance.chesstype===ChessType.renjiboyi){
- if(!data.isheqi){
- //this.animation.play('win');
- AudioMng.Instance.PlaySoundByName(GameMng.Instance.kill)
- this.onJueShaAni()
- setTimeout(() => {
- this.jiesuan_node.active = true;
- this.jiesuan_node.getComponent(UIjiesuan).setData(data,GameUtil.FormatTime_HHMMSS(this.gTime*1000),this.bushu)
- //this.endview.active=true;
- //let nodets= this.endview.getComponent(UIEndChess);
- //nodets.setData(result,GameUtil.FormatTime_HHMMSS(this.gTime*1000),this.bushu);
-
- }, 2000);
- }
- }else {
- if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
- switch(data.result_type){
- case 0:{ //正常绝杀
- this.onJueShaAni()
- break;
- }
- case 1:{ //到时间
- if(data.result==1){
- UITips.show("对方到时间,您获得胜利!");
- }else{
- UITips.show("你的到时间,对方获得胜利!");
- }
-
- break;
- }
- case 2:{ //逃跑
- if(data.result==1){
- UITips.show("对方逃跑,获得胜利");
- }else{
- UITips.show("你逃跑输掉比赛");
- }
-
- break;
- }
- case 3:{ //认输
- if(data.result==1){
- UITips.show("对方认输,对局结束");
- }else{
- UITips.show("我方认输,对局结束");
- }
-
- break;
- }
- case 4:{ //和棋
- if(data.result==1){
-
- }else{
-
- }
-
- break;
- }
- }
- this.jiesuan_node.active = true;
- this.jiesuan_node.getComponent(UIjiesuan).setData(data,GameUtil.FormatTime_HHMMSS(this.gTime*1000),this.bushu)
- }
- }
-
-
- }
- private gameEnd(result,isheqi:boolean=false)
- {
- this.isStartGame =false;
- if(ChessMng.Instance.chesstype == ChessType.renjiboyi){
- let settlement_data = new settlementData()
- settlement_data.result = result
- settlement_data.isheqi = isheqi
- this.wait_renji.active = true;
- this.settlement(settlement_data)
- }else{
- console.log("result == ",result)
- if(result === 1){
- //msgManager.final_game()
- }else{
- console.log("result",result)
- console.log("isheqi",isheqi)
- }
- }
-
- }
- findAllCanMove(sq){
- return this.pos.findAllCanMove(sq)
- }
- moveAni(mv,computerMove,isRight:boolean=true){
- AudioMng.Instance.PlaySoundByName(GameMng.Instance.putclip)
- this.busy = true;
- if (!this.animated) {
- if(computerMove)this.isComputerMoveing=false;
- this.postAddMove(mv, computerMove);
- return false;
- }
- console.log("sqSrc",mv)
- var sqSrc = this.flipped(SRC(mv));
- var xSrc = SQ_X(sqSrc);
- var ySrc = SQ_Y(sqSrc);
- //console.log("sqSrc",sqSrc,SRC(mv),this.computer);
- var sqDst = this.flipped(DST(mv));
- var xDst = SQ_X(sqDst);
- var yDst = SQ_Y(sqDst);
- var getNode = this.imgSquares[sqSrc];
- var bg = getNode.getChildByName("chess_bg");
- var qz = bg.getChildByName("chess_qz");
- var _bg = this.moveNode.getChildByName("chess_bg");
- var _qz = _bg.getChildByName("chess_qz");
- _qz.active = true;
- this.moveNode.active=true;
- getNode.active =false;
- this.moveNode.removeFromParent()
- _bg.getComponent(Sprite).spriteFrame=bg.getComponent(Sprite).spriteFrame;
- _qz.getComponent(Sprite).spriteFrame=qz.getComponent(Sprite).spriteFrame;
- //this.moveNode.getComponent(Sprite).spriteFrame=getNode.getComponent(Sprite).spriteFrame;
- this.moveNode.parent=getNode.parent;
- var temp_pos = new Vec3(getNode.position.x,getNode.position.y,0)
- this.moveNode.position = temp_pos
- var step = MAX_STEP - 1;
- var this_ = this;
- tween(this.moveNode)
- .to(0.2, { position:new Vec3(xDst,yDst,0)})
- .call(async ()=>{
- if(isRight){
- if(computerMove)this_.isComputerMoveing=false;
- var _status = this_.postAddMove(mv, computerMove)
-
- if(!computerMove){
- if(ChessMng.Instance.isJiFenRoom()||ChessMng.Instance.isHaoyouRoom()){
- let setup = new setupData
- setup.src_pos = SRC(mv)
- setup.dec_pos = DST(mv)
- setup.chess_whole = this.pos.toFen()
- setup.is_mate = _status?0:1
- if(GameMng._userData.room.firstid==1){
- setup.des_who = "我是红方"
- }else{
- setup.des_who = "我是黑方"
- }
-
- if(GameMng._userData.room!=null){
- if(GameMng._userData.room.rival!=null){
- if(GameMng._userData.room.rival.is_robot===1){
- setup.robot_data = await this.robot_search(setup.chess_whole,setup.is_mate)
- console.log("给人机插入数据","给人机插入数据")
- }else{
- console.log("人机数据插入空值","人机数据插入空值")
- }
- }
- }
- this.send_msg_self_move(setup)
- }
-
-
- }
-
- this_.moveList.splice(0,1)
- if(this_.moveCallfun!=null){
- this_.moveCallfun()
- this_.moveCallfun = null
- }
- this_.moveNode.active=false;
- getNode.active =true;
- }else{
- this.busy = false;
- this_.moveNode.active=false;
- getNode.active =true;
- tween(this.moveNode)
- .to(0.1, { position:temp_pos}).start()
- }
-
- })
- .start();
- }
- robot_search(chess_whole,isMate) :Promise<setupData>{
- if(isMate===1){
- let robot_data = new setupData
- robot_data.is_mate = 0
- robot_data.chess_whole = chess_whole
- return
- }
- return new Promise((resolve, reject) => {
- this.scheduleOnce(() => {
- var search = new Search()
- var pos = new ChessPos();
- pos.fromFen(chess_whole);
- search.setSearch(pos,1)
-
- switch(GameMng._userData.room.rival.robot_ai_lv){
- case robot_ai_level.xiaobai:{
- search.xiaoBaiSearchMain((a)=>{
- director.once(Director.EVENT_AFTER_DRAW,function(){
- let robot_data = new setupData
- robot_data.src_pos = SRC(a)
- robot_data.dec_pos = DST(a)
- if(!pos.makeMove(a)){
- robot_data.src_pos = 0
- robot_data.dec_pos = 0
- }
- robot_data.is_mate = pos.isMate()?1:0
- robot_data.chess_whole = pos.toFen()
- resolve(robot_data)
- })
- });
- return;
- }
- }
- search.searchMain(GameMng._userData.room.rival.robot_ai_lv,400,(a)=>{
- var robot_data = new setupData
- robot_data.src_pos = SRC(a)
- robot_data.dec_pos = DST(a)
- if(!pos.makeMove(a)){
- robot_data.src_pos = 0
- robot_data.dec_pos = 0
- }
- robot_data.is_mate = pos.isMate()?1:0
- robot_data.chess_whole = pos.toFen()
- resolve(robot_data)
- });
- }, 0)
- })
-
- }
- send_msg_self_move(setup:setupData){
- if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
- msgManager.sendPlayChess(setup)
- }
- }
- /** 判断一步棋是否合法,如果合法,就执行这步棋。 */
- addMove(mv, computerMove) {
- console.log("addMove",mv)
- var is_legalMove = !this.pos.legalMove(mv)
- if (is_legalMove) {
- console.log("不合法",mv)
- this.moveAni(mv, computerMove,false)
- return false;
- }
- var is_makeMove = !this.pos.makeMove(mv)
- if (is_makeMove) {
- UITips.show("不能送将!");
- this.moveAni(mv, computerMove,false)
- return false;
- }
- this.moveAni(mv, computerMove)
- // var timer = setInterval(function() {
- // if (step == 0) {
- // clearInterval(timer);
- // getNode.x = xSrc;
- // getNode.y = ySrc;
- // if(computerMove)this_.isComputerMoveing=false;
- // this_.postAddMove(mv, computerMove);
- // } else {
- // getNode.x = MOVE_PX(xSrc, xDst, step);
- // getNode.y = MOVE_PX(ySrc, yDst, step);
- // step --;
- // }
- // }, 16);
- return true;
- }
- showHuiheLabel(computerMove){
- // if(this.computer==0)
- // {
- // if(computerMove)
- // this.huihelabel.string="黑方回合";
- // else this.huihelabel.string="红方回合";
- // }
- // else{
- // if(computerMove)
- // this.huihelabel.string="红方回合";
- // else this.huihelabel.string="黑方回合";
- // }
- }
- drawOldAndNewSelect(old_sq,new_sq,isShow){
- let old_node = this.imgSquares[this.flipped(old_sq)];
- let new_node = this.imgSquares[this.flipped(new_sq)];
- if(isShow){
- old_node.getChildByName("track1").active = true;
- old_node.getChildByName("track2").active = false;
- new_node.getChildByName("track1").active = false;
- new_node.getChildByName("track2").active = true;
- }else{
- old_node.getChildByName("track1").active = false;
- old_node.getChildByName("track2").active = false;
- new_node.getChildByName("track1").active = false;
- new_node.getChildByName("track2").active = false;
- }
-
- }
- postAddMove(mv, computerMove):boolean {
- this.showHuiheLabel(computerMove);
- if (this.mvLast > 0) {
- this.drawSquare(SRC(this.mvLast), false,false);
- this.drawSquare(DST(this.mvLast), false,false);
- this.drawOldAndNewSelect(SRC(this.mvLast),DST(this.mvLast),false)
- }
- this.drawSquare(SRC(mv), true,false);
- this.drawSquare(DST(mv), true,false);
- this.drawOldAndNewSelect(SRC(mv),DST(mv),true)
- this.sqSelected = 0;
- this.mvLast = mv;
-
- if (this.pos.isMate()) { //被将死
-
- this.result = computerMove ? RESULT_LOSS : RESULT_WIN;
-
- var pc = SIDE_TAG(this.pos.sdPlayer) + PIECE_KING;
- var sqMate = 0;
- for (var sq = 0; sq < 256; sq ++) {
- if (this.pos.squares[sq] == pc) {
- sqMate = sq;
- break;
- }
- }
- if (!this.animated || sqMate == 0) {
- this.postMate(computerMove);
- return false;
- }
- this.postMate(computerMove);
- return false;
- // sqMate = this.flipped(sqMate);
- // var style = this.imgSquares[sqMate].style;
- // style.zIndex = 256;
- // var xMate = SQ_X(sqMate);
- // var step = MAX_STEP;
- // var this_ = this;
- // var timer = setInterval(function() {
- // if (step == 0) {
- // clearInterval(timer);
- // style.left = xMate + "px";
- // style.zIndex = 0;
- // this_.imgSquares[sqMate].src = this_.images +
- // (this_.pos.sdPlayer == 0 ? "r" : "b") + "km.gif";
- // this_.postMate(computerMove);
- // } else {
- // style.left = (xMate + ((step & 1) == 0 ? step : -step) * 2) + "px";
- // step --;
- // }
- // }, 50);
- // return;
- }
-
- // var vlRep = this.pos.repStatus(3);
- // if (vlRep > 0) {
- // vlRep = this.pos.repValue(vlRep);
- // if (vlRep > -WIN_VALUE && vlRep < WIN_VALUE) {
- // UITips.show("双方不变作和");
- // //this.heqi();
- // } else if (computerMove == (vlRep < 0)) {
- // this.result = RESULT_LOSS;
- // UITips.show("长打作负");
- // //this.postMate(true);
- // } else {
- // this.result = RESULT_WIN;
- // UITips.show("长打作负");
- // //this.postMate(false);
- // }
- // return;
- // }
-
- if (this.pos.captured()) {
- var hasMaterial = false;
- for (var sq = 0; sq < 256; sq ++) {
- if (IS_INBOARD(sq) && (this.pos.squares[sq] & 7) > 2) {
- hasMaterial = true;
- break;
- }
- }
- if (!hasMaterial) {
- UITips.show("双方都没有进攻棋子了,作和!");
- this.heqi();
- return false;
- }
- } else if (this.pos.pcList.length > 100) {
- var captured = false;
- for (var i = 2; i <= 100; i ++) {
- if (this.pos.pcList[this.pos.pcList.length - i] > 0) {
- captured = true;
- break;
- }
- }
- if (!captured) {
- UITips.show("超过自然限着,作和");
- this.heqi();
- return false;
- }
- }
-
- if (this.pos.inCheck()) {
- this.FaceKingWarning();
- // this.playSound(computerMove ? "check2" : "check");
- } else if (this.pos.captured()) {
- this.onchiGameAni()
- } else {
-
- // AudioMng.Instance.PlaySoundByName(AudioType.put)
-
- }
-
- this.bushu++;
- this.refreshBushulabel();
- this.response();
- return true;
- }
- refreshBushulabel(){
- // this.bushulabel.string='步数:'+this.bushu;
- }
- //和棋做赢
- heqi(){
- this.result = RESULT_DRAW;
- this.busy = false;
- if(ChessType.renjiboyi === ChessMng.Instance.chesstype){
- this.gameEnd(0,true);
- }else{
- if(GameMng._userData.room.firstid==0){
- msgManager.heqi_game()
- }
- }
-
- }
- //结束
- postMate(computerMove) {
- console.log("移动的是不是电脑",computerMove,this.result)
- if(ChessType.renjiboyi === ChessMng.Instance.chesstype){
- let result=computerMove?1:0;
- this.gameEnd(result);
-
- }else{
-
- if(!computerMove){ //说明电脑被将死
- this.postMateNew();
- }
- }
- this.busy = false;
- }
- postMateNew(){
- console.log("result",this.result)
- //this.gameEnd(this.result);
- this.busy = false;
- }
- setSearch (hashLevel) {
- this.search = hashLevel == 0 ? null : new Search();
- this.search.setSearch(this.pos, hashLevel)
- }
- updateRound(){
- if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
- if(this.who_round===1){
- this.busy = false;
- }else{
- this.busy = true;
- }
- }
- }
- response() {
- if(GameMng._userData.room!=null){
- console.log("谁的回合",GameMng._userData.room.round)
- }
- if (this.search == null || !this.computerMove()) {
- this.busy = false;
- return;
- }
- // this.thinking.style.visibility = "visible";
- var self = this;
- this.busy = true;
- setTimeout(() => {
- if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
- if(UIDiffSel.selectIndex==0){
- self.search.xiaoBaiSearchMain((a)=>{
- director.once(Director.EVENT_AFTER_DRAW,function(){
- self.addMove(a,true);
- })
- });
- }else{
- self.search.searchMain(UIDiffSel.selectIndex+1,self.millis,(a)=>{
- director.once(Director.EVENT_AFTER_DRAW,function(){
- self.addMove(a,true);
- });
- });
- }
-
- }else{
- if(ChessMng.Instance.isHaoyouRoom()){
- console.log("轮到好友走棋子")
- }
- }
-
- }, 200);
-
-
-
- }
-
-
- // ab():Promise<any>{
- // return new Promise<any>(async (resolve, reject) => {
- // let a=this.search.searchMain(LIMIT_DEPTH, this.millis);
-
- // resolve(a);
-
- // });
-
- // }
-
- computerMove() {
- if(GameMng._userData.room===null||GameMng._userData.room===undefined){
- return this.pos.sdPlayer == this.computer;
- }
- if(ChessMng.Instance.chesstype==ChessType.renjiboyi){
- }else{
- if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()){
- return this.pos.sdPlayer == GameMng._userData.room.firstid;
- }
- }
- return this.pos.sdPlayer == this.computer;
- }
-
- //刷新棋盘,也就是重新显示棋盘上的棋子。
- flushBoard() {
- this.mvLast = this.pos.mvList[this.pos.mvList.length - 1];
- for (var sq = 0; sq < 256; sq ++) {
- if (IS_INBOARD(sq)) {
- this.drawSquare(sq, sq == SRC(this.mvLast) || sq == DST(this.mvLast),false);
- }
- }
- }
-
- drawCanMovePath(sqList){
- for(var i=0;i<sqList.length;i++){
- var sq = sqList[i];
- var gnodeSp = this.imgSquares[this.flipped(sq)]
- if(gnodeSp){
- gnodeSp.getChildByName('path').active=true;
- }
- }
- }
- hideAllPath(){
- for (let index = 0; index < this.imgSquares.length; index++) {
- const gnodeSp = this.imgSquares[index];
- if(gnodeSp){
- gnodeSp.getChildByName('path').active=false;
- }
- }
- }
- hideAllTrack(){
- for (let index = 0; index < this.imgSquares.length; index++) {
- const gnodeSp = this.imgSquares[index];
- if(gnodeSp){
- gnodeSp.getChildByName('track1').active=false;
- gnodeSp.getChildByName('track2').active=false;
- }
- }
- }
- //显示sq位置的棋子图片。如果该位置没棋子,则显示一张透明的图片。
- drawSquare(sq, selected,isSelfOpe) {
- var node__ = this.imgSquares[this.flipped(sq)];
- var gnodeSp =node__.getChildByName("chess_bg").getComponent(Sprite) as Sprite;
- var sp =gnodeSp.node.getChildByName("chess_qz").getComponent(Sprite) as Sprite;
- let spName=PIECE_NAME[this.pos.squares[sq]]
- var shadow = node__.getChildByName("chess_put_down_shadow");
- var self = this;
- if(spName=="oo"){
- gnodeSp.enabled=false;
- sp.node.active = false;
- shadow.active = false;
- }
- else{
- ResMng.LoadImg(spName,sp,()=>{
- this.moveNode.active=false;
- gnodeSp.enabled=true;
- shadow.active = true;
- sp.node.active = true;
- },this);
- }
- if(selected){
- if(isSelfOpe){
- // tween()
- // .target(gnodeSp.node)
- // .to(0.1,{ scale: new Vec3(1.1, 1.1, 1.1) })
- // .start();
- if(spName=="oo"){
-
- }
- else{
- ResMng.LoadImg("s_"+spName,sp,()=>{
- sp.node.position = new Vec3(sp.node.position.x,54,sp.node.position.z);
- },this);
- }
- gnodeSp.node.getComponent(UITransform).width = 112;
- gnodeSp.node.getComponent(UITransform).height = 117;
- gnodeSp.spriteFrame = self.select_chess_icon;
- }
- //gnodeSp.node.scale = new Vec3(1.1,1.1,1.1);
- }else{
- //gnodeSp.node.scale = new Vec3(1,1,1)
- sp.node.position = new Vec3(sp.node.position.x,48,sp.node.position.z);
- gnodeSp.node.getComponent(UITransform).width = self.moveNode.getChildByName("chess_bg").getComponent(UITransform).width;
- gnodeSp.node.getComponent(UITransform).height = self.moveNode.getChildByName("chess_bg").getComponent(UITransform).height;
- gnodeSp.spriteFrame = self.moveNode.getChildByName("chess_bg").getComponent(Sprite).spriteFrame;
- }
- // node__.getChildByName('selected').active=selected;
- // img.src = this.images + PIECE_NAME[this.pos.squares[sq]] + ".gif";
- // img.style.backgroundImage = selected ? "url(" + this.images + "oos.gif)" : "";
- }
- //此处判定画什么颜色的象棋
- flipped(sq) {
- return this.computer == 0 ? SQUARE_FLIP(sq) : sq;
- }
- huiqi(isReSelf:boolean){
- this.result = RESULT_UNKNOWN;
- if (this.pos.mvList.length > 1) {
- this.pos.undoMakeMove();
- }
- // if(this.computerMove()){
- // console.log("我方悔棋",this.pos.sdPlayer)
- // }else{
- // console.log("对方悔棋",this.pos.sdPlayer)
- // }
- // if (this.pos.mvList.length > 1 && this.computerMove()) {
- // this.pos.undoMakeMove();
- // }
- this.flushBoard();
- this.hideAllPath();
- console.log("isReSelf",isReSelf)
- if (this.mvLast > 0) {
- this.drawSquare(SRC(this.mvLast), false,false);
- this.drawSquare(DST(this.mvLast), false,false);
- this.drawOldAndNewSelect(SRC(this.mvLast),DST(this.mvLast),false)
- }
- if(!isReSelf){
- if(this.mv_rival_last>0){
- this.mvLast = this.mv_rival_last;
- this.drawOldAndNewSelect(SRC(this.mv_rival_last),DST(this.mv_rival_last),true)
- }
- }
- this.response();
- console.log("this.mvLast == ",this.mvLast)
- }
-
- }
|