1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657 |
- 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, size} 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';
- import { wait_tongqian } from '../wait_tongqian';
- 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)
- chess_nor_Node: Node = null;
- @property(Node)
- wait_haoyou: Node = null;
- @property(Node)
- wait_jifen: Node = null;
- @property(Node)
- wait_renji: Node = null;
- @property(Node)
- wait_tongqian: 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;
-
- mv_self_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.mv_self_last = MOVE(setup.src_pos, setup.dec_pos)
- // 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)
- }else if(ChessMng.Instance.isTongqianRoom()){
- this.showWaitTongQian()
- self.wait_tongqian.getComponent(wait_tongqian).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)
- }else if(ChessMng.Instance.isTongqianRoom()){
- this.wait_tongqian.getComponent(wait_tongqian).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();
- }else if(ChessMng.Instance.isTongqianRoom()){
- this.showWaitTongQian();
- }
- }
- 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;
- } else if(ChessMng.Instance.isTongqianRoom()){
- this.showAllChess();
- this.wait_tongqian.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;
- }
- showWaitTongQian(){
- this.bottom_node.active = false;
- this.wait_haoyou.active = false;
- this.chatNode.active = true;
- this.wait_jifen.active = false;
- this.wait_tongqian.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.chess_nor_Node);
-
- 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()
- 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);
- }
- }else {
- if(ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isJiFenRoom()||ChessMng.Instance.isTongqianRoom()){
- 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",xSrc,ySrc,computerMove);
- var sqDst = this.flipped(DST(mv));
- var xDst = SQ_X(sqDst);
- var yDst = SQ_Y(sqDst);
- console.log("sqDst",xDst,yDst);
- var getNode = this.imgSquares[sqSrc];
- // this.moveNode = instantiate(getNode)
- 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()
- //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;
- let scale_ = 1.04
- let dir = 1
- _bg.getComponent(Sprite).spriteFrame=bg.getComponent(Sprite).spriteFrame;
- _qz.getComponent(Sprite).spriteFrame=qz.getComponent(Sprite).spriteFrame;
- if(computerMove){
- //console.log("bg", bg.getComponent(Sprite).spriteFrame.name);
- _bg.getComponent(Sprite).spriteFrame=this.select_chess_icon;
- _qz.getComponent(Sprite).spriteFrame=qz.getComponent(Sprite).spriteFrame;
- }
- _bg.getComponent(UITransform).contentSize =size(112,117)
- _qz.getComponent(UITransform).contentSize =size(90,83)
- //_bg.position = new Vec3(bg.position.x,bg.position.y,bg.position.z)
- _qz.position = new Vec3(0,13,qz.position.z)
-
- if(!isRight){
- tween(this.moveNode)
- .to(0.2, { position:new Vec3(xDst,yDst,0)}).call(()=>{
- this.busy = false;
- this_.moveNode.active=false;
- getNode.active =true;
- tween(this.moveNode)
- .to(0.1, { position:temp_pos}).start()
- }).start()
- return
- }
-
- if(yDst>ySrc){
- }else if(yDst<ySrc){
- //dir = -1;
- }
- _bg.scale = new Vec3(scale_,scale_,scale_)
- _qz.scale = new Vec3(scale_,scale_,scale_)
- tween(this.moveNode)
- .to(0.2, { position:new Vec3(xDst,yDst,0)})
- .delay(0.1)
- .call(()=>{
- if(computerMove){
- _bg.scale = new Vec3(scale_,scale_,scale_)
- _qz.scale = new Vec3(scale_,scale_,scale_)
- }else{
- _bg.scale = new Vec3(scale_,scale_*dir,scale_)
- _qz.scale = new Vec3(scale_,scale_*dir,scale_)
- }
-
- }).delay(0.1)
- .call(async ()=>{
- _bg.scale = new Vec3(1.1,1.1,1.1)
- _qz.scale = new Vec3(1.1,1.1,1.1)
- if(computerMove)this_.isComputerMoveing=false;
- var _status = this_.postAddMove(mv, computerMove)
- if(!computerMove){
- if(ChessMng.Instance.isJiFenRoom()||ChessMng.Instance.isHaoyouRoom()||ChessMng.Instance.isTongqianRoom()){
- 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;
-
- })
- .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()||ChessMng.Instance.isTongqianRoom()){
- 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)
- }
- if (this.mv_self_last > 0) {
- this.drawOldAndNewSelect(SRC(this.mv_self_last),DST(this.mv_self_last),false)
- }
- if (this.mv_rival_last > 0) {
- this.drawOldAndNewSelect(SRC(this.mv_rival_last),DST(this.mv_rival_last),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()||ChessMng.Instance.isTongqianRoom()){
- 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()||ChessMng.Instance.isTongqianRoom()){
- 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.getComponent(UITransform).width = 90;
- sp.node.getComponent(UITransform).height = 83;
- sp.node.position = new Vec3(sp.node.position.x,13,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.getComponent(UITransform).width = 80;
- sp.node.getComponent(UITransform).height = 79;
- sp.node.position = new Vec3(sp.node.position.x,10,sp.node.position.z);
- gnodeSp.spriteFrame = self.moveNode.getChildByName("chess_bg").getComponent(Sprite).spriteFrame;
- gnodeSp.node.getComponent(UITransform).width = 104;
- gnodeSp.node.getComponent(UITransform).height = 107;
- }
- // 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)
- console.log("this.mvLast",this.mvLast)
- 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.drawOldAndNewSelect(SRC(this.mv_rival_last),DST(this.mv_rival_last),true)
- }
- }else{
- if(this.mv_self_last>0){
- this.mvLast = this.mv_rival_last;
- this.drawOldAndNewSelect(SRC(this.mv_self_last),DST(this.mv_self_last),true)
- }
- }
- this.response();
- console.log("this.mvLast == ",this.mvLast)
- }
-
- }
|